ActiveMQ易做图使用和原理
在ActiveMQ中使用易做图和过滤器的使用多采用插件的形式实现,继承BrokerFilter实现BrokerPlugin接口类。BrokerFilter实质一个实现Broker接口的类。
public inte易做图ce BrokerPlugin {
/**
* Installs the plugin into the interceptor chain of the broker, returning the new
* intercepted broker to use.
*/
Broker installPlugin(Broker broker) throws Exception;
}
1. 日志易做图
日志易做图是Broker的一个易做图,默认的日志级别为INFO。你如你想改变日志的级别。这个日志易做图支持Commons-log和Log4j两种日志。
Attribute
Description
Default Value
logAll
Log all Events
false
logMessageEvents
Events related with producing, consuming and dispatching messages
false
logConnectionEvents
Events related to connections and sessions
true
logTransactionEvents
Events related to transaction handling
false
logConsumerEvents
Events related to consuming messages
false
logProducerEvents
Events related to producing messages
false
logInternalEvents
Events normally not of Interest for users like failover, querying internal objects etc
false
默认连接时间日志是开启,其他均未开启。通过activemq开启相关的日志。
<plugins>
<!-- lets enable detailed logging in the broker but ignore ConnectionEvents -->
<loggingBrokerPlugin logAll="true" logConnectionEvents="false"/>
</plugins>
2. 统计易做图
从ActiveMQ5.3开始,StatisticsPlugin插件被用作检测Broker中统计的插件。注意消息必须包含replyTo的消息头,如果是在JMS那么需要采用jmsReplyTo消息头,否则消息将被统计忽略。ReplyTo消息头包含了你想检查统计的消息。统计消息是一个MapMessage.
检查Broker的信息,仅仅需要一个名称为ActiveMQ.Statistics.Broker并且有一个replyTo的消息头的Destination。为了检测所有destination,你需要一个名称为ActiveMQ.Statistics.Destination.<destination-name>或者ActiveMQ.Statistics.Destination.<wildcard-expression>并且有一个replyTo的消息头。如果许多Destination匹配相关的模糊匹配表达式,那么统计的消息将被发送到replyTo的Destination.
<plugins>
<!-- lets enable detailed logging in the broker but ignore ConnectionEvents -->
<statisticsBrokerPlugin/>
</plugins>
Statistics插件发送消息到特殊的目标。
下面是一个统计Broker的消息
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);
String queueName = "ActiveMQ.Statistics.Broker";
Queue testQueue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(testQueue);
Message msg = session.createMessage();
msg.setJMSReplyTo(replyTo);
producer.send(msg);
MapMessage reply = (MapMessage) consumer.receive();
assertNotNull(reply);
for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
String name = e.nextElement().toString();
System.out.println(name + "=" + reply.getObject(name));
}
查询Destination的统计信息
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);
Queue testQueue = session.createQueue("TEST.FOO");
MessageProducer producer = session.createProducer(null);
String queueName = "ActiveMQ.Statistics.Destination." + testQueue.getQueueName()
Queue query = session.createQueue(queueName);
Message msg = session.createMessage();
producer.send(testQueue, msg)
msg.setJMSReplyTo(replyTo);
producer.send(query, msg);
MapMessage reply = (MapMessage) consumer.receive();
assertNotNull(reply);
assertTrue(reply.getMapNames().hasMoreElements());
for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
String name = e.nextElement().toString();
System.err.println(name + "=" + reply.getObject(name));
}
补充:综合编程 , 其他综合 ,