11.2 Advisory 消息
}?
else if (data.getDataStructureType() == RemoveInfo.DATA_STRUCTURE_TYPE)
{
RemoveInfo removeInfo = (RemoveInfo) data;
System.out.println("Connection stopped: " + removeInfo.getObjectId());
}?
else
{
System.err.println("Unknown message " + data);
}
?
You can see from the example that we use a regular JMS construct to start listening to
advisory topics. It’s worth noting the use of the AdvisorySupport class, which contains
the definition of all the advisory topic definitions. Things get harder when we start
using ActiveMQ-specific command objects—although a ConnectionInfo is sent when
a connection starts, a RemoveInfo is sent when a connection stops. The RemoveInfo
does carry the connectionId (set as the RemoveInfo’s objectId)—so it’s possible to
correlate which connection has stopped.
?
从上面的例子中可以看到,我们使用了一个常规的JMS结构启动了对advisory主题的监听.
(译注:貌似意思就是说,这里监听advisory主题的程序结构跟监听普通消息主题的程序结构类似的)
值得一提的是AdvisorySupport类,该类包含了所有的advisory主题(消息目的地)的定义.
尽管在连接启动时会发送一个ConnectionInfo,在连接关闭是发送一个RemoveInfo,
但使用特定的ActiveMQ命令对象更加困难了.RemoveInfo包含connectionId(通过RemoveInfo的
objectId获取),因此可以知道由connectionId关联的连接停止了.
?
Most advisory messages are specific to destinations. But the AdvisorySupport class
does have some helper methods to determine which advisory topic to listen to. You
can also use wildcards—so, for example, if you created an advisory topic for the queue
named >, you’d get information for all queues.
?
大部分advisory消息是关于消息目的地的.但是,AdvisorySupport类也有一些帮助方法以决定
要监听那些消息主题.你可以使用通配符--因此,比方说,你使用了>作为队列名称创建了一个
advisory主题,你就可以从所有队列中获取消息.
?
Let’s look at an example of listening for consumers coming and going for a queue
named test.Queue:
下面然我们看一个关于监听名称为test.Queue的消息队列的示例程序,
该消息队列包含了来来往往的消息消费者的相关消息:
?
Listing 11.1 Subscribing for consumer advisories
代码清单11.1 订阅consumer相关的advisorie 消息
?
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);
Connection connection = connectionFactory.createConnection();
connection.start();
//Lets first create a Consumer to listen too
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Lets first create a Consumer to listen too
Queue queue = session.createQueue("test.Queue");
MessageConsumer testConsumer = session.createConsumer(queue);
//so lets listen for the Consumer starting and stopping
Topic advisoryTopic = AdvisorySupport.getConsumerAdvisoryTopic(queue);
MessageConsumer consumer = session.createConsumer(advisoryTopic);
consumer.setMessageListener(
new MessageListener()
{
public void onMessage(Message m)?
{
try?
{
System.out.println("Consumer Count = " + m.getStringProperty("consumerCount"));
DataStructure data = (DataStructure) message.getDataStructure();
if (data.getDataStructureType() == ConsumerInfo.DATA_STRUCTURE_TYPE)?
{
? ConsumerInfo consumerInfo = (ConsumerInfo) data;
System.out.println("Consumer started: " + consumerInfo);
}?
else if (data.getDataStructureType() == RemoveInfo.DATA_STRUCTURE_TYPE)?
{
RemoveInfo removeInfo = (RemoveInfo) data;
System.out.println("Consumer stopped: "+ removeInfo.getObjectId());
}
else?
{
System.err.println("Unknown message " + data);
}
} catch (JMSException e)?
{
e.printStackTrace();
}
}
? ? );
?
testConsumer.close();
?
You’ll notice in the example that we create a test consumer on the queue test.queue
before we create the listener for consumer advisories on test.queue. This is to demonstrate
that the ActiveMQ broker will also send advisory messages for consumers that
already exist when you start to listen for them.
?
从示例代码中可以看到,我们为消息队列test.queue创建了一个测试消息消费者,然后为test.queue的
consumer相关的 advisory消息创建了监听器.这就说明,在启动监听已有的消费者时,ActiveMQ同样
会发送advisory消息.
?
There are some advisories on destinations that aren’t enabled by default; these are
advisories on message delivery, slow consumers, fast producers, and so forth. To enable
these advisories, you have to configure them on a destination policy in the ActiveMQ
broker configuration file. For example, to configure an advisory message for slow consumers
on all queues, you need to add the following to your configuration:
?
默认情况下,没有启用一些消息目的地相关的advisory消息,这些默认没有启动的消息是:消息分发相关的advisory消息,
慢速消费者相关的advisory消息,快速生产者相关的advisory消息,等等.要启用这些advisory消息,你需要在
ActiveMQ代理配置文件中配置消息目的地策略.例如,要配置所有队列的慢速消费者的advisory消息,你需要
添加如下代码片段到你的配置文件中:
?
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue=">" advisoryForSlowConsumers="true" />
</policyEntries>
</policyMap>
</destinationPolicy>
?
You can use advisory messages to supplement your application behavior (for example,
you could slow message production from your producers if you have slow consumers)
or to supplement JMX monitoring of the ActiveMQ broker. Advisory messages are useful
for getting dynamic information on changes to your ActiveMQ system.
?
借助advisory消息,你可以增加你的程序功能(比如,如果你有一些慢速消息消费者,你可以让你的消息
生产者见面生产消息的速度),或者使用JMX监控ActiveMQ代理.Advisory消息在你的ActiveMQ系统
发生变化时获取动态信息时十分有用.
?
Many ?different ?advisories are ?generated ?by the ?ActiveMQ ?broker to ?provide
information about the running system. A dozen of the more useful advisory topics
appear in ?the numbered ?list below, ?and their ?properties (matched ?by number)
appear in table 11.1.
?
ActiveMQ代理生成多个不同的advisory消息以便提供系统的运行信息.下面带编号的列表中列出了更多
有用的advisory消息主题,表11.1中列出了这些消息主题的相关属性(根据编号关联).
?
1 ActiveMQ.Advisory.Connection
2 ActiveMQ.Advisory.Producer.Queue
3 ActiveMQ.Advisory.Consumer.Queue
4 ActiveMQ.Advisory.Queue
5 ActiveMQ.Advisory.Expired.Queue
6 ActiveMQ.Advisory.SlowConsumer.Queue
7 ActiveMQ.Advisory.FastProducer.Queue
8 ActiveMQ.Advisory.MessageDelivered.Queue
9 ActiveMQ.Advisory.MessageConsumed.Queue
10 ActiveMQ.Advisory.FULL
11 ActiveMQ.Advisory.MasterBroker
12 ActiveMQ.Advisory.MessageDLQd.Queue
?
Table 11.1 Properties from the list of 12 ActiveMQ advisory topics
表11.1 ActiveMQ的advisory主题的属性
?
Description PropertiesData structure Generated by default ?Policy entry property
?
1 Generated when ?a connection ?start/stops null null true none
?
2 Producer start/stop messages on a queue ? ? ? String='producerCount'— ? ProducerInfo true none
number of producers ?
?
3 Consumer start/stop messages on a Queue ? ? String='consumerCount'—ConsumerInfo true none
number of Consumers
?
4 Queue created/destroyednull nulltrue none
?
5 Expired messages on aqueueString='orignalMessageId'— Message true none
expired id
?
6 Slow queue consumerString='consumerId'— ConsumerInfo false advisoryForSlowConsumers
consumer ID
?
7 Fast queue producerString='producerId'—ProducerInfo false advisdoryForFastProducers
producer ID
?
8 Message delivered to the brokerString='orignalMessageId'—Message false advisoryForDelivery
delivered ID
?
9 Message consumed by a clientString='orignalMessageId'—Message false advisoryForConsumed
delivered ID
?
10 A usage resource is at its limitString='usageName'—null false advisoryWhenFull
name of usage resource
?
11 A broker is now the master in a null null true none
master/slave ?configuration
?
12 Message sent to a dead letter queue ? ? ? ? ? ? String='orignalMessageId'—Message true none
delivered ID
?
?
In the next section, we’re going to change tack and look at an advanced feature called
virtual topics, which can be used to supplement the way you consume messages, combining
the features of both of topics and queues.
?
在下一节中,我们将改变方针,会看到虚拟主题这一高级特性.使用虚拟主题,结合消息主题和队列的相关特性
可以用来补充你是用消息的方式.
?