首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

11.3 使用虚拟化加强JMS消息主题

2013-12-19 
11.3 使用虚拟化增强JMS消息主题11.3 Supercharge JMS topics by going virtual11.3 使用虚拟化增强JMS消

11.3 使用虚拟化增强JMS消息主题

11.3 Supercharge JMS topics by going virtual

11.3 使用虚拟化增强JMS消息主题

?

If you want to broadcast a message to multiple consumers, then you use a JMS topic. If

you want a pool of consumers to receive messages from a destination, then you use a

JMS queue. But there’s no satisfactory way to send a message to a topic and then have

multiple consumers receiving messages on that topic the way you can with queues.

?

如果你想广播消息到多个消息消费者,你会使用JMS消息主题.如果你打算让多个消息消费者从

一个消息目的地接收消息,你会使用JMS消息队列.但是没有令人满意的方式用来发送消息到一个主题

并且想使用消息队列那样让多个消息消费者从这个主题接收消息.

?

The JMS spec requires that a durable subscriber to a topic use a unique JMS client ID

and subscriber name. Also, only one thread (a single consumer) can be active at any

time with that unique JMS client ID and subscriber name. This means that if that subscriber

dies for some reason, there will be no failover to another consumer and there’s

no ability to load balance messages across competing consumers. But using JMS queue

semantics allows the ability to fail over consumers, to load balance messages among

competing consumers, and to use ActiveMQ message groups (see chapter 12), which

allows sticky load balancing of messages to maintain message order. Furthermore, JMS

queue depths can be monitored via JMX (see chapter 14). Using virtual topics works

around these disadvantages while still retaining the benefits of JMS topics.

?

JMS规范要求持久化的主题订阅者使用唯一的JMS client ID和name属性.同样,同一时间只运行激活一个使用

该唯一的JMS client ID 和订阅者name的线程.(单一的消息消费者).这就是说,如果这个订阅者因为某种原因

失效后没有任何可以用来作为失效转移的消息消费者了,并且负载均衡消息也不能在处于竞争状态的消息消费者

中传递.使用JMS消息队列允许消息消费者进行失效转移,允许负载均衡消息在处于竞争状态的消息消费者之间

传递,并且允许使用消息群组(见12章)以便用粘性负载均衡消息来维持消息额次序.此外, JMS队列深度可以

通过JMX进行监控(参见第14章) .使用虚拟消息主题可以保留使用JMS主题的好处同时绕过使用主题的缺点.

?

Virtual topics allow a publisher to send messages to a normal JMS topic while consumers

receive messages from a normal JMS queue. So consumers subscribe to a

queue to receive messages that were published to a topic. Figure 11.1 shows a diagram

of how virtual topics are structured in ActiveMQ.

?

虚拟主题允许消息发布者发送消息到一个正常的JMS主题,而消息消费者从一个正常的的JMS队列接收消息.

因此,订阅队列的消息消费者接收到的消息实际上是发布到一个主题的.图11.1展示了ActiveMQ中虚拟主题的

结构.

?

Some naming conventions are required to allow virtual topics to operate correctly.

First, to identify that a topic is to be treated as a virtual topic, the topic name should

always follow the pattern of VirtualTopic.<topic name>. So if you want to create a virtual

topic for a topic whose name is orders, you need to create a destination with the name

VirtualTopic.orders. This means that a publisher sends messages to a topic named

VirtualTopic.orders. In order to consume from the queue that’s backed by the virtual

topic, consumers must subscribe to a queue whose name follows the pattern

Consumer.<consumer name>.VirtualTopic.<virtual topic name>.

?

为保证虚拟主题工作正常,需要使用一些命名规范.首先,为了标识一个主题作为虚拟主题,主题的名称

需要符合下面的格式:VirtualTopic.<topic name>.因此,如果你打算创建一个名称为 orders的

虚拟主题,你需要创建一个名称为VirtualTopic.orders的消息目的地.这就是说,消息发布者发送消息

