惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
爱范儿
爱范儿
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
IT之家
IT之家
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
G
Google Developers Blog
V
V2EX
雷峰网
雷峰网
美团技术团队
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
V
Visual Studio Blog
J
Java Code Geeks

博客园 - wolfman

正则表达式Pattern 字符串分割成一维数组、二维数组,一维数组与二维数组之间的转换 ActiveMQ在C#中的应用 深入掌握JMS(十二):MDB 深入掌握JMS(十一):TemporaryQueue和TemporaryTopic 深入掌握JMS(十):JMSCorrelationID与Selector 深入掌握JMS(九):Selector 深入掌握JMS(八):JMSReplyTo 深入掌握JMS(六):消息头 深入掌握JMS(四):实战Queue 深入掌握JMS(五):实战Topic 深入掌握JMS(三):MessageListener 深入掌握JMS(二):一个JMS例子 深入掌握JMS(一):JSM基础 oracle 分区表(转) Application,Session,Cookie,ViewState,Cache的区别(转) 网络编程基础 VS2008+Oracle92 网站发布注意问题 Windows Service开发应用
深入掌握JMS(七):DeliveryMode例子
wolfman · 2010-06-12 · via 博客园 - wolfman

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

            import javax.jms.Connection;
            import javax.jms.DeliveryMode;
            import javax.jms.MessageProducer;
            import javax.jms.Queue;
            import javax.jms.Session;
            import org.apache.activemq.ActiveMQConnectionFactory;
            import org.apache.activemq.command.ActiveMQQueue;

            public class DeliveryModeSendTest {

                public static void main(String[] args) throws Exception {
                    ActiveMQConnectionFactory factory = new
            ActiveMQConnectionFactory("vm://localhost");
                    Connection connection = factory.createConnection();
                    connection.start();
                    Queue queue = new ActiveMQQueue("testQueue");
                    Session session = connection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);
                    MessageProducer producer = session.createProducer(queue);
                    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                    producer.send(session.createTextMessage("A persistent
            Message"));
                    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                    producer.send(session.createTextMessage("A non persistent
            Message"));
                    System.out.println("Send messages sucessfully!");
                }
            }

                运行上面的程序,当输出“Send messages
            sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。

                接下来我们重新启动JMS Provicer,然后添加一个消费者:

            import javax.jms.Connection;
            import javax.jms.JMSException;
            import javax.jms.Message;
            import javax.jms.MessageConsumer;
            import javax.jms.MessageListener;
            import javax.jms.Queue;
            import javax.jms.Session;
            import javax.jms.TextMessage;
            import org.apache.activemq.ActiveMQConnectionFactory;
            import org.apache.activemq.command.ActiveMQQueue;

            public class DeliveryModeReceiveTest {

                public static void main(String[] args) throws Exception {
                    ActiveMQConnectionFactory factory = new
            ActiveMQConnectionFactory("vm://localhost");
                    Connection connection = factory.createConnection();
                    connection.start();
                    Queue queue = new ActiveMQQueue("testQueue");
                    Session session = connection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);
                    MessageConsumer comsumer = session.createConsumer(queue);
                    comsumer.setMessageListener(new MessageListener(){
                        public void onMessage(Message m) {
                            try {
                                System.out.println("Consumer get " +
            ((TextMessage)m).getText());
                            } catch (JMSException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }

            运行上面的程序,可以得到下面的输出结果:

            Consumer get A persistent Message

            可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。

            另外, 如果发送一个non persistent消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失.