您的位置:首页 > 编程语言 > Java开发

ActiveMQ与Spring的整合

2018-02-12 23:00 323 查看
ActiveMQ是遵循jms规范编写的,所以单独使用ActiveMQ时,步骤稍微有点繁琐,但又比较死板。但ActiveMQ可以与spring完美整合,整合后,ActiveMQ的使用就相当方便了。目前企业开发绝大多数用到spring,所以ActiveMQ与Spring的整合过程有必要学习。
接下来笔者详细带大家学习ActiveMQ与Spring的整合过程。(笔者建议读者先阅读笔者之前写过文章,了解ActiveMQ的基础
首先在作为生产者(发送消息)的工程spring配置文件(笔者这里配置文件名为applicationContext-activemq.xml)添加如下配置
1.当spring容易初始化时,得到connectionFactory <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.131:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>2.配置生产者<!-- 配置生产者 -->
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory" />
</bean>3.得到目的Destination对象
一种是queue点对点,一种是topic发布/订阅<!--这个是队列目的地,点对点的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--这个是主题目的地,一对多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>通过以上三个步骤,生产者的配置就已经配置完成了啦,接下来,写java测试代码来发送消息@Test
public void springMQTest() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:spring/applicationContext-activemq.xml");
JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
Destination destination = (Destination) applicationContext.getBean("queueDestination");

jmsTemplate.send(destination, new MessageCreator() {

@Override
public Message createMessage(Session session) throws JMSException {

return session.createTextMessage("send activemq message");
}
});
}生产者发送完消息已经搞定,接下来我们来看看消费者的配置以及java代码的书写
在接受消息的工程的spring配置文件中添加如下配置
1.得到connectionFactory<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.131:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>2.配置目的对象<!--这个是队列目的地,点对点的 -->
<bean id="queueDestination" class="org.apache.acti
a665
vemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--这个是主题目的地,一对多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>3.配置监听器
配置监听器需要我们手写一个类实现MessageListener接口来实现监听操作,java代码如下public class MyMessageListener implements MessageListener {

@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}接下就可以配置监听器了<!-- 配置监听器 -->
<bean id="myMessageListener" class="MyMessageListener的权限路径" />4.配置消息监听容器<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="myMessageListener" />
</bean>最后我们写一个消费者测试类接受消息@Test
public void MessageConcustomer() throws Exception {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:spring/applicationContext-activemq.xml");
System.in.read();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: