JMS入门(六)--DeliveryMode



1.在下面的例子中,分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS。

发送端代码如下:

[java]  view plain copy
  1. /** 
  2.  * @author Administrator 
  3.  * @description 分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS 
  4.  * 运行这个程序,当输出“Send messages sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。 
  5.  */  
  6. package com.wl.jms;  
  7.   
  8. import javax.jms.Connection;  
  9. import javax.jms.DeliveryMode;  
  10. import javax.jms.JMSException;  
  11. import javax.jms.MessageProducer;  
  12. import javax.jms.Queue;  
  13. import javax.jms.Session;  
  14.   
  15. import org.apache.activemq.ActiveMQConnectionFactory;  
  16. import org.apache.activemq.command.ActiveMQQueue;  
  17.   
  18. public class DeliveryModeSendTest {  
  19.   
  20.     /** 
  21.      * @param args 
  22.      * @throws JMSException  
  23.      */  
  24.     public static void main(String[] args) throws JMSException {  
  25.         // TODO Auto-generated method stub  
  26.         ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");  
  27.         Connection connection=factory.createConnection();  
  28.         connection.start();  
  29.           
  30.         Queue queue=new ActiveMQQueue("testQueue");  
  31.         Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  32.         //创建一个消息的生产者  
  33.         MessageProducer producer=session.createProducer(queue);  
  34.         //设置消息的DeliveryMode为Persistent  
  35.         producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
  36.         producer.send(session.createTextMessage("A persistent Message"));  
  37.         //设置消息的DeliveryMode为Non-Persistent  
  38.         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
  39.         producer.send(session.createTextMessage("A non persistent Message"));  
  40.           
  41.         System.out.println("Send messages successfully!");  
  42.     }  
  43.   
  44. }  
运行上面的程序,结果如下:


当输出“Send messages sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。
2. 接下来我们重新启动JMS Provicer,然后添加一个消费者:

