读书人

应用ActiveMQ创建Java应用程序

发布时间: 2012-07-20 10:38:30 作者: rapoo

使用ActiveMQ创建Java应用程序

1. 将ActiceMQ嵌入至Java应用程序中

??? (1) 嵌入ActiveMQ使用BrokerService

??? public static void main(String[] args) throws Exception {

??????? //实例化并且配置BrokerService
??? ??? 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
??? ??? broker.start();
??? ???
??? ??? System.out.println();
??? ??? System.out.println("Press any key to stop the broker");
??? ??? System.out.println();
??? ???
??? ??? System.in.read();
??? }

??? 说明:在使用BrokerService进行Broker配置时,需要先将plug-ins添加至broker,后添加connector;否则

??? plug-ins是不能被初始化的。

??? (2) 嵌入ActiveMQ使用BrokerFactory

??? public static void main(String[] args) throws Exception {
??????? System.setProperty("activemq.base", System.getProperty("user.dir"));
??????? BrokerService broker = BrokerFactory.createBroker(
??????????????? new URI("xbean:target/classes/org/apache/activemq/book/ch7/activemq-simple.xml"));
??????? broker.start();

??????? System.out.println();
??????? System.out.println("Press any key to stop the broker");
??????? System.out.println();

??????? System.in.read();
??? }

??? 说明:配置中的xbean URI命名空间是用来告诉BrokerFactory去ClassPath 或文件系统中查找给定的配置文

??? 件,activemq-simple.xml:

??? <beans xmlns="http://www.springframework.org/schema/beans"
??? xmlns:amq="http://activemq.apache.org/schema/core"
??? 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.apache.org/schema/core http://activemq.apache.org/schema/core

??? /activemq-core.xsd
??? http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema

??? /spring/camel-spring.xsd">

??? <!-- Allows us to use system properties as variables in this configuration file -->
??? <bean
??? ?????? />

??? <broker xmlns="http://activemq.apache.org/schema/core"
??? ?????? brokerName="localhost" dataDirectory="${activemq.base}/data">

?????????? <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>
???????
??? ?????? <!-- The transport connectors ActiveMQ will listen to -->
??? ?????? <transportConnectors>
??? ??? ?????????? <transportConnector name="openwire" uri="tcp://localhost:61616" />
??? ?????? </transportConnectors>
??????? </broker>

??? </beans>

2. 将ActiveMQ嵌入至Spring应用中

??? (1) 使用纯粹的SpringXML配置

??? (2) 使用BrokerFactoryBean配置:

??? <beans>
??????? <bean id="broker" />
??? ????????? <property name="start" value="true" />
??????? </bean>
??? </beans>

??? 启动ActivaMQ:

??? public static void main(String[] args) throws Exception {
??? ??? if (args.length == 0) {
??? ??? ??? System.err.println("Please define a configuration file!");
??? ??? ??? return;
??? ??? }
??? ??? String config = args[0];
??? ??? System.out.println("Starting broker with the following configuration: " + config);
??? ??? System.setProperty("activemq.base", System.getProperty("user.dir"));
??? ??? FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(config);
??? ???
??? ??? Publisher publisher = new Publisher();
??? ??? for (int i = 0; i < 100; i++) {
??? ??? ??? publisher.sendMessage(new String[]{"JAVA", "IONA"});
??? ??? }??? ???
??? }

??? (3) 使用自定义的XML命名空间with Spring

??? <beans xmlns="http://www.springframework.org/schema/beans"
??? xmlns:amq="http://activemq.apache.org/schema/core"
??? 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.apache.org/schema/core http://activemq.apache.org/schema/core

??? /activemq-core.xsd">

??? <amq:broker
??? ???? brokerName="localhost" dataDirectory="${activemq.base}/data">

??? ???? <!-- The transport connectors ActiveMQ will listen to -->
??? ???? <amq:transportConnectors>
??? ??? ???? <amq:transportConnector name="openwire"? uri="tcp://localhost:61616" />
??? ???? </amq:transportConnectors>
??? ???? <amq:plugins>
??? ??? ????? <amq:simpleAuthenticationPlugin>
??? ??? ??? ???? <amq:users>
??? ??? ??? ??? ??? <amq:authenticationUser username="admin" password="password"

???????????????????????? groups="admins,publishers,consumers"/>
??? ??? ??? ??? ??? <amq:authenticationUser username="publisher" password="password"

???????????????????????? groups="publishers,consumers"/>
??? ??? ??? ??? ??? <amq:authenticationUser username="consumer" password="password"

???????????????????????? groups="consumers"/>
??? ??? ??? ??? ??? <amq:authenticationUser username="guest" password="password" groups="guests"/>
??? ??? ??? ???? </amq:users>
??? ??? ????? </amq:simpleAuthenticationPlugin>
??? ????? </amq:plugins>
?????? </amq:broker>
??? </beans>

3. 使用JMS实现request/reply

4. 使用Spring对JMS的封装编写JMS Client

读书人网 >软件架构设计

热点推荐