- 浏览: 478751 次
- 性别:
- 来自: 大连
文章分类
最新评论
-
龘龘龘:
TrueBrian 写道有个问题,Sample 1中,为了控制 ...
What's New on Java 7 Phaser -
龘龘龘:
楼主总结的不错。
What's New on Java 7 Phaser -
TrueBrian:
有个问题,Sample 1中,为了控制线程的启动时机,博主实际 ...
What's New on Java 7 Phaser -
liguanqun811:
不知道楼主是否对zookeeper实现的分布式锁进行过性能测试 ...
Distributed Lock -
hobitton:
mysql的get lock有版本限制,否则get lock可 ...
Distributed Lock
2.6.7 Wildcards
Wildcards用来支持联合的名字分层体系(federated name hierarchies)。它不是JMS规范的一部分,而是ActiveMQ的扩展。ActiveMQ支持以下三种wildcards:
- "." 用于作为路径上名字间的分隔符。
- "*" 用于匹配路径上的任何名字。
- ">" 用于递归地匹配任何以这个名字开始的destination。
作为一种组织事件和订阅感兴趣那部分信息的一种方法,这个概念在金融市场领域已经流行了一段时间了。设想你有以下两个destination:
- PRICE.STOCK.NASDAQ.IBM (IBM在NASDAQ的股价)
- PRICE.STOCK.NYSE.SUNW (SUN在纽约证券交易所的股价)
订阅者可以明确地指定destination的名字来订阅消息,或者它也可以使用wildcards来定义一个分层的模式来匹配它希望订阅的destination。例如:
Subscription | Meaning |
PRICE.> | Any price for any product on any exchange |
PRICE.STOCK.> | Any price for a stock on any exchange |
PRICE.STOCK.NASDAQ.* | Any stock price on NASDAQ |
PRICE.STOCK.*.IBM | Any IBM stock price on any exchange |
2.6.8 Async Sends
ActiveMQ支持以同步(sync)方式或者异步(async)方式向broker发送消息。 使用何种方式对send方法的延迟有巨大的影响。对于生产者来说,既然延迟是决定吞吐量的重要因素,那么使用异步发送方式会极大地提高系统的性能。
ActiveMQ缺省使用异步传输方式。但是按照JMS规范,当在事务外发送持久化消息的时候,ActiveMQ会强制使用同步发送方式。在这种情况下,每一次发送都是同步的,而且阻塞到收到broker的应答。这个应答保证了broker已经成功地将消息持久化,而且不会丢失。但是这样作也严重地影响了性能。
如果你的系统可以容忍少量的消息丢失,那么可以在事务外发送持久消息的时候,选择使用异步方式。以下是几种不同的配置方式:
cf = new ActiveMQConnectionFactory("tcp://locahost:61616?jms.useAsyncSend=true"); ((ActiveMQConnectionFactory)connectionFactory).setUseAsyncSend(true); ((ActiveMQConnection)connection).setUseAsyncSend(true);
2.6.9 Dispatch Policies
2.6.9.1 Round Robin Dispatch Policy
在2.6.4小节介绍过ActiveMQ的prefetch机制,ActiveMQ的缺省参数是针对处理大量消息时的高性能和高吞吐量而设置的。所以缺省的prefetch参数比较大,而且缺省的dispatch policies会尝试尽可能快的填满prefetch缓冲。然而在有些情况下,例如只有少量的消息而且单个消息的处理时间比较长,那么在缺省的prefetch和dispatch policies下,这些少量的消息总是倾向于被分发到个别的consumer上。这样就会因为负载的不均衡分配而导致处理时间的增加。
Round robin dispatch policy会尝试平均分发消息,以下是ActiveMQ配置文件的一个例子:
<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="FOO.>"> <dispatchPolicy> <roundRobinDispatchPolicy /> </dispatchPolicy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>
2.6.9.2 Strict Order Dispatch Policy
有时候需要保证不同的topic consumer以相同的顺序接收消息。通常ActiveMQ会保证topic consumer以相同的顺序接收来自同一个producer的消息。然而,由于多线程和异步处理,不同的topic consumer可能会以不同的顺序接收来自不同producer的消息。例如有两个producer,分别是P和Q。差不多是同一时间内,P发送了P1、P2和P3三个消息;Q发送了Q1和Q2两个消息。两个不同的consumer可能会以以下顺序接收到消息:
consumer1: P1 P2 Q1 P3 Q2
consumer2: P1 Q1 Q2 P2 P3
Strict order dispatch policy 会保证每个topic consumer会以相同的顺序接收消息,代价是性能上的损失。以下是采用了strict order dispatch policy后,两个不同的consumer可能以以下的顺序接收消息:
consumer1: P1 P2 Q1 P3 Q2
consumer2: P1 P2 Q1 P3 Q2
以下是ActiveMQ配置文件的一个例子:
<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=""FOO.>"> <dispatchPolicy> <strictOrderDispatchPolicy /> </dispatchPolicy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>
2.6.10 Message Cursors
当producer发送的持久化消息到达broker之后,broker首先会把它保存在持久存储中。接下来,如果发现当前有活跃的consumer,而且这个consumer消费消息的速度能跟上producer生产消息的速度,那么ActiveMQ会直接把消息传递给broker内部跟这个consumer关联的dispatch queue;如果当前没有活跃的consumer或者consumer消费消息的速度跟不上producer生产消息的速度,那么ActiveMQ会使用Pending Message Cursors保存对消息的引用。在需要的时候,Pending Message Cursors把消息引用传递给broker内部跟这个consumer关联的dispatch queue。以下是两种Pending Message Cursors:
- VM Cursor。在内存中保存消息的引用。
- File Cursor。首先在内存中保存消息的引用,如果内存使用量达到上限,那么会把消息引用保存到临时文件中。
在缺省情况下,ActiveMQ 5.0根据使用的Message Store来决定使用何种类型的Message Cursors,但是你可以根据destination来配置Message Cursors。
对于topic,可以使用的pendingSubscriberPolicy 有vmCursor和fileCursor。可以使用的PendingDurableSubscriberMessageStoragePolicy有vmDurableCursor 和 fileDurableSubscriberCursor。以下是ActiveMQ配置文件的一个例子:
<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="org.apache.>"> <pendingSubscriberPolicy> <vmCursor /> </pendingSubscriberPolicy> <PendingDurableSubscriberMessageStoragePolicy> <vmDurableCursor/> </PendingDurableSubscriberMessageStoragePolicy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>
对于queue,可以使用的pendingQueuePolicy有vmQueueCursor 和 fileQueueCursor。以下是ActiveMQ配置文件的一个例子:
<destinationPolicy> <policyMap> <policyEntries> <policyEntry queue="org.apache.>"> <pendingQueuePolicy> <vmQueueCursor /> </pendingQueuePolicy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>
2.6.11 Optimized Acknowledgement
ActiveMQ缺省支持批量确认消息。由于批量确认会提高性能,因此这是缺省的确认方式。如果希望在应用程序中禁止经过优化的确认方式,那么可以采用如下方法:
cf = new ActiveMQConnectionFactory ("tcp://locahost:61616?jms.optimizeAcknowledge=false"); ((ActiveMQConnectionFactory)connectionFactory).setOptimizeAcknowledge(false); ((ActiveMQConnection)connection).setOptimizeAcknowledge(false);
2.6.12 Producer Flow Control
同步发送消息的producer会自动使用producer flow control ;对于异步发送消息的producer,要使用producer flow control,你先要为connection配置一个ProducerWindowSize参数,如下:
((ActiveMQConnectionFactory)cf).setProducerWindowSize(1024000);
ProducerWindowSize是producer在发送消息的过程中,收到broker对于之前发送消息的确认之前, 能够发送消息的最大字节数。你也可以禁用producer flow control,以下是ActiveMQ配置文件的一个例子:
<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="FOO.>" producerFlowControl="false"> <dispatchPolicy> <strictOrderDispatchPolicy/> </dispatchPolicy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>
2.6.13 Message Transformation
有时候需要在JMS provider内部进行message的转换。从4.2版本起,ActiveMQ 提供了一个MessageTransformer 接口用于进行消息转换,如下:
public interface MessageTransformer { Message producerTransform(Session session, MessageProducer producer, Message message) throws JMSException; Message consumerTransform(Session session, MessageConsumer consumer, Message message)throws JMSException; }
通过在以下对象上通过调用setTransformer方法来设置MessageTransformer:
- ActiveMQConnectionFactory
- ActiveMQConnection
- ActiveMQSession
- ActiveMQMessageConsumer
- ActiveMQMessageProducer
MessageTransformer接口支持:
- 在消息被发送到JMS provider的消息总线前进行转换。通过producerTransform方法。
- 在消息到达消息总线后,但是在consumer接收到消息前进行转换。通过consumerTransform方法。
以下是个简单的例子:
public class SimpleMessage implements Serializable { // private static final long serialVersionUID = 2251041841871975105L; // private String id; private String text; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
在producer内发送ObjectMessage,如下:
SimpleMessage sm = new SimpleMessage(); sm.setId("1"); sm.setText("this is a sample message"); ObjectMessage message = session.createObjectMessage(); message.setObject(sm); producer.send(message);
在consumer的session上设置一个MessageTransformer用于将ObjectMessage转换成TextMessage,如下:
((ActiveMQSession)session).setTransformer(new MessageTransformer() { public Message consumerTransform(Session session, MessageConsumer consumer, Message message) throws JMSException { ObjectMessage om = (ObjectMessage)message; XStream xstream = new XStream(); xstream.alias("simple message", SimpleMessage.class); String xml = xstream.toXML(om.getObject()); return session.createTextMessage(xml); } public Message producerTransform(Session session, MessageProducer consumer, Message message) throws JMSException { return null; } });
评论
是初学,还不是很懂,能稍微写个小例子让我看懂么?谢谢了
难怪caoxg 让我先试用memcacheq
我的msn :comchyi00@hotmail.com
email : honnom@163.com
在这里支持white sock。
发表评论
-
Terracotta in Action (3)
2009-03-25 09:20 50903 Inside Terracotta 3.1 Core T ... -
Terracotta in Action (2)
2009-03-21 21:09 46182. Terracotta Eclipse Plugin ... -
Terracotta in Action (1)
2009-03-19 21:52 62441. Introduction Terraco ... -
OpenEJB (4)
2008-05-11 09:05 3136本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenEJB (3)
2008-05-11 09:04 2691本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenEJB (2)
2008-05-11 09:03 3283本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenEJB (1)
2008-05-10 22:39 5069本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenJPA (7)
2008-03-25 21:56 351610 Miscellaneous Features 10 ... -
OpenJPA (6)
2008-03-23 21:33 62558 Object Locking 8.1 Configu ... -
OpenJPA (5)
2008-03-18 22:38 49607 Inheritance 对象使用引用以便关联到其 ... -
OpenJPA (4)
2008-03-11 23:27 70406 Query 6.1 JPQL Queries 6.1. ... -
OpenJPA (3)
2008-03-09 23:09 51944 EntityManagerFactory 4.1 Ove ... -
OpenJPA (2)
2008-03-05 23:59 72513 Metadata 通过javax.persist ... -
OpenJPA (1)
2008-03-04 23:11 68301 Overview Apache OpenJPA是 ... -
ActiveMQ in Action (6)
2008-02-26 15:22 137332.6 Features ActiveMQ包含了很多 ... -
ActiveMQ in Action (5)
2008-02-26 00:35 135312.5 Clustering ActiveMQ从多种 ... -
ActiveMQ in Action (4)
2008-02-26 00:21 112122.4 Security ActiveMQ ... -
ActiveMQ in Action (3)
2008-02-26 00:16 105912.3 Persistence 2.3.1 AMQ Mess ... -
ActiveMQ in Action (2)
2008-02-25 23:58 132172.2 Transport ActiveMQ目前支持 ... -
ActiveMQ in Action (1)
2008-02-25 23:18 237831 JMS 在介绍ActiveMQ ...
相关推荐
ActiveMQ In Action及其源码 本来想免费提供下载的,但是发现csdn不能选择0积分下载,坑的很
ActiveMQ in Action pdf英文原版加源代码压缩包。 Apache ActiveMQ in Action is a thorough, practical guide to implementing message-oriented systems in Java using ActiveMQ. The book lays out the core of ...
这是最新版的《ActiveMQ in Action》包括第7、9、11、13等章节
ActiveMQ in Action的开发文档
activeMq in action 使用activeMq开发JMS的简单讲述,activeMq in action 使用activeMq开发JMS的简单讲述
ActiveMQ in Action 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
ActiveMQ In Action翻译笔记-更新版2011
activemq in action PDF 英文版 源代码 合二为一,目前好像只有英文版,源代码一起发布的少。我收集到了,分享给大家。 这本书讲得真是不错,英文看起来也不难,希望对你有帮助 。
ActiveMQ In Action的精简版
ActiveMQ 不仅实现了 JMS 规范中定义的所有特性,也额外提供了一些特有且有用的特性。我们会在 3.1 小节详细列说这些特性,并且书中剩余的章节还会继续讨论这些特性。为了演示这些特性,我们创建了两个基于实际业务...
ActiveMQ_in_Action_中文ActiveMQ_in_Action_中文ActiveMQ_in_Action_中文ActiveMQ_in_Action_中文
ActiveMQ in Action
经典的学习ActiveMQ的书籍。这本书讲得真是不错,英文看起来也不难,希望对你有帮助 。
ActiveMQ In Action.zip
ActiveMQ_in_Action.pdf 教程文档
activemq in action - activemq in action
ActiveMQ_in_Action_中文.zip
ActiveMQ+In+Action翻译笔记-+更新版2011.pdf
ActionMQ In Action 就是网上可以下载的那个版本, 不知道作者是谁,和英文版的书应该不是一回事。在原版的基础上增加了书签,可以看的更方便。这本书对ActiveMQ入门到一般的管理非常有帮助。