[java]  view plain copy
  1. /** 
  2.  * @author Administrator 
  3.  * @description 这个程序是基于DeliveryModeSendTest基础上来做的 
  4.  * 运行上面的程序,可以得到下面的输出结果:Consumer get:A persistent Message 
  5.  * 可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。 
  6.  */  
  7. package com.wl.jms;  
  8.   
  9. import javax.jms.Connection;  
  10. import javax.jms.JMSException;  
  11. import javax.jms.Message;  
  12. import javax.jms.MessageConsumer;  
  13. import javax.jms.MessageListener;  
  14. import javax.jms.Queue;  
  15. import javax.jms.Session;  
  16. import javax.jms.TextMessage;  
  17.   
  18. import org.apache.activemq.ActiveMQConnectionFactory;  
  19. import org.apache.activemq.command.ActiveMQQueue;  
  20.   
  21. public class DeliveryModeReceiveTest {  
  22.   
  23.     /** 
  24.      * @param args 
  25.      * @throws JMSException  
  26.      */  
  27.     public static void main(String[] args) throws JMSException {  
  28.         // TODO Auto-generated method stub  
  29.         ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");  
  30.         Connection connection=factory.createConnection();  
  31.         connection.start();  
  32.           
  33.         Queue queue=new ActiveMQQueue("testQueue");  
  34.         Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  35.         //创建消息的接收者,来接受DeliveryModeSendTest中发送的消息  
  36.         MessageConsumer consumer=session.createConsumer(queue);  
  37.         consumer.setMessageListener(new MessageListener(){  
  38.   
  39.             public void onMessage(Message m) {  
  40.                 // TODO Auto-generated method stub  
  41.                 try {  
  42.                     System.out.println("Consumer get: "+((TextMessage)m).getText());  
  43.                 } catch (JMSException e) {  
  44.                     // TODO Auto-generated catch block  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.               
  49.         });  
  50.     }  
  51.   
  52. }  
运行这个接受端的程序,结果如下:


可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。
另外, 如果发送一个non persistent消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失.



1.在下面的例子中,分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS。

发送端代码如下:

[java]  view plain copy
  1. /** 
  2.  * @author Administrator 
  3.  * @description 分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS 
  4.  * 运行这个程序,当输出“Send messages sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。 
  5.  */  
  6. package com.wl.jms;  
  7.   
  8. import javax.jms.Connection;  
  9. import javax.jms.DeliveryMode;  
  10. import javax.jms.JMSException;  
  11. import javax.jms.MessageProducer;  
  12. import javax.jms.Queue;  
  13. import javax.jms.Session;  
  14.   
  15. import org.apache.activemq.ActiveMQConnectionFactory;  
  16. import org.apache.activemq.command.ActiveMQQueue;  
  17.   
  18. public class DeliveryModeSendTest {  
  19.   
  20.     /** 
  21.      * @param args 
  22.      * @throws JMSException  
  23.      */  
  24.     public static void main(String[] args) throws JMSException {  
  25.         // TODO Auto-generated method stub  
  26.         ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");  
  27.         Connection connection=factory.createConnection();  
  28.         connection.start();  
  29.           
  30.         Queue queue=new ActiveMQQueue("testQueue");  
  31.         Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  32.         //创建一个消息的生产者  
  33.         MessageProducer producer=session.createProducer(queue);  
  34.         //设置消息的DeliveryMode为Persistent  
  35.         producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
  36.         producer.send(session.createTextMessage("A persistent Message"));  
  37.         //设置消息的DeliveryMode为Non-Persistent  
  38.         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
  39.         producer.send(session.createTextMessage("A non persistent Message"));  
  40.           
  41.         System.out.println("Send messages successfully!");  
  42.     }  
  43.   
  44. }  
运行上面的程序,结果如下:


当输出“Send messages sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。
2. 接下来我们重新启动JMS Provicer,然后添加一个消费者:

[java]  view plain copy
  1. /** 
  2.  * @author Administrator 
  3.  * @description 这个程序是基于DeliveryModeSendTest基础上来做的 
  4.  * 运行上面的程序,可以得到下面的输出结果:Consumer get:A persistent Message 
  5.  * 可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。 
  6.  */  
  7. package com.wl.jms;  
  8.   
  9. import javax.jms.Connection;  
  10. import javax.jms.JMSException;  
  11. import javax.jms.Message;  
  12. import javax.jms.MessageConsumer;  
  13. import javax.jms.MessageListener;  
  14. import javax.jms.Queue;  
  15. import javax.jms.Session;  
  16. import javax.jms.TextMessage;  
  17.   
  18. import org.apache.activemq.ActiveMQConnectionFactory;  
  19. import org.apache.activemq.command.ActiveMQQueue;  
  20.   
  21. public class DeliveryModeReceiveTest {  
  22.   
  23.     /** 
  24.      * @param args 
  25.      * @throws JMSException  
  26.      */  
  27.     public static void main(String[] args) throws JMSException {  
  28.         // TODO Auto-generated method stub  
  29.         ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");  
  30.         Connection connection=factory.createConnection();  
  31.         connection.start();  
  32.           
  33.         Queue queue=new ActiveMQQueue("testQueue");  
  34.         Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  35.         //创建消息的接收者,来接受DeliveryModeSendTest中发送的消息  
  36.         MessageConsumer consumer=session.createConsumer(queue);  
  37.         consumer.setMessageListener(new MessageListener(){  
  38.   
  39.             public void onMessage(Message m) {  
  40.                 // TODO Auto-generated method stub  
  41.                 try {  
  42.                     System.out.println("Consumer get: "+((TextMessage)m).getText());  
  43.                 } catch (JMSException e) {  
  44.                     // TODO Auto-generated catch block  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.               
  49.         });  
  50.     }  
  51.   
  52. }  
运行这个接受端的程序,结果如下:


可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。
另外, 如果发送一个non persistent消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值