首页 > 代码库 > activemq spring客户端
activemq spring客户端
一、相关jar包
activemq-pool、activemq-broker、activemq-client、xbean-spring(embbed的broker使用)
二、spring-activemq-provider.xml
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- activemq连接工厂 -->
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
<property name="userName">
<value>admin</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<!-- 连接池 -->
<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
</bean>
<!-- 消费发送和接收模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="pooledJmsFactory" />
</property>
</bean>
<!-- destination:queue和topic -->
<bean id="queueDest" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">
<constructor-arg value="http://www.mamicode.com/queue-test" />
</bean>
<bean id="topicDest" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor">
<constructor-arg value="http://www.mamicode.com/topic-test" />
</bean>
<!-- 业务实现类 -->
<bean id="springProvider" class="com.activemq.demo.SpringProvider">
<property name="template">
<ref local="jmsTemplate" />
</property>
<property name="destinations">
<list>
<ref local="queueDest" />
<ref local="topicDest" />
</list>
</property>
</bean>
</beans>
三、spring-activemq-consumer.xml
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- activemq连接工厂 -->
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
<property name="userName">
<value>admin</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<!-- 连接池 -->
<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
</bean>
<!-- 消费发送和接收模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="pooledJmsFactory" />
</property>
</bean>
<!-- destination:queue和topic -->
<bean id="queueDest" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">
<constructor-arg value="http://www.mamicode.com/queue-test" />
</bean>
<!-- 业务实现的监听器 -->
<bean id="msgListener" class="com.activemq.demo.SpringMessageListener" />
<!-- 消费者整合监听器 -->
<bean id="javaConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="queueDest" />
<property name="messageListener" ref="msgListener" />
</bean>
</beans>
四、相关业务实现类
public class SpringProvider
{
private JmsTemplate template;
private List<Destination> destinations;
public void sendQueue(String msg)
{
template.convertAndSend(destinations.get(0), msg);
}
public void sendTopic(String msg)
{
template.convertAndSend(destinations.get(1), msg);
}
public JmsTemplate getTemplate() {
return template;
}
public void setTemplate(JmsTemplate template) {
this.template = template;
}
public List<Destination> getDestinations() {
return destinations;
}
public void setDestinations(List<Destination> destinations) {
this.destinations = destinations;
}
}
public class SpringMessageListener implements MessageListener
{
@Override
public void onMessage(Message msg)
{
System.err.println("已经消费数据:" + msg);
}
}
五、测试
public static void main(String[] args)
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-activemq-provider.xml");
SpringProvider springProvider = context.getBean(SpringProvider.class);
springProvider.sendQueue("这就是测试");
}
public static void main(String[] args)
{
new ClassPathXmlApplicationContext("spring-activemq-consumer.xml");
}
参考地址:
http://activemq.apache.org/spring-support.html
http://docs.spring.io/spring/docs/2.5.x/reference/jms.html#jms-mdp
本文出自 “旅行者” 博客,请务必保留此出处http://881206524.blog.51cto.com/10315134/1928731
activemq spring客户端
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。