ActiveMQ拦截器使用和原理

本文介绍ActiveMQ中的日志拦截器和统计插件的配置与使用方法。日志拦截器支持调整不同级别的日志输出,统计插件则能收集Broker及Destination的相关统计数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


     在ActiveMQ中使用拦截器和过滤器的使用多采用插件的形式实现,继承BrokerFilter实现BrokerPlugin接口类。BrokerFilter实质一个实现Broker接口的类。

public interface 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));

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值