ActiveMQ与Spring整合-MessageListener
消费者,使用监听的实现方式。
1. pom.xml
2. 生产者
- package org.ygy.mq.lesson04;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- /**
- *
- * @author 于贵洋
- * @description 生产者
- * @time 2013-10-14
- * @version V1.0
- */
- public class Lesson4Producer {
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- //使用JMSTemplate发送消息
- public void send(final String msg) {
- jmsTemplate.send(new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- return session.createTextMessage(msg);
- }
- });
- }
- }
3. 消费者
- package org.ygy.mq.lesson04;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageListener;
- import javax.jms.TextMessage;
- /**
- *
- * @author 于贵洋
- * @description 通过监听的方式实现消费者
- * @time 2013-10-14
- * @version V1.0
- */
- public class Lesson4Consumer implements MessageListener {
- @Override
- public void onMessage(Message message) {
- TextMessage textMsg = (TextMessage) message;
- try {
- System.out.println("接收到了消息,消息内容是:" + textMsg.getText());
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
4. 配置文件
- <?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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <!-- 1.配置connectionFactory -->
- <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
- <property name="connectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL">
- <value>tcp://127.0.0.1:61616</value>
- </property>
- </bean>
- </property>
- <property name="maxConnections" value="100"></property>
- </bean>
- <!-- 2.定义消息目标 -->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg index="0" value="queue_temp"></constructor-arg>
- </bean>
- <!-- 3.配置Spring JMS Template -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <!-- 3.1 注入jmsFactory -->
- <property name="connectionFactory" ref="jmsFactory"></property>
- <!-- 3.2 配置消息目标 -->
- <property name="defaultDestination" ref="destination" />
- </bean>
- <!-- 4.配置生产者 -->
- <bean id="lesson4Producer" class="org.ygy.mq.lesson04.Lesson4Producer">
- <property name="jmsTemplate" ref="jmsTemplate"/>
- </bean>
- <!-- 5.配置消费者 -->
- <bean id="lesson4Consumer" class="org.ygy.mq.lesson04.Lesson4Consumer"></bean>
- <!-- 消息监听容器 -->
- <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
- <property name="connectionFactory" ref="jmsFactory" />
- <property name="destination" ref="destination" />
- <property name="messageListener" ref="lesson4Consumer" />
- </bean>
- </beans>
配置文件的话,重点关注消费者的配置,将其配置在一个消息监听容器中。
这里的监听容器有多个,明天会分享或者,直接转载一些文章。
5. 测试
- package org.ygy.mq.lesson04;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * 配置了监听容器
- *
- * 参考:http://haohaoxuexi.iteye.com/blog/1893038
- * @author Administrator
- *
- */
- public class Client4 {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("lesson4.xml");
- Lesson4Producer producer = (Lesson4Producer) applicationContext.getBean("lesson4Producer");
- producer.send("你在哪里啊?");
- producer.send("嗨,你好吗?");
- }
- }