ActiveMQ
ActiveMQ
1.什么是ActiveMQ
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
2.ActiveMQ的下载
主页:http://activemq.apache.org/
开发包及源码下载地址:http://activemq.apache.org/activemq-5111-release.html
ActiveMQ 服务启动地址:http://127.0.0.1:8161/admin/ 用户名/密码 admin/admin
ActiveMQ的点对点使用
Session.AUTO_ACKNOWLEDGE。当客户成功的从receive 方法返回的时候,或者从MessageListener.onMessage方法成功返回的时候,会话自动确认客户收到的消息。
Session.CLIENT_ACKNOWLEDGE。 客户通过消息的 acknowledge 方法确认消息。需要注意的是,在这种模式中,确认是在会话层上进行:确认一个被消费的消息将自动确认所有已被会话消 费的消息。例如,如果一个消息消费者消费了 10 个消息,然后确认第 5 个消息,那么所有 10 个消息都被确认。
Session.DUPS_ACKNOWLEDGE。 该选择只是会话迟钝的确认消息的提交。如果 JMS provider 失败,那么可能会导致一些重复的消息。如果是重复的消息,那么 JMS provider 必须把消息头的 JMSRedelivered 字段设置为 true。
消息生产者(MessageProducer)代码
package com.royal; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Producer { private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码 private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址 public static void main(String[] args) { System.out.println("host: " + BROKEURL); ConnectionFactory connectionFactory = null;//连接工厂 Connection connection = null;//连接 Session session = null;//会话 Destination destination = null;//消息目的地,队列 MessageProducer messageProducer = null;//消息生产者 connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL); try { connection = connectionFactory.createConnection(); connection.start();//启动连接 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("短信发送"); messageProducer = session.createProducer(destination); //发送消息 for (int i=0;i<10;i++){ TextMessage textMessage = session.createTextMessage("13800001111:100" + i); messageProducer.send(destination,textMessage); } } catch (JMSException e) { e.printStackTrace(); }finally { try { connection.close(); session.close(); messageProducer.close(); } catch (JMSException e) { e.printStackTrace(); } } } }
消息消费者(MessageConsumer )代码
package com.royal; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Consumer { private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码 private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址 public static void main(String[] args) { System.out.println("host: " + BROKEURL); ConnectionFactory connectionFactory = null;//连接工厂 Connection connection = null;//连接 Session session = null;//会话 Destination destination = null;//消息目的地,队列 MessageConsumer messageConsumer = null;//消息生产者 connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL); try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("短信发送"); messageConsumer = session.createConsumer(destination); //接受消息,主动获取 /*for (int i=0;i<5;i++){ TextMessage textMessage = (TextMessage) messageConsumer.receive(); System.out.println(textMessage.getText()); }*/ //使用监听器接受消息 messageConsumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("消费的消息:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }); } catch (JMSException e) { e.printStackTrace(); }finally { /*try { //使用监听器,不需关闭资源 connection.close(); session.close(); messageConsumer.close(); } catch (JMSException e) { e.printStackTrace(); }*/ } } }
ActiveMQ的发布和订阅
订阅代码
package com.royal; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Consumer1 { private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码 private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址 public static void main(String[] args) { System.out.println("host: " + BROKEURL); ConnectionFactory connectionFactory = null;//连接工厂 Connection connection = null;//连接 Session session = null;//会话 Destination destination = null;//消息目的地,队列 MessageConsumer messageConsumer = null;//消息生产者 connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL); try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createTopic("短信发送"); messageConsumer = session.createConsumer(destination); //使用监听器接受消息 messageConsumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("消费的消息:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }); } catch (JMSException e) { e.printStackTrace(); } } }
copy几个进行测试
发布代码
package com.royal; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Producer { private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码 private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址 public static void main(String[] args) { System.out.println("host: " + BROKEURL); ConnectionFactory connectionFactory = null;//连接工厂 Connection connection = null;//连接 Session session = null;//会话 Destination destination = null;//消息目的地,队列 MessageProducer messageProducer = null;//消息生产者 connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL); try { connection = connectionFactory.createConnection(); connection.start();//启动连接 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createTopic("短信发送"); messageProducer = session.createProducer(destination); //发送消息 for (int i=0;i<10;i++){ TextMessage textMessage = session.createTextMessage("13800001111:100" + i); messageProducer.send(destination,textMessage); } } catch (JMSException e) { e.printStackTrace(); }finally { try { connection.close(); session.close(); messageProducer.close(); } catch (JMSException e) { e.printStackTrace(); } } } }