1. 程式人生 > >activemq spring客戶端

activemq spring客戶端

activemq

一、相關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="queue-test" />
</bean>
<bean id="topicDest" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor">
<constructor-arg value="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="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客戶端