`

深入掌握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....

    JMS学习手册

    JMS(Java Message Service)是一种Java企业版(Java EE)技术...从这些知识点来看,JMS学习手册提供了一个全面的框架,帮助开发者了解和掌握JMS的关键概念、架构和编程模型,是学习和应用Java消息服务技术的宝贵资源。

    基于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 ...

    JMS教程+activemq以及activemq和tomcat的整合

    **JMS教程** Java Message Service(JMS)是Java平台中用于企业级应用间进行异步消息传递的标准接口。它允许应用程序创建、发送、接收和读取消息,从而实现应用程序间的解耦和通信。JMS提供了两种主要的消息模型:...

    ActiveMQ in Action

    在深入探讨ActiveMQ的具体应用之前,我们需要先了解Java消息服务(Java Message Service,简称JMS)的基础概念及其核心构件。JMS是一种Java平台中的消息中间件标准,它允许应用程序组件创建、发送、接收和读取信息,...

    ActiveMQ代码示例

    **ActiveMQ 深度解析与代码实践** ...理解并熟练掌握 ActiveMQ 的使用,对于构建高可用、高性能的分布式系统至关重要。通过实践和学习提供的代码示例,可以深入理解 ActiveMQ 的核心特性和应用场景。

    rabbitmq-c rabbitmq amqp c++接口库

    channel.basicPublish("my_exchange", "", amqp::deliveryMode::persistent, "Hello, RabbitMQ!"); auto message = channel.basicConsume("my_queue"); while (true) { auto msg = message.next(); std::cout ...

    activeMQ笔记

    了解和掌握这些消息正文格式对于正确使用ActiveMQ和其他基于JMS的应用至关重要。 通过以上的知识点梳理,我们可以看到ActiveMQ在消息传递方面提供了丰富的特性和灵活性,适用于各种不同的应用场景。无论是需要高度...

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

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

    0924分布式消息通信-ActiveMQ1

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

    ios应用源码之相册拾取器 2018128

    本文将深入探讨“ios应用源码之相册拾取器 2018128”这个主题,包括源码解析、关键API的使用以及实现流程。 首先,iOS提供了一个名为Photos Framework的库,它是处理照片和视频的核心组件。在创建相册拾取器时,...

    Apache ActiveMQ学习笔记【原创:mq的方式有两种:点到点和发布/订阅】

    具体实现中可能还需要定义消息创建器 (`MessageCreator`) 和配置消息属性(例如 `DeliveryMode`、`Session` 等),以便实际发送消息。 #### 四、总结 通过以上介绍,我们可以看到 ActiveMQ 提供了两种主要的消息...

    各种消息队列技术对比分享

    对于Java开发者来说,深入理解并掌握消息队列的工作原理和技术特性,对于构建高效稳定的分布式应用至关重要。无论是从系统设计的角度还是从实际应用的角度来看,合理利用消息队列技术都是十分必要的。

    ios-PhotoKit 仿微信相册图片选择.zip

    接下来,我们将深入探讨`PhotoKit`的相关知识点,以及如何构建这样的用户界面。 `PhotoKit`是iOS、iPadOS和watchOS系统功能的一部分,它为应用程序提供了访问和管理用户照片库的能力。通过`PhotoKit`,开发者可以...

    解析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 =...

Global site tag (gtag.js) - Google Analytics