第八章 - ActiveMQ 和java应用的结合
ActiveMQ 和java应用的结合
章节导读
- 在java应用中嵌入ActiveMQ
- ActiveMq结合Spring
- 用Spring编写JMS客户端
1.1 将ActiveMq嵌入java
首先我们使用JMS API 中的BrokerService类来配置代理,接着我们使用xml文件配置的方式来配置代理.我们通过BrokerFactory类来达到这个目的,
1.1.1 使用org.apache.activemq.broker.BrokerService
这个类用来配置代理以及管理它的生命周期,让我们用之前的认证插件的例子来距离.之前那个例子的xml配置如下:
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="myBroker" dataDirectory="${activemq.base}/data"> <transportConnectors> <transportConnector name="openwire" uri="tcp://localhost:61616" /> </transportConnectors> <plugins> <simpleAuthenticationPlugin> <users> <authenticationUser username="admin" password="password" groups="admins,publishers,consumers"/> <authenticationUser username="publisher" password="password" groups="publishers,consumers"/> <authenticationUser username="consumer" password="password" groups="consumers"/> <authenticationUser username="guest" password="password" groups="guests"/> </users> </simpleAuthenticationPlugin> </plugins> </broker>对应的java代码如下
import java.util.ArrayList; import java.util.List; import org.apache.activemq.broker.BrokerPlugin; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.security.AuthenticationUser; import org.apache.activemq.security.SimpleAuthenticationPlugin; public class ActiveMqEmbedJava { public static void main(String[] args) throws Exception { BrokerService broker = new BrokerService(); broker.setBrokerName("myBroker"); broker.setDataDirectory("data/"); SimpleAuthenticationPlugin authentication = new SimpleAuthenticationPlugin(); List<AuthenticationUser> users = new ArrayList<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(); } }
注意:必须在增加Connector之前配置插件,否则插件不会被初始化.代理启动之后再去增加Connector将不会起到预期的作用.
1.1.2 使用org.apache.activemq.broker.BrokerFactory
在某些情况下,你想用相同的配置初始化代理,就可以使用BrokerFactory.它可以使用ActiveMQ URL来创建一个代理,基于代理 URl协议,找到合适的工厂类并创建对应的BrokerService.用的最多的就是XBeanBrokerFactory,通过XBean风格的URl配置,例如:xbean:/path/to/activemq.xml
这个URl告诉BrokerFactory使用XBeanBrokerFactory以及创建代理实例的路径.BrokerFactory可以使用ActivemQ的xml配置文件来初始化BrokerService.例子如下:
public class Factory {
public static void main(String[] args) throws Exception { System.setProperty("activemq.base", System.getProperty("user.dir")); String configUri = "xbean:target/classes/org/apache/activemq/book/ch6/activemq-simple.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(); }
}
1.2 将ActiveMq嵌入Spring
1.纯Spring XMl的形式(将BrokerService 作为一个bean)
spring.xml配置文件,放置在src/main/resources目录下(maven)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <bean id="admins" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="admin" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="admins,publisher,consumers" /> </bean> <bean id="publishers" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="publisher" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="publisher,consumers" /> </bean> <bean id="consumers" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="consumer" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="consumers" /> </bean> <bean id="guests" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="guest" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="guests" /> </bean> <bean id="simpleAuthPlugin" class="org.apache.activemq.security.SimpleAuthenticationPlugin"> <property name="users"> <list> <ref bean="admins" /> <ref bean="publishers" /> <ref bean="consumers" /> <ref bean="guests" /> </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" /> <property name="transportConnectorURIs"> <list> <value>tcp://localhost:61616</value> </list> </property> <property name="plugins"> <list> <ref bean="simpleAuthPlugin"/> </list> </property> </bean> </beans>用以下代码加载spring配置文件启动ActiveMq即可
import org.springframework.context.support.ClassPathXmlApplicationContext; public class PurlSpringXml { public static void main(String[] args) { ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("classpath:/*.xml"); } }