JMS学习笔记(三)-ActiveMQ简单HelloWord
1. 导读
在第一篇博客中,我们介绍了JMS的两种消息模型:点对点(P2P)和发布订阅(Pub/Sub)。这篇博文我们使用ActiveMQ实现一个简单的P2P模型
2. 开发环境
ActiveMQ-window-5.14.1
JDK 1.7
tomcat 7
3. 建立项目
这里使用的是ActiveMQ 5.14.1,下载地址参考上一篇。下载解压后把里面的activemq-all-5.14.1.jar包导入到项目中,这个jar包包含了所以JMS接口api的实现。
点对点的消息模型只需要一个消息生产者和一个消息消费者,下面开始编写具体代码
4. 生产者
/** * 生产者 * @author mengY */ public class JMSProducer { //默认连接用户名 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; //发送的消息数量 private static final int SENDNUM = 10; public static void main(String[] args) { //连接工厂 ConnectionFactory connectionFactory; //连接 Connection connection = null; //会话 接收或者发送消息的线程 Session session; //消息的目的地 Destination destination; //消息生产者 MessageProducer messageProducer; //实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL); try{ //从工厂中拿连接,然后启动 connection = connectionFactory.createConnection(); connection.start(); //创建Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //创建队列 destination = session.createQueue("HelloWorld"); //创建消息产生者 messageProducer = session.createProducer(destination); //发送 sendMessage(session,messageProducer); session.commit(); }catch(Exception e){ e.printStackTrace(); }finally{ if(connection != null){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } /** * 发送消息 * @param session * @param messageProducer 消息产生者 * @throws JMSException */ private static void sendMessage(Session session,MessageProducer messageProducer) throws JMSException { for (int i = 0; i < JMSProducer.SENDNUM; i++) { TextMessage textMessage=session.createTextMessage("ActiveMQ 发送消息:"+i); System.out.println("发送数据:"+i); messageProducer.send(textMessage); } }
5. 消费者代码
/** * 消费者 * @author mengY */ public class JMSConsumer { 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) { //连接工厂 ConnectionFactory connectionFactory; //连接 Connection connection = null; //会话 接收或者发送消息的线程 Session session; //消息的目的地 Destination destination; //消息的消费者 MessageConsumer messageConsumer; //实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL); try{ //拿到连接,然后启动连接 connection = connectionFactory.createConnection(); connection.start(); //创建会话 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //创建队列 destination = session.createQueue("HelloWorld"); //创建消费者 messageConsumer = session.createConsumer(destination); //获取队列中的数据 receiveMessage(messageConsumer); }catch(Exception e){ e.printStackTrace(); }finally{ if(connection != null){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } /** * 接收消息 * @param messageConsumer 消息消费者 * @throws JMSException */ private static void receiveMessage(MessageConsumer messageConsumer) throws JMSException { while(true){ TextMessage textMessage = (TextMessage) messageConsumer.receive(100000); if(textMessage!=null){ System.out.println("接收到:"+textMessage.getText()); } } }
6. 运行
启动activeMQ服务,参考第二篇,然后运行生产者
然后在浏览器中查看ActiveMQ服务器,Queues内容如下:
然后我们可以看见一个名为HelloWorld消息队列,队列中有10条数据未被消费。
现在我们运行消费者代码。
我们可以看到消息被一个消费者消费掉了,现在队列中为空。






浙公网安备 33010602011771号