ActiveMQ与业务系统的集成

ActiveMQ是一款基于Java的开源消息服务器产品,所以,咱们能够将其集成到经过Java实现的业务系统中。下面将对集成方法作一个简单的总结。java

首先,咱们看一下ActiveMQ中的部分核心类:apache

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协议实现集成:tcp

客户端链接ActiveMQ消息服务器时,能够使用多种协议(TCPVMSTOMP等),其中VM协议是指客户端会从当前JVM中查找ActiveMQ消息服务器实例,若查找到,则与其创建链接,若查找不到,则自动建立ActiveMQ消息服务器实例。若是客户端和服务器端都在一个JVM中,则能够经过VM协议实现ActiveMQ消息服务器的自动建立、与业务系统的集成。ide


2.直接建立BrokerService实例:spa

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实例:xml

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实例blog

<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>