到名称为VirtualTopic.orders的主题.为了使用虚拟主题维护的消息队列,消息消费者必须订阅一个

队列,该队列的名称需要符合下面的模式:

Consumer.<consumer name>.VirtualTopic.<virtual topic name>

?

Suppose you want consumers to compete for messages on a queue, but you want

that queue to be backed by a topic. You’d create a two queue receivers, each consuming

from a queue named Consumer.Foo.VirtualTopic.orders. An example of this is shown next.

?

设想一下你打算让一些消息消费者争抢来自队列的消息,而这个消息队列是由消息主题维护的.你需要

创建两个队列消息接受者,每个接受者都使用名称为Consumer.Foo.VirtualTopic.orders的队列中的消息.

下面是示例代码:

?

Listing 11.2 Using virtual topics

代码清单11.2 使用虚拟主题

...

String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);

Connection consumerConnection = connectionFactory.createConnection();

consumerConnection.start();

?

String queueName = "Consumer.Foo.VirtualTopic.orders";

?

// Create the first consumer for Consumer.Foo.VirtualTopic.orders

Session sessionA = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);

Queue fooQueueA = sessionA.createQueue(queueName);

MessageConsumer consumerA = sessionA.createConsumer(fooQueueA);

consumerA.setMessageListener(getMessageListener());

?

// Create the second consumer for Consumer.Foo.VirtualTopic.orders

Session sessionB = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);

Queue fooQueueB = sessionB.createQueue(queueName);

MessageConsumer consumerB = sessionB.createConsumer(fooQueueB);

consumerB.setMessageListener(getMessageListener());

?

// Create the sender

String topicName = "VirtualTopic.orders";

Connection senderConnection = connectionFactory.createConnection();

senderConnection.start();

Session senderSession = senderConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Topic ordersDestination = senderSession.createTopic(topicName);

MessageProducer producer = senderSession.createProducer(ordersDestination);

producer.setDeliveryMode(DeliveryMode.PERSISTENT);

?

// Send 2000 messages

for (int i = 0; i < 2000; ++i)?

{

TextMessage message = createMessage(i);

producer.send(message);

}

...

?

In listing 11.2, note that two consumers are subscribed to the same queue whose name

follows the virtual topic naming pattern for the queue side. Also note that the producer

is sending to a topic whose name follows the virtual topic naming pattern for

the topic side. When the 2,000 messages are sent to the topic, each consumer will

receive 1,000 messages from the queue.

?

在代码清单11.2中,注意到两个消息消费者订阅了同一个消息队列,该队列的命名遵循

虚拟主题的中队列一侧的命名模式.同时,注意到消息生产者发送消息到消息主题,该主题的命名

遵循虚拟主题中主题一侧的命名模式.当发送完2000个消息到主题后,每个消息消费者

将从队列接收到1000个消息.

?

?

Virtual topics are a convenient mechanism to combine the load balancing and

failover aspects of queues, with the durability of topics. Not only does the consumer

not need to worry about creating a unique JMS client ID and subscriber name, but the

consumers are competing for messages in a load balanced manner using JMS queue

semantics. If one of the consumers dies, the other consumer will continue to receive

all the messages on the queue.

?

虚拟主题是一种方便的机制,使用了联合消息队列的负载均衡和失效转移特性的持久化主题.不但消息消费者

不选哟担心创建唯一的JMS client ID和订阅者名称属性,而且所有消息消费者可以用JMS 队列语法,并以一种

负载均衡的方式竞争接收消息.如果有一个消息消费者失效了,其他的消费者镜持续接收队列中所有的消息.

.

?

In the next section we’ll look at using ActiveMQ to combine the longevity of durable

subscribers, with the performance of normal topic subscribers.

?

下一节,我们将看到使用ActiveMQ联合持久化订阅者的长生命周期和常规主题订阅者的性能.

?

热点排行