当前位置: 首页 > ActiveMQ in Action 读书笔记 > 正文

11.2 Advisory 消息

11.2 Advisory messages

Advisory messages are notification messages generated by the ActiveMQ broker as aresult of changes to the broker. Typically, an advisory message will be generated everytime a new administered object (connection, destination, consumer, producer) joinsor leaves the broker, but advisory messages can be generated to warn about theActiveMQ broker reaching system limits, too.

11.2 Advisory 消息

Advisory消息是在ActiveMQ代理发生改变时生成的通知消息.通常,在一个新的管理对象(连接,消息目的地,消息生产者消息消费者)加入或离开代理时生成advisory消息,同样,在ActiveMQ代理达到系统限制时也可以生成advisory消息作为告警.

Advisory messages are regular JMS messages that are generated on system-defined topics, which enables ActiveMQ applications to be notified asynchronously  over JMS of changes in the ActiveMQ broker’s state. They can be a good  alternative to using JMX to find out about the running state of an ActiveMQ broker. Advisory消息是常规的JMS消息,来自系统自定义的消息主题中,使得在ActiveMQ代理状态发生变化时通过JMS异步的通知使用ActiveMQ的应用程序.使用Advisory消息可以替代JMX用于监控ActiveMQ代理的运行状态.
ActiveMQ uses advisory messages internally too, to notify connections about  theavailability  of   temporary  destinations   and  notify   networks  about   theavailability of consumers, so you should take care if you want to disable them. 在ActiveMQ内部也使用advisory消息来通知连接哪些临时消息目的地是可用的,以及通知网络那些消息消费者是可用的,因此在禁用advisory消息执勤啊,你需要仔细考虑.
Every advisory message generated has a JMS Type of Advisory and predefined JMSString properties, identifying the broker where the advisory was generated:

? originBrokerId—The ID of the broker that generated the advisory
? originBrokerName—The name of the broker that generated the advisory
? originBrokerURL—The first transport connector URL of the broker that generated the advisory

每一个advisory消息都有一个JMS类型的Advisory以及一个预定义额JMS字符串属性,用于指定该advisory消息是哪个代理生成的.
? originBrokerId—生成该 advisory消息的代理ID
? originBrokerName—生成该 advisory消息的代理名称
? originBrokerURL—生成该 advisory消息的代理的首个传输连接器的
Advisory messages for changes in  state to the administered objects  usually useActiveMQ-specific internal  commands as  the payload,  but they  do carry usefulinformation. Let’s look at how to listen for connections starting and  stoppingwith ActiveMQ: 因管理对象状态改变而生成的Advisory消息通常使用ActiveMQ特定的内部命令作为有效载荷(译注:消息的主要内容),但是这些消息确实含有一些有用信息.下面,让我们来看看如何监听ActiveMQ的启动和停止:
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic connectionAdvisory = AdvisorySupport.CONNECTION_ADVISORY_TOPIC;
MessageConsumer consumer = session.createConsumer(connectionAdvisory);
ActiveMQMessage message = (ActiveMQMessage) consumer.receive();
DataStructure data = (DataStructure) message.getDataStructure();
if (data.getDataStructureType() == ConnectionInfo.DATA_STRUCTURE_TYPE) 
{
	ConnectionInfo connectionInfo = (ConnectionInfo) data;
	System.out.println("Connection started: " + connectionInfo);
} 
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 toadvisory topics. It’s worth noting the use of the AdvisorySupport class, which containsthe definition of all the advisory topic definitions. Things get harder when we startusing ActiveMQ-specific command objects—although a ConnectionInfo is sent whena connection starts, a RemoveInfo is sent when a connection stops. The RemoveInfodoes carry the connectionId (set as the RemoveInfo’s objectId)—so it’s possible tocorrelate 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 classdoes have some helper methods to determine which advisory topic to listen to. Youcan also use wildcards—so, for example, if you created an advisory topic for the queuenamed >, 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:
Listing 11.1 Subscribing for consumer advisories
下面然我们看一个关于监听名称为test.Queue的消息队列的示例程序,该消息队列包含了来来往往的消息消费者的相关消息:
代码清单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.queuebefore we create the listener for consumer advisories on test.queue. This is to demonstratethat the ActiveMQ broker will also send advisory messages for consumers thatalready 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 areadvisories on message delivery, slow consumers, fast producers, and so forth. To enablethese advisories, you have to configure them on a destination policy in the ActiveMQbroker configuration file. For example, to configure an advisory message for slow consumerson 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 usefulfor getting dynamic information on changes to your ActiveMQ system. 借助advisory消息,你可以增加你的程序功能(比如,如果你有一些慢速消息消费者,你可以让你的消息生产者见面生产消息的速度),或者使用JMX监控ActiveMQ代理.Advisory消息在你的ActiveMQ系统发生变化时获取动态信息时十分有用.
Many different  advisories are  generated  by the  ActiveMQ  broker to  provideinformation about the running system. A dozen of the more useful advisory topicsappear 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 Properties Data 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’—number of producers ProducerInfo true none
3 Consumer start/stop messages on a Queue String=’consumerCount’—number of Consumers  ConsumerInfo true none
4  Queue created/
destroyed
 null null true none
5  Expired messages on a queue String=’orignalMessageId’—expired id  Message true none
6  Slow queue
consumer
String=’consumerId’—consumer ID  ConsumerInfo false  dvisory
For
Slow
Consumers
7 Fast queue producer String=’producerId’—producer ID ProducerInfo false advisdory
ForFast
Producers
8  Message delivered to the broker String=’orignalMessageId’—delivered ID  Message false advisory
For
Delivery
9  Message consumed by a client  Message consumed by a client Message false advisory
For
Consumed
10  A usage resource is at its limit String=’usageName’—name of usage resource null  false advisory
WhenFull
11 A broker is now the master in a master/slave
configuration
null  null  true  none
12  Message sent to a dead letter queue  String=’orignalMessageId’—delivered ID Message  true  none
In the next section, we’re going to change tack and look at an advanced feature calledvirtual topics, which can be used to supplement the way you consume messages, combiningthe features of both of topics and queues. 在下一节中,我们将改变方针,会看到虚拟主题这一高级特性.使用虚拟主题,结合消息主题和队列的相关特性可以用来补充你是用消息的方式.
赞 赏

   微信赞赏  支付宝赞赏


本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/1688.html | 边城网事

该日志由 边城网事 于2013年12月16日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 11.2 Advisory 消息 | 边城网事
关键字: , , ,

11.2 Advisory 消息 暂无评论

发表评论

快捷键:Ctrl+Enter