`
zhaohaolin
  • 浏览: 984033 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ActiveMQ 实例

    博客分类:
  • JMS
阅读更多

2009-06-24

ProducerTool.java用于发送消息:

 

  1. import javax.jms.Connection;     
  2. import javax.jms.DeliveryMode;     
  3. import javax.jms.Destination;     
  4. import javax.jms.JMSException;     
  5. import javax.jms.MessageProducer;     
  6. import javax.jms.Session;     
  7. import javax.jms.TextMessage;     
  8.     
  9. import org.apache.activemq.ActiveMQConnection;     
  10. import org.apache.activemq.ActiveMQConnectionFactory;     
  11.     
  12. public class ProducerTool {     
  13.     
  14.     private String user = ActiveMQConnection.DEFAULT_USER;     
  15.     
  16.     private String password = ActiveMQConnection.DEFAULT_PASSWORD;     
  17.     
  18.     private String url = ActiveMQConnection.DEFAULT_BROKER_URL;     
  19.     
  20.     private String subject = "TOOL.DEFAULT";     
  21.     
  22.     private Destination destination = null;     
  23.     
  24.     private Connection connection = null;     
  25.     
  26.     private Session session = null;     
  27.     
  28.     private MessageProducer producer = null;     
  29.     
  30.     // 初始化     
  31.     private void initialize() throws JMSException, Exception {     
  32.         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(     
  33.                 user, password, url);     
  34.         connection = connectionFactory.createConnection();     
  35.         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);     
  36.         destination = session.createQueue(subject);     
  37.         producer = session.createProducer(destination);     
  38.         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);     
  39.     }     
  40.     
  41.     // 发送消息     
  42.     public void produceMessage(String message) throws JMSException, Exception {     
  43.         initialize();     
  44.         TextMessage msg = session.createTextMessage(message);     
  45.         connection.start();     
  46.         System.out.println("Producer:->Sending message: " + message);     
  47.         producer.send(msg);     
  48.         System.out.println("Producer:->Message sent complete!");     
  49.     }     
  50.     
  51.     // 关闭连接     
  52.     public void close() throws JMSException {     
  53.         System.out.println("Producer:->Closing connection");     
  54.         if (producer != null)     
  55.             producer.close();     
  56.         if (session != null)     
  57.             session.close();     
  58.         if (connection != null)     
  59.             connection.close();     
  60.     }     
  61. }      

 

ConsumerTool.java用于接受消息,我用的是基于消息监听的机制,需要实现MessageListener接口,这个接口有个onMessage方法,当接受到消息的时候会自动调用这个函数对消息进行处理。

 

  1. import javax.jms.Connection;     
  2. import javax.jms.Destination;     
  3. import javax.jms.JMSException;     
  4. import javax.jms.MessageConsumer;     
  5. import javax.jms.Session;     
  6. import javax.jms.MessageListener;     
  7. import javax.jms.Message;     
  8. import javax.jms.TextMessage;     
  9.     
  10. import org.apache.activemq.ActiveMQConnection;     
  11. import org.apache.activemq.ActiveMQConnectionFactory;     
  12.     
  13. public class ConsumerTool implements MessageListener {     
  14.     
  15.     private String user = ActiveMQConnection.DEFAULT_USER;     
  16.     
  17.     private String password = ActiveMQConnection.DEFAULT_PASSWORD;     
  18.     
  19.     private String url = ActiveMQConnection.DEFAULT_BROKER_URL;     
  20. padding-top: 0px !important; padding-right: 3px !important; paddin
    分享到:
    评论

相关推荐

Global site tag (gtag.js) - Google Analytics