ActiveMQ实战1:ActiveMQ Java

1.准备工作

  1) 下载安装,启动activemq

  2) 下载activemq   jar包导入项目

2.消息生产者

Java代码   收藏代码
  1. package com.activemq.demo1;  
  2.   
  3. import javax.jms.*;  
  4. import org.apache.activemq.*;  
  5. /** 
  6.  * 消息生产者,用于生成并发送消息 
  7.  */  
  8. public class ProducerTool {  
  9.       
  10.     private String user = ActiveMQConnection.DEFAULT_USER;      
  11.     private String password = ActiveMQConnection.DEFAULT_PASSWORD;      
  12.     private String url = ActiveMQConnection.DEFAULT_BROKER_URL;      
  13.     private String subject = "TOOL.DEFAULT";      
  14.     private Destination destination = null;      
  15.     private Connection connection = null;      
  16.     private Session session = null;      
  17.     private MessageProducer producer = null;      
  18.      
  19.     /** 
  20.      * 初始化     
  21.      * @throws Exception 
  22.      */  
  23.     private void initialize() throws Exception {      
  24.         ActiveMQConnectionFactory connectionFactory =   
  25.                 new ActiveMQConnectionFactory(user, password, url);      
  26.         connection = connectionFactory.createConnection();  
  27.         /* 创建Session,参数解释:             
  28.                             第一个参数     是否使用事务:当消息发送者向消息提供者(即消息代理)发送消息时,消息发送者等待消息代理的确认, 
  29.             没有回应则抛出异常,消息发送程序负责处理这个错误。             
  30.                             第二个参数      消息的确认模式:             
  31.               AUTO_ACKNOWLEDGE : 指定消息提供者在每次收到消息时自动发送确认。消息只向目标发送一次, 
  32.                 但传输过程中可能因为错误而丢失消息。            
  33.               CLIENT_ACKNOWLEDGE : 由消息接收者确认收到消息,通过调用消息的acknowledge()方法 
  34.                 (会通知消息提供者收到了消息)             
  35.               DUPS_OK_ACKNOWLEDGE : 指定消息提供者在消息接收者没有确认发送时重新发送消息 
  36.                 (这种确认模式不在乎接收者收到重复的消息)。*/  
  37.         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);      
  38.         destination = session.createQueue(subject);      
  39.         producer = session.createProducer(destination);    
  40.         //设置是否持久化  
  41.         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);      
  42.     }      
  43.      
  44.     /** 
  45.      * 发送消息     
  46.      * @param message 
  47.      * @throws Exception 
  48.      */  
  49.     public void produceMessage(String message) throws Exception {      
  50.         initialize();   
  51.         //发送TextMessage,还可发送MapMessage,ObjectMessage,StreamMessage  
  52.         TextMessage msg = session.createTextMessage(message);  
  53.         connection.start();   
  54.         System.out.println("Producer:-> send start.");  
  55.         producer.send(msg);   
  56.         System.out.println("Producer:-> send complete.");  
  57.         close();  
  58.     }      
  59.      
  60.     /** 
  61.      * 关闭连接     
  62.      * @throws JMSException 
  63.      */  
  64.     public void close() throws JMSException {   
  65.         System.out.println("Producer:->Closing Connection.");  
  66.         if (producer != null)      
  67.             producer.close();      
  68.         if (session != null)      
  69.             session.close();      
  70.         if (connection != null)      
  71.             connection.close();      
  72.     }      
  73. }      

3.消息消费者

Java代码   收藏代码
  1. package com.activemq.demo1;  
  2.   
  3. import javax.jms.*;  
  4. import javax.jms.Message;  
  5.   
  6. import org.apache.activemq.*;  
  7. /** 
  8.  * 消息消费者,用于接收消息 
  9.  */  
  10. public class ConsumerTool implements MessageListener {    
  11.       
  12.     private String user = ActiveMQConnection.DEFAULT_USER;      
  13.     private String password = ActiveMQConnection.DEFAULT_PASSWORD;      
  14.     private String url = ActiveMQConnection.DEFAULT_BROKER_URL;      
  15.     private String subject = "TOOL.DEFAULT";      
  16.     private Destination destination = null;      
  17.     private Connection connection = null;      
  18.     private Session session = null;      
  19.     private MessageConsumer consumer = null;      
  20.      
  21.     /** 
  22.      * 初始化     
  23.      * @throws JMSException 
  24.      * @throws Exception 
  25.      */  
  26.     private void initialize() throws Exception {      
  27.         ActiveMQConnectionFactory connectionFactory =   
  28.                 new ActiveMQConnectionFactory(user, password, url);      
  29.         connection = connectionFactory.createConnection();      
  30.         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);      
  31.         destination = session.createQueue(subject);      
  32.         consumer = session.createConsumer(destination);      
  33.     }      
  34.      
  35.     /** 
  36.      * 消费消息     
  37.      * @throws Exception 
  38.      */  
  39.     public void consumeMessage() throws Exception {      
  40.         initialize();      
  41.         connection.start();      
  42.         System.out.println("Consumer:->Begin listening...");      
  43.         // 开始监听      
  44.         consumer.setMessageListener(this);   
  45.     }      
  46.      
  47.     /** 
  48.      * 关闭连接     
  49.      * @throws JMSException 
  50.      */  
  51.     public void close() throws JMSException {      
  52.         System.out.println("Consumer:->Closing connection");      
  53.         if (consumer != null)      
  54.             consumer.close();      
  55.         if (session != null)      
  56.             session.close();      
  57.         if (connection != null)      
  58.             connection.close();      
  59.     }      
  60.      
  61.     /** 
  62.      *  消息处理函数     
  63.      */  
  64.     public void onMessage(Message message) {   
  65.         try {      
  66.             if (message instanceof TextMessage) {      
  67.                 TextMessage txtMsg = (TextMessage) message;      
  68.                 String msg = txtMsg.getText();      
  69.                 System.out.println("Consumer:->Received textMessage: " + msg);      
  70.             } else {      
  71.                 System.out.println("Consumer:->Received: " + message);      
  72.             }   
  73.             close();  
  74.         } catch (JMSException e) {      
  75.             e.printStackTrace();      
  76.         }      
  77.     }  
  78.   
  79. }    

4.测试类

Java代码   收藏代码
  1. package com.activemq.demo1;  
  2.   
  3. import javax.jms.*;  
  4.   
  5. public class Test {      
  6.      
  7.     /**    
  8.      * @param args    
  9.      */     
  10.     public static void main(String[] args) throws JMSException, Exception {      
  11.         ConsumerTool consumer = new ConsumerTool();      
  12.         ProducerTool producer = new ProducerTool();      
  13.         // 开始监听      
  14.         consumer.consumeMessage();      
  15.               
  16.         // 延时500毫秒之后发送消息      
  17.         Thread.sleep(500);      
  18.         producer.produceMessage("Hello, world!");      
  19.               
  20.     }      
  21. }   



原文地址:http://425826501.iteye.com/blog/2198115





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值