问题描述
我正在尝试为activemq(5.15.13)创建一个拦截器。 我使用https://activemq.apache.org/interceptors中的代码并将其编译为jar文件。 我将jar文件添加到/ lib文件夹。
然后我将其添加到activemq.xml
<plugins>
<loggingbrokerPlugin logall="true" logConnectionEvents="true"/>
<bean xmlns="http://www.springframework.org/schema/beans" id="myPlugin" class="com.xxx.mqplugin.MyPlugin"/>
</plugins>
我知道
jvm 1 |信息|创建的LoggingbrokerPlugin:LoggingbrokerPlugin(logall = true,logConnectionEvents = true,logSessionEvents = true,logConsumerEvents = false,logProducerEvents = false,logTransactionEvents = false,logInternalEvents = false)
package com.xxx.mqplugin;
import java.util.logging.Logger;
import org.apache.activemq.broker.broker;
import org.apache.activemq.broker.brokerFilter;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.region.MessageReference;
import org.apache.activemq.command.ConnectionInfo;
import org.apache.activemq.command.ProducerInfo;
import org.apache.activemq.command.SessionInfo;
public class Mybroker extends brokerFilter {
public Mybroker(broker next) {
super(next);
}
public void addConnection(ConnectionContext context,ConnectionInfo info) throws Exception {
// Your code goes here
System.out.println("addConnection:" + info.toString());
Logger.getLogger("test").info("addConnection:" + info.toString());
// Then call your parent
super.addConnection(context,info);
}
public void addSession(ConnectionContext context,SessionInfo info) throws Exception {
// Your code goes here...
System.out.println("addSession:" + info.toString());
Logger.getLogger("test").info("addSession:" + info.toString());
// Then call your parent
super.addSession(context,info);
}
@Override
public void addProducer(ConnectionContext context,ProducerInfo info) throws Exception {
// Your code goes here...
System.out.println("addProducer:" + info.toString());
Logger.getLogger("test").info("addProducer:" + info.toString());
super.addProducer(context,info);
}
@Override
public void messageDelivered(ConnectionContext context,MessageReference messageReference) {
System.out.println("messageDelivered:" + messageReference.toString());
Logger.getLogger("test").info("messageDelivered:" + messageReference.toString());
super.messageDelivered(context,messageReference);
}
}
Thanks
解决方法
您还需要实现一个“ BrokerPlugin”对象,该对象是用于安装“ BrokerFilter”实例的类型。
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerPlugin;
public class MyPlugin implements BrokerPlugin {
public Broker installPlugin(Broker broker) throws Exception {
return new MyBroker(broker);
}
}
那将是您在代理XML配置中使用的类型
<plugins>
<bean xmlns="http://www.springframework.org/schema/beans" id="myPlugin" class="org.myorg.MyPlugin"/>
</plugins>