SpringMVC整合ActiveMQ
文章目录
- 一、Spring整合ActiveMQ实战
- 1.1. 创建一个父工程:
- 1.2. 引入依赖
- 1.3. 创建一个子项目生产者
- 1.4. 创建一个spring配置文件
- 4. 创建一个点对点的生产者监听
- 5. 创建一个topic的生产者监听
- 四、Spring整合ActiveMQ实战
- 1. 创建一个消费者工程
- 2. 引入依赖
- 3. 创建spring配置文件
- 4. 配置消费者监听类和测试类
- 4.1 监听类
- 4.2. 测试类
- 5. 创建生产者测试类
- 5.1. 启动生产者测试类
- 5.2. 登录后台,消息成功投递
- 5.3. 启动消费者测试类
- 5.4. 启动1个生产者,2个消费者
- 5.5. 登陆后台
- 六、topic 生产者
- 6.1. 生产者代码
- 6.2. 生产者配置文件
- 6.3. 生产者测试类
- 6.4. 消费者监听代码
- 6.5. 消费者配置文件
- 6.6. 消费者测试类
- 6.7. 启动2个消费者,1个生产者
- 6.7.1. 2个消费者
- 6.7.2. 个生产者
- 6.7.3. 登录后台
一、Spring整合ActiveMQ实战
1.1. 创建一个父工程:
1.2. 引入依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gblfy.mq</groupId><artifactId>springjms-parent</artifactId><version>1.0-SNAPSHOT</version><modules><module>springjms-producer</module></modules><packaging>pom</packaging><name>springjms-parent Maven Webapp</name><url>http://www.gblfy.com</url><properties><!--全局编码设置--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--全局maven编译版本--><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><!--全局版本控制--><spring.version>4.2.4.RELEASE</spring.version><activemq.version>5.13.4</activemq.version><junit.version>4.9</junit.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId><version>${activemq.version}</version></dependency></dependencies>
</project>
1.3. 创建一个子项目生产者
- 项目名称:springjms_producer
项目创建完成!!!
1.4. 创建一个spring配置文件
applicationContext-jms-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.gblfy.springjms"></context:component-scan><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.135:61616"/> </bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory"/> </bean> <!--这个是队列目的地,点对点的 文本信息--> <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="queue_text"/> </bean>
</beans>
4. 创建一个点对点的生产者监听
package com.gblfy.springjms.monitor.queue;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;@Component
public class QueueProducer {@Autowiredprivate JmsTemplate jmsTemplate;@Autowiredprivate Destination queueTextDestination;/*** 发送文本消息* @param text*/public void sendTextMessage(final String text){jmsTemplate.send(queueTextDestination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(text);}});}
}
5. 创建一个topic的生产者监听
package com.gblfy.springjms.monitor.topic;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;@Component
public class TopicProducer {@Autowiredprivate JmsTemplate jmsTemplate;@Autowiredprivate Destination topicTextDestination;/*** 发送文本消息* @param text*/public void sendTextMessage(final String text){jmsTemplate.send(topicTextDestination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(text);}});}
}
四、Spring整合ActiveMQ实战
1. 创建一个消费者工程
项目搭建完成!!!
2. 引入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gblfy.springjms</groupId><artifactId>springjms_consumer</artifactId><version>0.0.1-SNAPSHOT</version><properties><spring.version>4.2.4.RELEASE</spring.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId><version>5.13.4</version></dependency></dependencies> </project>
3. 创建spring配置文件
applicationContext-jms-consumer-queue.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.135:61616"/> </bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--这个是队列目的地,点对点的 文本信息--> <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="queue_text"/> </bean> <!-- 我的监听类 --><bean id="myMessageListener" class="com.gblfy.springjms.MyMessageListener"></bean><!-- 消息监听容器 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueTextDestination" /><property name="messageListener" ref="myMessageListener" /></bean></beans>
4. 配置消费者监听类和测试类
4.1 监听类
package com.gblfy.springjms;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;public class MyMessageListener implements MessageListener {public void onMessage(Message message) {TextMessage textMessage=(TextMessage)message;try {System.out.println("接收到消息:"+textMessage.getText());} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
4.2. 测试类
package com.gblfy.springjms.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.io.IOException;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-consumer-queue.xml")
public class TestQueue {@Testpublic void testQueue(){try {System.in.read();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
5. 创建生产者测试类
package com.gblfy.springjms.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.gblfy.springjms.QueueProducer;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestQueue {@Autowiredprivate QueueProducer queueProducer;@Testpublic void testSend(){queueProducer.sendTextMessage("spring JMS 点对点");}
}
验证
5.1. 启动生产者测试类
5.2. 登录后台,消息成功投递
5.3. 启动消费者测试类
5.4. 启动1个生产者,2个消费者
5.5. 登陆后台
六、topic 生产者
6.1. 生产者代码
package com.gblfy.springjms;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;@Component
public class TopicProducer {@Autowiredprivate JmsTemplate jmsTemplate;@Autowiredprivate Destination topicTextDestination;/*** 发送文本消息* @param text*/public void sendTextMessage(final String text){jmsTemplate.send(topicTextDestination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(text);}});}
}
6.2. 生产者配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.gblfy.springjms"></context:component-scan><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.43.103:61616"/></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory"/> </bean> <!--这个是订阅模式 文本信息--> <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic_text"/> </bean> </beans>
6.3. 生产者测试类
package com.gblfy.springjms.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.gblfy.springjms.TopicProducer;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestTopic {@Autowiredprivate TopicProducer topicProducer;@Testpublic void testSend(){topicProducer.sendTextMessage("spring JMS 发布订阅");}
}
6.4. 消费者监听代码
package com.gblfy.springjms;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;public class MyMessageListener implements MessageListener {public void onMessage(Message message) {TextMessage textMessage=(TextMessage)message;try {System.out.println("接收到消息:"+textMessage.getText());} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
6.5. 消费者配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.43.103:61616"/></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!--这个是队列目的地,点对点的 文本信息--> <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic_text"/> </bean> <!-- 我的监听类 --><bean id="myMessageListener" class="com.gblfy.springjms.MyMessageListener"></bean><!-- 消息监听容器 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="topicTextDestination" /><property name="messageListener" ref="myMessageListener" /></bean></beans>
6.6. 消费者测试类
package com.gblfy.springjms.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.io.IOException;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-consumer-topic.xml")
public class TestTopic {@Testpublic void testTopic(){try {System.in.read();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
6.7. 启动2个消费者,1个生产者
6.7.1. 2个消费者