不为有趣之事,何遣有涯之生
不失其所者久,死而不亡者寿

AMQ简明教程(6) ActiveMQ构建应用

ActiveMQ构建应用

Broker介绍

Broker:相当于一个ActiveMQ服务器实例
命令行启动参数示例如下:
1:activemq start :使用默认的activemq.xml来启动
2:activemq start xbean:file:../conf/activemq-2.xml :使用指定的配置文件来启动
3:如果不指定file,也就是xbean:activemq-2.xml,那么xml必须在classpath下面

用ActiveMQ来构建Java应用
这里主要将用ActiveMQ Broker作为独立的消息服务器来构建JAVA应用。
ActiveMQ也支持在vm中通信基于嵌入式的broker,能够无缝的集成其它java应用

嵌入式Broker启动

1:Broker Service启动broker ,示例如下:

BrokerService broker = new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();

2:BrokerFactory启动broker ,示例如下:

String Uri = "properties:broker.properties";
BrokerService broker1 = BrokerFactory.createBroker(new URI(Uri));
broker1.addConnector("tcp://localhost:61616");
broker1.start();

broker.properties的内容示例如下:

useJmx=true
persistent=false
brokerName=Cheese 

利用Spring集成Broker

Spring的配置文件如下:

<beans>
<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">
<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" />
<property name="transportConnectorURIs">
<list>
<value>tcp://localhost:61616</value>
</list>
</property>
<property name="plugins">
<list>
<ref bean="simpleAuthPlugin"/>
</list>
</property>
</bean>
</beans>

或者配置BrokerFactoryBean,示例如下:

<beans>
<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
<property name="config" value="resources/activemq-simple.xml"/>
<property name="start" value="true" />
</bean>
</beans>

ActiveMQ的启动:

1:可以通过在应用程序中以编码的方式启动broker,例如: broker.start();
如果需要启动多个broker,那么需要为broker设置一个名字。例如:

BrokerService broker = new BrokerService();
broker.setName("fred");
broker.addConnector("tcp://localhost:61616");
broker.start();

2:还可以通过spring来启动,前面已经演示过了

未经允许不得转载:菡萏如佳人 » AMQ简明教程(6)

欢迎加入极客江湖

进入江湖关于作者