ActiveMQ與業務系統的集成

ActiveMQ是一款基於Java的開源消息服務器產品,因此,我們可以將其集成到通過Java實現的業務系統中。下面將對集成方法做一個簡單的總結。

首先,我們看一下ActiveMQ中的部分核心類:

org.apache.activemq.ServiceActiveMQ中的一個接口,定義了startstop方法。org.apache.activemq.broker.BrokerServiceActiveMQ中的核心類,其實現自Service接口,用於對外提供消息服務。org.apache.activemq.broker.XBeanBrokerServiceBrokerService的一個子類,XBeanBrokerService實例可以通過xbean xml配置文件生成。ActiveMQ作爲單獨服務啓動時,其配置文件activemq.xml就是xbean xml格式的,啓動後生成的就是一個XBeanBrokerService實例。


1.通過VM協議實現集成:

客戶端連接ActiveMQ消息服務器時,可以使用多種協議(TCPVMSTOMP等),其中VM協議是指客戶端會從當前JVM中查找ActiveMQ消息服務器實例,若查找到,則與其建立連接,若查找不到,則自動創建ActiveMQ消息服務器實例。如果客戶端和服務器端都在一個JVM中,則可以通過VM協議實現ActiveMQ消息服務器的自動創建、與業務系統的集成。


2.直接創建BrokerService實例:

public static void main(String[] args)throws Exception {
  BrokerService broker = new BrokerService();
  broker.setBrokerName("myBroker");
  broker.setDataDirectory("data/");
  SimpleAuthenticationPlugin authentication = newSimpleAuthenticationPlugin();
  List<AuthenticationUser> users = newArrayList<AuthenticationUser>();
  users.add(new AuthenticationUser("admin","password", "admins,publishers,consumers"));
  users.add(new AuthenticationUser("publisher","password", "publishers,consumers"));
  users.add(new AuthenticationUser("consumer","password", "consumers"));
  users.add(new AuthenticationUser("guest","password", "guests"));
  authentication.setUsers(users);
  broker.setPlugins(new BrokerPlugin[]{authentication});
  broker.addConnector("tcp://localhost:61616");
  broker.start();
  System.out.println();
  System.out.println("Press any key to stop the broker");
  System.out.println();
  System.in.read();
}


3.使用工廠方法創建BrokerService實例:

public static void main(String[] args)throws Exception {
  System.setProperty("activemq.base",System.getProperty("user.dir"));
  String configUri ="xbean:activemq.xml"
  URI brokerUri = new URI(configUri);
  BrokerService broker = BrokerFactory.createBroker(brokerUri);
  broker.start();
  System.out.println();
  System.out.println("Press any key to stop the broker");
  System.out.println();
  System.in.read();
}


4.通過Spring配置直接創建BrokerService實例

<bean id="simpleAuthPlugin“class="org.apache.activemq.security.SimpleAuthenticationPlugin">
  <property name="users">
  <util:list>
    <ref bean="admins" />
    <ref bean="publishers" />
    <ref bean="consumers" />
    <ref bean="guests" />
  </util:list>
  </property>
</bean>
<bean id="broker"class="org.apache.activemq.broker.BrokerService" init-method="start"destroy-method="stop">
  <property name="brokerName"value="myBroker" />
  <property name="persistent"value="false" />
  <propertyname="transportConnectorURIs">
    <list><value>tcp://localhost:61616</value></list>
  </property>
  <property name="plugins">
    <list><refbean="simpleAuthPlugin"/></list>
  </property>
</bean>


5.通過Spring配置使用BrokerFactoryBean創建BrokerService實例:

<bean id="broker“class="org.apache.activemq.xbean.BrokerFactoryBean">
  <property name="config"
value="classpath:activemq.xml"/>
  <property name="start"value="true" />
</bean>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章