ActiveMQ之HelloWorld

来源:互联网 发布:淘宝万艾可 编辑:程序博客网 时间:2024/06/07 18:16

篇文章从代码角度去实现一个mq。因为ActiveMQ是对JMS的一种实现,因此,AMQ的开发步骤就应该和JMS的开发模型一样。

1. 创建ConnectionFactory

2. 创建Connection

3. 创建Session

4. 创建Destination/Topic

5. 创建Producer/Consumer

[java] view plain copy
  1. package com.zhuyang.mq.p2p;  
  2.   
  3. import javax.jms.Connection;  
  4. import javax.jms.ConnectionFactory;  
  5. import javax.jms.Destination;  
  6. import javax.jms.JMSException;  
  7. import javax.jms.MessageProducer;  
  8. import javax.jms.Session;  
  9. import javax.jms.TextMessage;  
  10.   
  11. import org.apache.activemq.ActiveMQConnection;  
  12. import org.apache.activemq.ActiveMQConnectionFactory;  
  13.   
  14. public class Producer {  
  15.     // default connection username  
  16.     private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;  
  17.     // default connection password  
  18.     private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;  
  19.     // default connection url  
  20.     private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;  
  21.   
  22.     public static void main(String[] args) {  
  23.         ConnectionFactory cf = null;  
  24.         Connection connection = null;  
  25.         // session used to revieve or send  
  26.         Session session = null;  
  27.         // message destination  
  28.         Destination destination = null;  
  29.         MessageProducer messageProducer = null;  
  30.         // create ConnectionFactory  
  31.         try {  
  32.             cf = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);  
  33.             // create activemq connection  
  34.             connection = cf.createConnection();  
  35.             connection.start();  
  36.             // create session  
  37.             session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);  
  38.             // create a queue name= helloworld  
  39.             destination = session.createQueue("helloworld");  
  40.             // create MessageProducer  
  41.             messageProducer = session.createProducer(destination);  
  42.             for (int i = 0; i < 10; i++) {  
  43.                 TextMessage msg = session.createTextMessage("hello" + i);  
  44.                 messageProducer.send(msg);  
  45.             }  
  46.             session.commit();  
  47.         } catch (JMSException e) {  
  48.             // TODO Auto-generated catch block  
  49.             e.printStackTrace();  
  50.         } finally {  
  51.             try {  
  52.                 connection.close();  
  53.             } catch (JMSException e) {  
  54.                 // TODO Auto-generated catch block  
  55.                 e.printStackTrace();  
  56.             }  
  57.         }  
  58.   
  59.     }  
  60.   
  61. }  

运行完后active mq控制台会显示出创建的Queue"helloworld"



[java] view plain copy
  1. package com.zhuyang.mq.p2p;  
  2.   
  3. import javax.jms.Connection;  
  4. import javax.jms.ConnectionFactory;  
  5. import javax.jms.Destination;  
  6. import javax.jms.JMSException;  
  7. import javax.jms.MessageConsumer;  
  8. import javax.jms.Session;  
  9. import javax.jms.TextMessage;  
  10.   
  11. import org.apache.activemq.ActiveMQConnection;  
  12. import org.apache.activemq.ActiveMQConnectionFactory;  
  13.   
  14. public class Consumer {  
  15.     // default connection username  
  16.     private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;  
  17.     // default connection password  
  18.     private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;  
  19.     // default connection url  
  20.     private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;  
  21.   
  22.     public static void main(String[] args) {  
  23.         ConnectionFactory cf = null;  
  24.         Connection connection = null;  
  25.         Session session = null;  
  26.         Destination destination = null;  
  27.         MessageConsumer messageConsumer;  
  28.   
  29.         try {  
  30.             cf = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);  
  31.             connection = cf.createConnection();  
  32.             connection.start();  
  33.             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  34.             destination = session.createQueue("helloworld");  
  35.             messageConsumer = session.createConsumer(destination);  
  36.             while (true) {  
  37.                 TextMessage msg = (TextMessage) messageConsumer.receive(100000);  
  38.                 if (msg != null) {  
  39.                     System.out.println("message recieved:" + msg.getText());  
  40.                 } else {  
  41.                     break;  
  42.                 }  
  43.             }  
  44.         } catch (JMSException e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.     }  
  50.   
  51. }  

Consumer执行完后

[html] view plain copy
  1. message recieved:hello0  
  2. message recieved:hello1  
  3. message recieved:hello2  
  4. message recieved:hello3  
  5. message recieved:hello4  
  6. message recieved:hello5  
  7. message recieved:hello6  
  8. message recieved:hello7  
  9. message recieved:hello8  
  10. message recieved:hello9