activemq学习 二

4.2.6.3 消息选择器JMS 提供了一种机制,使用它,消息服务可根据消息选择器中的标准来执行消息过滤。生产者可在消息中放入应用程序特有的属性,而消费者可使用基于这些属性的选择 标准来表明对消息是否感兴趣。这就简化了客户端的工作,并避免了向不需要这些消息的消费者传送消息的开销。然而,它也使得处理选择标准的消息服务增加了一 些额外开销。消息选择器是用于 MessageConsumer 的过滤器,可以用来过滤传入消息的属性和消息头部分(但不过滤消息体),并确定是否将实际消费该消息。按照JMS 文档的说法,消息选择器是一些字符串,它们基于某种语法,而这种语法是SQL-92 的子集。可以将消息选择器作为MessageConsumer 创建的一部分。Java 客户端:例如:public final String SELECTOR = “JMSType = ‘TOPIC_PUBLISHER’”;该选择器检查了传入消息的JMSType 属性,并确定了这个属性的值是否等于TOPIC_PUBLISHER。如果相等,则消息被消费;如果不相等,那么消息会被忽略。4.2.7 MessageJMS 程序的最终目的是生产和消费的消息能被其他程序使用,JMS 的 Message 是一个既简单又不乏灵活性的基本格式,允许创建不同平台上符合非JMS 程序格式的消息。Message 由以下几部分组成:消息头,属性和消息体。Java 客户端:ActiveMQSession 方法:BlobMessage createBlobMessage(File file)BlobMessage createBlobMessage(InputStream in)BlobMessage createBlobMessage(URL url)BlobMessage createBlobMessage(URL url, boolean deletedByBroker)BytesMessage createBytesMessage()MapMessage createMapMessage()Message createMessage()ObjectMessage createObjectMessage()ObjectMessage createObjectMessage(Serializable object)TextMessage createTextMessage()TextMessage createTextMessage(String text)例如:下例演示创建并发送一个 TextMessage 到一个队列:TextMessage message = queueSession.createTextMessage();message.setText(msg_text); // msg_text is a StringqueueSender.send(message);下例演示接收消息并转换为合适的消息类型:Message m = queueReceiver.receive();if (m instanceof TextMessage) {TextMessage message = (TextMessage) m;System.out.println("Reading message: " + message.getText());} else {// Handle error}C++客户端:函数原型:cms::Message* ActiveMQSession::createMessage(void)throw ( cms::CMSException )cms::BytesMessage* ActiveMQSession::createBytesMessage(void)throw ( cms::CMSException )cms::BytesMessage* ActiveMQSession::createBytesMessage(const unsigned char* bytes,unsigned long long bytesSize )throw ( cms::CMSException )cms::TextMessage* ActiveMQSession::createTextMessage(void)throw ( cms::CMSException )cms::TextMessage* ActiveMQSession::createTextMessage( const std::string& text )throw ( cms::CMSException )cms::MapMessage* ActiveMQSession::createMapMessage(void)throw ( cms::CMSException )例如:下例演示创建并发送一个 TextMessage 到一个队列:TextMessage* message = session->createTextMessage( text ); // text is a stringproducer->send( message );delete message;下例演示接收消息:Message *message = consumer->receive();const TextMessage* textMessage = dynamic_cast< const TextMessage* >( message );string text = textMessage->getText();printf( "Received: %s/n", text.c_str() );delete message;4.3 可靠性机制发送消息最可靠的方法就是在事务中发送持久性的消息,ActiveMQ 默认发送持久性消息。结束事务有两种方法:提交或者回滚。当一个事务提交,消息被处理。如果事务中有一个步骤失败,事务就回滚,这个事务中的已经执行的动 作将被撤销。接收消息最可靠的方法就是在事务中接收信息,不管是从PTP 模式的非临时队列接收消息还是从Pub/Sub 模式持久订阅中接收消息。对于其他程序,低可靠性可以降低开销和提高性能,例如发送消息时可以更改消息的优先级或者指定消息的过期时间。消息传送的可靠性 越高,需要的开销和带宽就越多。性能和可靠性之间的折衷是设计时要重点考虑的一个方面。可以选择生成和使用非持久性消息来获得最佳性能。另一方面,也可以 通过生成和使用持久性消息并使用事务会话来获得最佳可靠性。在这两种极端之间有许多选择,这取决于应用程序的要求。4.3.1 基本可靠性机制4.3.1.1 控制消息的签收(Acknowledgment)客户端成功接收一条消息的标志是这条消息被签收。成功接收一条消息一般包括如下三个阶段:1.客户端接收 消息;2.客户端处理消息;3.消息被签收。签收可以由ActiveMQ 发起,也可以由客户端发起,取决于Session 签收模式的设置。在带事务的 Session 中,签收自动发生在事务提交时。如果事务回滚,所有已经接收的消息将会被再次传送。在不带事务的Session 中,一条消息何时和如何被签收取决于Session 的设置。1.Session.AUTO_ACKNOWLEDGE当客户端从 receive 或onMessage 成功返回时,Session 自动签收客户端的这条消息的收条。在AUTO_ACKNOWLEDGE 的Session 中,同步接收receive 是上述三个阶段的一个例外,在这种情况下,收条和签收紧随在处理消息之后发生。2.Session.CLIENT_ACKNOWLEDGE客户端通过调用 消息的 acknowledge 方法签收消息。在这种情况下,签收发生在Session 层面:签收一个已消费的消息会自动地签收这个Session 所有已消费消息的收条。3.Session.DUPS_OK_ACKNOWLEDGE此选项指示 Session 不必确保对传送消息的签收。它可能引起消息的重复,但是降低了Session 的开销,所以只有客户端能容忍重复的消息,才可使用(如果ActiveMQ 再次传送同一消息,那么消息头中的JMSRedelivered 将被设置为true)。Java 客户端:签收模式分别为:1. Session.AUTO_ACKNOWLEDGE2. Session.CLIENT_ACKNOWLEDGE3. Session.DUPS_OK_ACKNOWLEDGEActiveMQConnection 方法:Session createSession(boolean transacted, int acknowledgeMode);例如:Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);C++客户端:签收模式分别为:1. Session::AUTO_ACKNOWLEDGE2. Session::CLIENT_ACKNOWLEDGE3. Session::DUPS_OK_ACKNOWLEDGE4. Session::SESSION_TRANSACTED函数原型:cms::Session* ActiveMQConnection::createSession(cms::Session::AcknowledgeMode ackMode )throw ( cms::CMSException )例如:Session* session = connection->createSession( Session::AUTO_ACKNOWLEDGE );对队列来说,如果当一个Session 终止时它接收了消息但是没有签收,那么ActiveMQ 将保留这些消息并将再次传送给下一个进入队列的消费者。对主题来说,如果持久订阅用户终止时,它已消费未签收的消息也将被保留,直到再次传送给这个用户。 对于非持久订阅,AtiveMQ 在用户Session 关闭时将删除这些消息。如果使用队列和持久订阅,并且Session 没有使用事务,那么可以使用Session 的recover 方法停止Session,再次启动后将收到它第一条没有签收的消息,事实上,重启后Session 一系列消息的传送都是以上一次最后一条已签收消息的下一条为起点。如果这时有消息过期或者高优先级的消息到来,那么这时消息的传送将会和最初的有所不同。 对于非持久订阅用户,重启后,ActiveMQ 有可能删除所有没有签收的消息。4.3.1.2 指定消息传送模式ActiveMQ 支持两种消息传送模式:PERSISTENT 和NON_PERSISTENT 两种。1.PERSISTENT(持久性消息)这是 ActiveMQ 的默认传送模式,此模式保证这些消息只被传送一次和成功使用一次。对于这些消息,可靠性是优先考虑的因素。可靠性的另一个重要方面是确保持久性消息传送至 目标后,消息服务在向消费者传送它们之前不会丢失这些消息。这意味着在持久性消息传送至目标时,消息服务将其放入持久性数据存储。如果消息服务由于某种原 因导致失败,它可以恢复此消息并将此消息传送至相应的消费者。虽然这样增加了消息传送的开销,但却增加了可靠性。2.NON_PERSISTENT(非持 久性消息)保证这些消息最多被传送一次。对于这些消息,可靠性并非主要的考虑因素。此模式并不要求持久性的数据存储,也不保证消息服务由于某种原因导致失 败后消息不会丢失。有两种方法指定传送模式:1.使用setDeliveryMode 方法,这样所有的消息都采用此传送模式;2.使用send 方法为每一条消息设置传送模式;Java 客户端:传送模式分别为:1. DeliveryMode.PERSISTENT2. DeliveryMode.NON_PERSISTENTActiveMQMessageProducer 方法:void setDeliveryMode(int newDeliveryMode);或者void send(Destination destination, Message message, int deliveryMode, int priority,long timeToLive);void send(Message message, int deliveryMode, int priority, long timeToLive);其中 deliveryMode 为传送模式,priority 为消息优先级,timeToLive 为消息过期时间。例如:producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);C++客户端: 传送模式分别为:1. DeliveryMode::PERSISTANT2. DeliveryMode::NON_PERSISTANT函数原型:void setDeliveryMode( int mode );或者void ActiveMQProducer::send( cms::Message* message, int deliveryMode,int priority,long long timeToLive )throw ( cms::CMSException );void ActiveMQProducer::send( const cms::Destination* destination,cms::Message* message, int deliveryMode,int priority, long long timeToLive)throw ( cms::CMSException );例如:producer->setDeliveryMode( DeliveryMode::NON_PERSISTANT );如果不指定传送模式,那么默认是持久性消息。如果容忍消息丢失,那么使用非持久性消息可以改善性能和减少存储的开销。4.3.1.3 设置消息优先级通常,可以确保将单个会话向目标发送的所有消息按其发送顺序传送至消费者。然而,如果为这些消息分配了不同的优先级,消息传送系统将首先尝 试传送优先级较高的消息。有两种方法设置消息的优先级:1.使用setDeliveryMode 方法,这样所有的消息都采用此传送模式;2.使用send 方法为每一条消息设置传送模式;Java 客户端:ActiveMQMessageProducer 方法:void setPriority(int newDefaultPriority);或者void send(Destination destination, Message message, int deliveryMode, int priority,long timeToLive);void send(Message message, int deliveryMode, int priority, long timeToLive);其中 deliveryMode 为传送模式,priority 为消息优先级,timeToLive 为消息过期时间。例如:producer.setPriority(4);C++客户端:函数原型:void setPriority( int priority );或者void ActiveMQProducer::send( cms::Message* message, int deliveryMode,int priority,long long timeToLive )throw ( cms::CMSException );void ActiveMQProducer::send( const cms::Destination* destination,cms::Message* message, int deliveryMode,int priority, long long timeToLive)throw ( cms::CMSException );例如:producer-> setPriority(4);消息优先级从0-9 十个级别,0-4 是普通消息,5-9 是加急消息。如果不指定优先级,则默认为4。JMS 不要求严格按照这十个优先级发送消息,但必须保证加急消息要先于普通消息到达。4.3.1.4 允许消息过期默认情况下,消息永不会过期。如果消息在特定周期内失去意义,那么可以设置过期时间。有两种方法设置消息的过期时间,时间单位为毫秒:1.使 用setTimeToLive 方法为所有的消息设置过期时间;2.使用send 方法为每一条消息设置过期时间;Java 客户端:ActiveMQMessageProducer 方法:void setTimeToLive(long timeToLive);或者void send(Destination destination, Message message, int deliveryMode, int priority,long timeToLive);void send(Message message, int deliveryMode, int priority, long timeToLive);其中 deliveryMode 为传送模式,priority 为消息优先级,timeToLive 为消息过期时间。例如:producer.setTimeToLive(1000);C++客户端:函数原型:void setTimeToLive( long long time );或者void ActiveMQProducer::send( cms::Message* message, int deliveryMode,int priority,long long timeToLive )throw ( cms::CMSException );void ActiveMQProducer::send( const cms::Destination* destination,cms::Message* message, int deliveryMode,int priority, long long timeToLive)throw ( cms::CMSException );例如:Producer->setTimeToLive(1000);消息过期时间,send 方法中的timeToLive 值加上发送时刻的GMT 时间值。如果timeToLive 值等于零,则JMSExpiration 被设为零,表示该消息永不过期。如果发送后,在消息过期时间之后消息还没有被发送到目的地,则该消息被清除。4.3.1.5 创建临时目标ActiveMQ 通过createTemporaryQueue 和createTemporaryTopic 创建临时目标,这些目标持续到创建它的Connection 关闭。只有创建临时目标的Connection 所创建的客户端才可以从临时目标中接收消息,但是任何的生产者都可以向临时目标中发送消息。如果关闭了创建此目标的Connection,那么临时目标被 关闭,内容也将消失。Java 客户端:ActiveMQSession 方法:TemporaryQueue createTemporaryQueue();TemporaryTopic createTemporaryTopic();C++客户端:函数原型:cms::TemporaryQueue* ActiveMQSession::createTemporaryQueue(void)throw ( cms::CMSException );cms::TemporaryTopic* ActiveMQSession::createTemporaryTopic(void)throw ( cms::CMSException );某些客户端需要一个目标来接收对发送至其他客户端的消息的回复。这时可以使用临时目标。Message 的属性之一是JMSReplyTo 属性,这个属性就是用于这个目的的。可以创建一个临时的Destination,并把它放入Message 的JMSReplyTo 属性中,收到该消息的消费者可以用它来响应生产者。Java 客户端:如下所示代码段,将创建临时的Destination,并将它放置在TextMessage 的JMSReplyTo 属性中:// Create a temporary queue for replies...Destination tempQueue = session.createTemporaryQueue();// Set ReplyTo to temporary queue...msg.setJMSReplyTo(tempQueue);消费者接收这条消息时,会从JMSReplyTo 字段中提取临时Destination,并且会通过应用程序构造一个MessageProducer,以便将响应消息发送回生产者。这展示了如何使用 JMS Message 的属性,并显示了私有的临时Destination 的有用之处。它还展示了客户端可以既是消息的生产者,又可以是消息的消费者。// Get the temporary queue from the JMSReplyTo// property of the message...Destination tempQueue = msg.getJMSReplyTo();...// create a Sender for the temporary queueMessageProducer Sender = session. createProducer(tempQueue);TextMessage msg = session.createTextMessage();msg.setText(REPLYTO_TEXT);...// Send the message to the temporary queue...sender.send(msg);4.3.2 高级可靠性机制4.3.2.1 创建持久订阅通过为发布者设置 PERSISTENT 传送模式,为订阅者时使用持久订阅,这样可以保证Pub/Sub 程序接收所有发布的消息。消息订阅分为非持久订阅(non-durable subscription)和持久订阅(durable subscription),非持久订阅只有当客户端处于激活状态,也就是和ActiveMQ 保持连接状态才能收到发送到某个主题的消息,而当客户端处于离线状态,这个时间段发到主题的消息将会丢失,永远不会收到。持久订阅时,客户端向 ActiveMQ 注册一个识别自己身份的ID,当这个客户端处于离线时,ActiveMQ 会为这个ID 保存所有发送到主题的消息,当客户端再次连接到ActiveMQ 时,会根据自己的ID 得到所有当自己处于离线时发送到主题的消息。持久订阅会增加开销,同一时间在持久订阅中只有一个激活的用户。建立持久订阅的步骤:1. 为连接设置一个客户 ID;2. 为订阅的主题指定一个订阅名称;上述组合必须唯一。4.3.2.1.1 创建持久订阅Java 客户端:ActiveMQConnection 方法:void setClientID(String newClientID)和ActiveMQSession 方法:TopicSubscriber createDurableSubscriber(Topic topic, String name)TopicSubscriber createDurableSubscriber(Topic topic, String name, StringmessageSelector, boolean noLocal)其中 messageSelector 为消息选择器;noLocal 标志默认为false,当设置为true时限制消费者只能接收和自己相同的连接(Connection)所发布的消息,此标志只适用于主题,不适用于队 列;name 标识订阅主题所对应的订阅名称,持久订阅时需要设置此参数。C++客户端:函数原型:virtual void setClientId( const std::string& clientId );和cms::MessageConsumer* ActiveMQSession::createDurableConsumer(const cms::Topic* destination,const std::string& name,const std::string& selector,bool noLocal )throw ( cms::CMSException )4.3.2.1.2 删除持久订阅Java 客户端:ActiveMQSession 方法:void unsubscribe(String name);4.3.2.2 使用本地事务在事务中生成或使用消息时,ActiveMQ 跟踪各个发送和接收过程,并在客户端发出提交事务的调用时完成这些操作。如果事务中特定的发送或接收操作失败,则出现异常。客户端代码通过忽略异常、重试 操作或回滚整个事务来处理异常。在事务提交时,将完成所有成功的操作。在事务进行回滚时,将取消所有成功的操作。本地事务的范围始终为一个会话。也就是 说,可以将单个会话的上下文中执行的一个或多个生产者或消费者操作组成一个本地事务。不但单个会话可以访问 Queue 或 Topic (任一类型的 Destination ),而且单个会话实例可以用来操纵一个或多个队列以及一个或多个主题,一切都在单个事务中进行。这意味着单个会话可以(例如)创建队列和主题中的生产者, 然后使用单个事务来同时发送队列和主题中的消息。因为单个事务跨越两个目标,所以,要么队列和主题的消息都得到发送,要么都未得到发送。类似地,单个事务 可以用来接收队列中的消息并将消息发送到主题上,反过来也可以。由于事务的范围只能为单个的会话,因此不存在既包括消息生成又包括消息使用的端对端事务。 (换句话说,至目标的消息传送和随后进行的至客户端的消息传送不能放在同一个事务中。)4.3.2.2.1 使用事务Java 客户端:ActiveMQConnection 方法:Session createSession(boolean transacted, int acknowledgeMode);其中 transacted 为使用事务标识,acknowledgeMode 为签收模式。例如:Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);C++客户端:函数原型:cms::Session* ActiveMQConnection::createSession(cms::Session::AcknowledgeMode ackMode );其中 AcknowledgeMode ackMode 需指定为SESSION_TRANSACTED。例如:Session* session = connection->createSession( Session:: SESSION_TRANSACTED );4.3.2.2.2 提交Java 客户端:ActiveMQSession 方法:void commit();例如:try {producer.send(consumer.receive());session.commit();}catch (JMSException ex) {session.rollback();}C++客户端:函数原型:void ActiveMQSession::commit(void) throw ( cms::CMSException )4.3.2.2.3 回滚Java 客户端:ActiveMQSession 方法:void rollback();C++客户端:函数原型:void ActiveMQSession::rollback(void) throw ( cms::CMSException )4.4 高级特征4.4.1 异步发送消息ActiveMQ 支持生产者以同步或异步模式发送消息。使用不同的模式对send 方法的反应时间有巨大的影响,反映时间是衡量ActiveMQ 吞吐量的重要因素,使用异步发送可以提高系统的性能。在默认大多数情况下,AcitveMQ 是以异步模式发送消息。例外的情况:在没有使用事务的情况下,生产者以PERSISTENT 传送模式发送消息。在这种情况下,send 方法都是同步的,并且一直阻塞直到ActiveMQ 发回确认消息:消息已经存储在持久性数据存储中。这种确认机制保证消息不会丢失,但会造成生产者阻塞从而影响反应时间。高性能的程序一般都能容忍在故障情 况下丢失少量数据。如果编写这样的程序,可以通过使用异步发送来提高吞吐量(甚至在使用PERSISTENT 传送模式的情况下)。Java 客户端:使用 Connection URI 配置异步发送:cf = new ActiveMQConnectionFactory("tcp://locahost:61616?jms.useAsyncSend=true"); 在 ConnectionFactory 层面配置异步发送: ((ActiveMQConnectionFactory)connectionFactory).setUseAsyncSend(true);在 Connection 层面配置异步发送,此层面的设置将覆盖ConnectionFactory 层面的设置:((ActiveMQConnection)connection).setUseAsyncSend(true);4.4.2 消费者特色4.4.2.1 消费者异步分派在 ActiveMQ4 中,支持ActiveMQ 以同步或异步模式向消费者分派消息。这样的意义:可以以异步模式向处理消息慢的消费者分配消息;以同步模式向处理消息快的消费者分配消息。 ActiveMQ 默认以同步模式分派消息,这样的设置可以提高性能。但是对于处理消息慢的消费者,需要以异步模式分派。Java 客户端:在 ConnectionFactory 层面配置同步分派: ((ActiveMQConnectionFactory)connectionFactory).setDispatchAsync(false);在 Connection 层面配置同步分派,此层面的设置将覆盖ConnectionFactory 层面的设置:((ActiveMQConnection)connection).setDispatchAsync(false);在消费者层面以 Destination URI 配置同步分派,此层面的设置将覆盖ConnectionFactory 和Connection 层面的设置:queue = new ActiveMQQueue("TEST.QUEUE?consumer.dispatchAsync=false");consumer = session.createConsumer(queue);4.4.2.2 消费者优先级在 ActveMQ 分布式环境中,在有消费者存在的情况下,如果更希望ActveMQ 发送消息给消费者而不是其他的ActveMQ 到ActveMQ 的传送,可以如下设置:Java 客户端:queue = new ActiveMQQueue("TEST.QUEUE?consumer.prority=10");consumer = session.createConsumer(queue);4.4.2.3 独占的消费者ActiveMQ 维护队列消息的顺序并顺序把消息分派给消费者。但是如果建立了多个Session 和MessageConsumer,那么同一时刻多个线程同时从一个队列中接收消息时就并不能保证处理时有序。有时候有序处理消息是非常重要的。 ActiveMQ4 支持独占的消费。ActiveMQ 挑选一个MessageConsumer,并把一个队列中所有消息按顺序分派给它。如果消费者发生故障,那么ActiveMQ 将自动故障转移并选择另一个消费者。可以如下设置:Java 客户端:queue = new ActiveMQQueue("TEST.QUEUE?consumer.exclusive=true");consumer = session.createConsumer(queue);4.4.2.4 再次传送策略在以下三种情况中,消息会被再次传送给消费者:1.在使用事务的Session 中,调用rollback()方法;2.在使用事务的Session 中,调用commit()方法之前就关闭了Session;3.在Session 中使用CLIENT_ACKNOWLEDGE 签收模式,并且调用了recover()方法。可以通过设置 ActiveMQConnectionFactory 和ActiveMQConnection 来定制想要的再次传送策略。属性 默认值描述collisionAvoidanceFactor0.15 The percentage of range ofcollision avoidance if enabledmaximumRedeliveries 6 Sets the maximum number of timesa message will be redelivered beforeit is considered a poisoned pill andreturned to the broker so it can go toa Dead Letter QueueinitialRedeliveryDelay1000L The initial redelivery delay inmillisecondsuseCollisionAvoidancefalse Should the redelivery policy usecollision avoidanceuseExponentialBackOfffalse Should exponential back-off beused (i.e. to exponentially increasethe timeout)backOffMultiplier 5 The back-off multiplier4.4.3 目标特色4.4.3.1 复合目标在 1.1 版本之后,ActiveMQ 支持混合目标技术。它允许在一个JMS 目标中使用一组JMS 目标。例如可以利用混合目标在同一操作中用向 12 个队列发送同一条消息或者在同一操作中向一个主题和一个队列发送同一条消息。在混合目标中,通过“,”来分隔不同的目标。Java 客户端:例如:// send to 3 queues as one logical operationQueue queue = new ActiveMQQueue("FOO.A,FOO.B,FOO.C");producer.send(queue, someMessage);如果在一个目标中混合不同类别的目标,可以通过使用“queue://”和“topic://”前缀来识别不同的目标。例如: // send to queues and topic one logical operationQueue queue = new ActiveMQQueue("FOO.A,topic://NOTIFY.FOO.A");producer.send(queue, someMessage);4.4.3.2 目标选项属性 默认值描述consumer.prefetchSize variableThe number of message theconsumer will prefetch.consumer.maximumPendingMessageLimit0 Use to control if messagesare dropped if a slow consumersituation exists.consumer.noLocal falseSame as the noLocal flag ona Topic consumer. Exposed hereso that it can be used with aqueue.consumer.dispatchAsync falseShould the broker dispatchmessages asynchronously to theconsumer.consumer.retroactive falseIs this a RetroactiveConsumer.consumer.selector null JMS Selector used with theconsumer.consumer.exclusive falseIs this an ExclusiveConsumer.consumer.priority 0 Allows you to configure aConsumer Priority.Java 客户端:例如:queue = new ActiveMQQueue("TEST.QUEUE?consumer.dispatchAsync=false&consumer.prefetchSize=10");consumer = session.createConsumer(queue);4.4.4 消息预取ActiveMQ 的目标之一就是高性能的数据传送,所以ActiveMQ 使用“预取限制”来控制有多少消息能及时的传送给任何地方的消费者。一旦预取数量达到限制,那么就不会有消息被分派给这个消费者直到它发回签收消息(用来 标识所有的消息已经被处理)。可以为每个消费者指定消息预取。如果有大量的消息并且希望更高的性能,那么可以为这个消费者增大预取值。如果有少量的消息并 且每条消息的处理都要花费很长的时间,那么可以设置预取值为1,这样同一时间,ActiveMQ 只会为这个消费者分派一条消息。Java 客户端:在 ConnectionFactory 层面为所有消费者配置预取值:tcp://localhost:61616?jms.prefetchPolicy.all=50在 ConnectionFactory 层面为队列消费者配置预取值:tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1使用 “目标选项”为一个消费者配置预取值:queue = new ActiveMQQueue("TEST.QUEUE?consumer.prefetchSize=10");consumer = session.createConsumer(queue);4.4.5 配置连接URLActiveMQ 支持通过Configuration URI 明确的配置连接属性。例如:当要设置异步发送时,可以通过在 Configuration URI 中使用jms.$PROPERTY 来设置。tcp://localhost:61616?jms.useAsyncSend=true以下的选项在 URI 必须以“jms.”为前缀。属性 默认值 描述alwaysSessionAsync true If this flag is set thena seperate thread is notused for dispatchingmessages for each Session inthe Connection. However, aseparate thread is alwaysused if there is more thanone session, or the sessionisn't in auto acknowledge ordups ok modeclientID null Sets the JMS clientID touse for the connectioncloseTimeout 15000(milliseconds)Sets the timeout beforea close is consideredcomplete. Normally aclose() on a connectionwaits for confirmation fromthe broker; this allows thatoperation to timeout to savethe client hanging if thereis no broker.copyMessageOnSend true Should a JMS message becopied to a new JMS Messageobject as part of the send()method in JMS. This isenabled by default to becompliant with the JMSspecification. You candisable it if you do notmutate JMS messages afterthey are sent for aperformance boost.disableTimeStampsByDefault false Sets whether or nottimestamps on messagesshould be disabled or not.If you disable them it addsa small performance boost.dispatchAsync false Should the brokerdispatch messagesasynchronously to theconsumer.nestedMapAndListEnabled true Enables/disableswhether or not StructuredMessage Properties andMapMessages are supportedso that Message propertiesand MapMessage entries cancontain nested Map and Listobjects. Available sinceversion 4.1 onwardsobjectMessageSerializationDeferedfalse When an object is set onan ObjectMessage, the JMSspec requires the object tobe serialized by that setmethod. Enabli

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值