攔截器
ActiveMQ Classic 具有複雜的攔截器堆疊,讓您可以輕鬆地將所需的功能附加到訊息中介軟體,而不會使其他中介軟體程式碼變得複雜。這確實幫助我們保持程式碼的整潔和模組化,同時提供強大的擴充點。
有關您可以使用攔截器執行的各種操作範例,請參閱以下頁面
外掛程式如何運作
外掛程式是 BrokerPlugin 介面的實例,它允許外掛程式將自身加入中介軟體攔截器鏈中,通常使用 BrokerFilter 作為基底類別,以僅允許自訂某些操作。
實作 BrokerPlugin 介面的物件在訊息中介軟體的 XML 組態檔案中稱為外掛程式(請參閱以下範例)。然後,您的外掛程式可以選擇性地參考 XML 檔案中定義的其他 bean。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.org/config/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.org/config/1.0
https://activemq.dev.org.tw/schema/activemq-core.xsd https://activemq.dev.org.tw/camel/schema/spring https://activemq.dev.org.tw/camel/schema/spring/camel-spring.xsd">
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
<broker xmlns="http://activemq.org/config/1.0" brokerName="localhost" dataDirectory="${activemq.base}/data" plugins="#myPlugin">
<!-- The transport connectors ActiveMQ Classic will listen to -->
<transportConnectors>
<transportConnector name="openwire" uri="tcp://127.0.0.1:61616" />
</transportConnectors>
</broker>
<bean id="myPlugin" class="org.myorg.MyPlugin">
<!-- You can reference one or more Spring beans in this file -->
<property name="myMgr" ref="myManager"/>
</bean>
<bean id="myManager" class="org.myorg.MyManager">
<property name="fooList">
<list>
<value>foo</value>
<value>foo2</value>
</list>
</property>
</bean>
</beans>
您也可以從
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.org/config/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.org/config/1.0
https://activemq.dev.org.tw/schema/activemq-core.xsd https://activemq.dev.org.tw/camel/schema/spring
https://activemq.dev.org.tw/camel/schema/spring/camel-spring.xsd">
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
<broker xmlns="http://activemq.org/config/1.0" brokerName="localhost" dataDirectory="${activemq.base}/data">
<!-- The transport connectors ActiveMQ Classic will listen to -->
<transportConnectors>
<transportConnector name="openwire" uri="tcp://127.0.0.1:61616" />
</transportConnectors>
<plugins>
<bean xmlns="http://www.springframework.org/schema/beans" id="myPlugin" class="org.myorg.MyPlugin"/>
</plugins>
</broker>
</beans>
在啟動時,主要或核心中介軟體會呼叫您的外掛程式的 installPlugin() 方法。此方法會建立並傳回一個通常擴充 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);
}
}
BrokerFilter 類別是一個方便的類別,實作了 Broker 介面。此介面定義您的實作可以攔截的所有主要操作(例如,addConnection、addSession 等)。擴充 BrokerFilter 的類別會覆寫 Broker 介面中定義的任何方法,以便它可以攔截對應的核心引擎操作。以下是一個類別擴充 BrokerFilter 並攔截/覆寫 addConnection() 和 addSession() Broker 方法/操作的範例。
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerFilter;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.command.ConnectionInfo;
public class MyBroker extends BrokerFilter {
public MyBroker(Broker next) {
super(next);
}
public void addConnection(ConnectionContext context, ConnectionInfo info)
throws Exception {
// Your code goes here
// Then call your parent
super.addConnection(context, info);
}
public void addSession(ConnectionContext context, SessionInfo info)
throws Exception {
// Your code goes here...
// Then call your parent
super.addSession(context, info);
}
}
如需更多詳細資訊,請參閱開發外掛程式