`

深入掌握JMS(七):DeliveryMode例子 (转)

    博客分类:
  • JMS
阅读更多

在下面的例子中,分别发送一个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消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失.

分享到:
评论
1 楼 hyneng 2011-05-05  
运行第二个例子有错
org.apache.activemq.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
	at org.apache.activemq.protobuf.InvalidProtocolBufferException.invalidEndTag(InvalidProtocolBufferException.java:52)
	at org.apache.activemq.protobuf.CodedInputStream.checkLastTagWas(CodedInputStream.java:95)
	at org.apache.activemq.protobuf.BaseMessage.mergeUnframed(BaseMessage.java:185)
	at org.apache.activemq.protobuf.BaseMessage.mergeUnframed(BaseMessage.java:213)
	at org.apache.activemq.protobuf.BaseMessage.mergeFramed(BaseMessage.java:237)
	at org.apache.activemq.store.kahadb.MessageDatabase.load(MessageDatabase.java:825)
	at org.apache.activemq.store.kahadb.KahaDBStore.loadMessage(KahaDBStore.java:955)
	at org.apache.activemq.store.kahadb.KahaDBStore$KahaDBMessageStore$5.execute(KahaDBStore.java:493)
	at org.apache.kahadb.page.Transaction.execute(Transaction.java:728)
	at org.apache.activemq.store.kahadb.KahaDBStore$KahaDBMessageStore.recoverNextMessages(KahaDBStore.java:485)
	at org.apache.activemq.store.ProxyMessageStore.recoverNextMessages(ProxyMessageStore.java:88)
	at org.apache.activemq.broker.region.cursors.QueueStorePrefetch.doFillBatch(QueueStorePrefetch.java:97)
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.fillBatch(AbstractStoreCursor.java:260)
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.reset(AbstractStoreCursor.java:108)
	at org.apache.activemq.broker.region.cursors.StoreQueueCursor.reset(StoreQueueCursor.java:157)
	at org.apache.activemq.broker.region.Queue.doPageInForDispatch(Queue.java:1712)
	at org.apache.activemq.broker.region.Queue.pageInMessages(Queue.java:1932)
	at org.apache.activemq.broker.region.Queue.iterate(Queue.java:1440)
	at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:127)
	at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
1593 [BrokerService[localhost] Task-1] ERROR org.apache.activemq.broker.region.cursors.AbstractStoreCursor - QueueStorePrefetch2569862 - Failed to fill batch
java.lang.RuntimeException: org.apache.activemq.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.fillBatch(AbstractStoreCursor.java:263)
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.reset(AbstractStoreCursor.java:108)
	at org.apache.activemq.broker.region.cursors.StoreQueueCursor.reset(StoreQueueCursor.java:157)
	at org.apache.activemq.broker.region.Queue.doPageInForDispatch(Queue.java:1712)
	at org.apache.activemq.broker.region.Queue.pageInMessages(Queue.java:1932)
	at org.apache.activemq.broker.region.Queue.iterate(Queue.java:1440)
	at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:127)
	at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
Caused by: org.apache.activemq.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
	at org.apache.activemq.protobuf.InvalidProtocolBufferException.invalidEndTag(InvalidProtocolBufferException.java:52)
	at org.apache.activemq.protobuf.CodedInputStream.checkLastTagWas(CodedInputStream.java:95)
	at org.apache.activemq.protobuf.BaseMessage.mergeUnframed(BaseMessage.java:185)
	at org.apache.activemq.protobuf.BaseMessage.mergeUnframed(BaseMessage.java:213)
	at org.apache.activemq.protobuf.BaseMessage.mergeFramed(BaseMessage.java:237)
	at org.apache.activemq.store.kahadb.MessageDatabase.load(MessageDatabase.java:825)
	at org.apache.activemq.store.kahadb.KahaDBStore.loadMessage(KahaDBStore.java:955)
	at org.apache.activemq.store.kahadb.KahaDBStore$KahaDBMessageStore$5.execute(KahaDBStore.java:493)
	at org.apache.kahadb.page.Transaction.execute(Transaction.java:728)
	at org.apache.activemq.store.kahadb.KahaDBStore$KahaDBMessageStore.recoverNextMessages(KahaDBStore.java:485)
	at org.apache.activemq.store.ProxyMessageStore.recoverNextMessages(ProxyMessageStore.java:88)
	at org.apache.activemq.broker.region.cursors.QueueStorePrefetch.doFillBatch(QueueStorePrefetch.java:97)
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.fillBatch(AbstractStoreCursor.java:260)
	... 10 more
1593 [BrokerService[localhost] Task-1] ERROR org.apache.activemq.broker.region.Queue - Failed to page in more queue messages 
java.lang.RuntimeException: java.lang.RuntimeException: org.apache.activemq.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.reset(AbstractStoreCursor.java:111)
	at org.apache.activemq.broker.region.cursors.StoreQueueCursor.reset(StoreQueueCursor.java:157)
	at org.apache.activemq.broker.region.Queue.doPageInForDispatch(Queue.java:1712)
	at org.apache.activemq.broker.region.Queue.pageInMessages(Queue.java:1932)
	at org.apache.activemq.broker.region.Queue.iterate(Queue.java:1440)
	at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:127)
	at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.RuntimeException: org.apache.activemq.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.fillBatch(AbstractStoreCursor.java:263)
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.reset(AbstractStoreCursor.java:108)
	... 9 more
Caused by: org.apache.activemq.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
	at org.apache.activemq.protobuf.InvalidProtocolBufferException.invalidEndTag(InvalidProtocolBufferException.java:52)
	at org.apache.activemq.protobuf.CodedInputStream.checkLastTagWas(CodedInputStream.java:95)
	at org.apache.activemq.protobuf.BaseMessage.mergeUnframed(BaseMessage.java:185)
	at org.apache.activemq.protobuf.BaseMessage.mergeUnframed(BaseMessage.java:213)
	at org.apache.activemq.protobuf.BaseMessage.mergeFramed(BaseMessage.java:237)
	at org.apache.activemq.store.kahadb.MessageDatabase.load(MessageDatabase.java:825)
	at org.apache.activemq.store.kahadb.KahaDBStore.loadMessage(KahaDBStore.java:955)
	at org.apache.activemq.store.kahadb.KahaDBStore$KahaDBMessageStore$5.execute(KahaDBStore.java:493)
	at org.apache.kahadb.page.Transaction.execute(Transaction.java:728)
	at org.apache.activemq.store.kahadb.KahaDBStore$KahaDBMessageStore.recoverNextMessages(KahaDBStore.java:485)
	at org.apache.activemq.store.ProxyMessageStore.recoverNextMessages(ProxyMessageStore.java:88)
	at org.apache.activemq.broker.region.cursors.QueueStorePrefetch.doFillBatch(QueueStorePrefetch.java:97)
	at org.apache.activemq.broker.region.cursors.AbstractStoreCursor.fillBatch(AbstractStoreCursor.java:260)
	... 10 more

相关推荐

    javax.jms.jar

    javax.jms.DeliveryMode.class javax.jms.Connection.class javax.jms.Session.class javax.jms.ConnectionMetaData.class javax.jms.ExceptionListener.class javax.jms.ServerSessionPool.class javax.jms....

    javax.jms.jar下载

    javax.jms.DeliveryMode.class javax.jms.Destination.class javax.jms.ExceptionListener.class javax.jms.IllegalStateException.class javax.jms.InvalidClientIDException.class javax.jms....

    基于Java通讯开发jms源代码 (jms通讯开发源码)

    基于Java通讯开发jms源代码 (jms通讯开发源码) java,net,socket,通讯开发,jms /* * @(#)Message.java 1.60 02/04/09 * * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved. * * SUN ...

    ActiveMQ实例

    import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; ...

    java面试题第四部分:中间件

    - **消息投递模式持久化**:`deliveryMode`设置为`2`。 - **消息到达持久化交换器**。 - **消息到达持久化队列**。 14. **持久化缺点**:降低服务器吞吐量,可使用SSD硬盘优化。 15. **广播模式** - **Direct*...

    0924分布式消息通信-ActiveMQ1

    本文将深入探讨ActiveMQ 的一些核心特性,包括消息发送策略、持久化、消费者行为、消息确认机制以及Spring 集成。 1. **消息发送策略**: - **持久化消息**:默认情况下,生产者发送的消息是持久化的,即使在消息...

    解析ActiveMQ的使用说明总结

    在本文中,我们将深入探讨ActiveMQ的一些关键知识点,包括消息过滤、错误处理、发送模式、负载均衡以及配置选项。 1. **消息过滤(Message Filtering)** ActiveMQ支持消息选择器,允许你指定接收特定类型的消息。...

    testActiveMQ.rar

    producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT); // Create the Thread Id String string threadIdStr = Long::toString(Thread::currentThread()->getId()); // Create a messages string text =...

    消息中间件面试专题及答案

    - **异步方式**:发送方无需等待接收方响应,提高系统效率,Java中的JMS支持点对点和发布/订阅两种异步消息模式。 6. **ActiveMQ调优** - 使用非持久化消息以减少磁盘I/O。 - 使用事务确保消息批量发送成功,...

    rabbitmq笔记及面试要点

    【标题】:深入理解RabbitMQ及其面试关键点 【正文】: RabbitMQ作为一款广泛应用的消息...理解并掌握这些知识点,不仅可以加深对RabbitMQ的理解,也有助于在面试中展现出对分布式系统架构和消息中间件的深入认识。

    ActiveMQ持久化机制代码实例

    在本文中,我们将深入探讨ActiveMQ的持久化机制,并通过代码实例来展示其工作原理。 ActiveMQ的持久化机制是为了确保在系统崩溃或重启后,未被消费的消息仍然能够被恢复并继续处理。这主要涉及到两个关键概念:非...

    wireless.zip

    >第二种情况是持久化磁盘,具体是将queue的持久化标识durable设置为true,则代表是一个持久的队列,发送消息的时候讲deliveryMode=2 > >第三是主要消费者采取的是自动确认消息模式?可以改成手动确定的模式,但是...

    c# rabbitmq 简单收发消息的示例代码

    在本文中,我们将深入探讨如何使用C#与RabbitMQ进行简单的消息收发。RabbitMQ是一个开源的消息代理和队列服务器,它允许应用程序之间通过消息进行异步通信。C#是.NET框架的主要编程语言,提供了丰富的库来与RabbitMQ...

    RabbitMQ .NET消息队列使用详解

    在本文中,我们将深入探讨如何在.NET环境中使用RabbitMQ进行消息队列操作。首先,我们需要在RabbitMQ官方网站下载适用于Windows 64位系统的安装包,包括otp_win64_19.0.exe和rabbitmq-server-3.6.3.exe,并完成安装...

Global site tag (gtag.js) - Google Analytics