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();
}
接下来笔者详细带大家学习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();
}
相关文章推荐
- 消息中间件-activemq实战之整合Spring(四)
- ActiveMQ安装与Spring整合
- spring整合activemq发送MQ消息[Topic模式]实例,activemqmq
- Spring整合JMS——基于ActiveMQ实现(一)
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- Spring整合activeMQ
- Spring整合ActiveMQ
- springboot与ActiveMQ整合
- ActiveMQ(四):Spring ActiveMQ 整合
- Spring整合ActiveMQ 实现消息服务
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- Spring整合activeMq(二):发布订阅模式
- Spring2.5整合ActiveMQ 5.2(P2P文本消息)
- Spring整合ActiveMQ
- Spring整合JMS(一)——基于ActiveMQ实现
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- spring 整合 ActiveMQ
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- Spring整合activeMQ
- spring整合JMS实现同步收发消息(基于ActiveMQ的实现)