利用Spring與ActiveMQ整合發送、接收消息實例,同時使用Queue與Topic兩種模式。
1.運行環境:Win10+Eclipse Java EE IDE Oxygen Release (4.7.0)+jdk1.8
2.web-inf/lib 中依賴jar包:activemq-all-5.15.0.jar
spring-aspects-4.1.0.RELEASE.jar
spring-beans-4.2.2.RELEASE.jar
spring-context-4.1.0.RELEASE.jar
spring-context-support-4.1.0.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
spring-messaging-4.1.0.RELEASE.jar
3.向topic發送消息的生產者TopicSender代碼:
public class TopicSender {
@Resource
private JmsTemplate jmsTopicTemplate;
//發送消息
public void sendMessage(Destination destination,final String message) {
System.out.println("TopicSender發送消息:"+message);
jmsTopicTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
return session.createTextMessage(message);
}
});
}
}
4.向queue發送消息的生產者QueueSender代碼:
public class QueueSender {
@Resource
private JmsTemplate jmsQueueTemplate;
//發送消息
public void sendMessage(Destination destination,final String message) {
System.out.println("QueueSender發送消息:"+message);
jmsQueueTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
return session.createTextMessage(message);
}
});
}
}
5.接收topic消息的消費者1:
public class TopicReceiver1 implements MessageListener {
/*
* (非 Javadoc) <p>Title: onMessage</p> <p>Description: </p>
*
* @param arg0
*
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("TopicReceiver1接收到消息內容是:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6.接收topic消息的消費者2:
public class TopicReceiver2 implements MessageListener {
/*
* (非 Javadoc) <p>Title: onMessage</p> <p>Description: </p>
*
* @param arg0
*
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("TopicReceiver2接收到消息內容是:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
7.接收queue消息的消費者1:
public class QueueReceiver1 implements MessageListener {
/*
* (非 Javadoc) <p>Title: onMessage</p> <p>Description: </p>
*
* @param arg0
*
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("QueueReceiver1接收到消息內容是:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
8.接收queue消息的消費者2:
public class QueueReceiver2 implements MessageListener {
/*
* (非 Javadoc) <p>Title: onMessage</p> <p>Description: </p>
*
* @param arg0
*
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("QueueReceiver2接收到消息內容是:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
9.控制生產者產生消息的控制器:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class ActivemqController {
@Resource
private QueueSender queueSender;
@Resource
private TopicSender topicSender;
@Resource
@Qualifier("queueDestination")
private Destination queueDestination;
@Resource
@Qualifier("topicDestination")
private Destination topicDestination;
@Test
public void testSend() {
for (int i = 0; i < 5; i++) {
queueSender.sendMessage(queueDestination, "queue生產者產生消息:" + (i + 1));
}
for (int i = 0; i < 5; i++) {
topicSender.sendMessage(topicDestination, "topic生產者產生消息:" + (i + 1));
}
}
}
10.web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>ActiveMQANDSpring</display-name>
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:ActiveMQ.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
11.applicationContext.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:oxm="http://www.springframework.org/schema/oxm"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.sgcc.SpringActivemq" />
<import resource="ActiveMQ.xml" />
</beans>
12.ActiveMQ.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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
<!-- 配置connectionFactory -->
<!-- ActiveMQ連接工廠 -->
<!-- 由對應的 JMS服務廠商提供的ConnectionFactory -->
<!-- 連接URL以及用戶名,密碼 -->
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" />
<!-- Spring Caching連接工廠 -->
<!-- Spring用於管理真正的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="amqConnectionFactory" />
<!-- session緩存數量 -->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- Spring JMS Template -->
<!-- 生產者 -->
<!-- Queue類型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 這個connectionFactory對應的是我們定義的Spring提供的那個ConnectionFactory對象 -->
<constructor-arg ref="connectionFactory"></constructor-arg>
<!-- 非pub/sub模型(發佈/訂閱),即隊列模式 -->
<property name="pubSubDomain" value="false" />
</bean>
<bean id="queueSender" class="com.sgcc.SpringActivemq.mq.producer.queue.QueueSender"></bean>
<bean id="topicSender" class="com.sgcc.SpringActivemq.mq.producer.topic.TopicSender"></bean>
<!-- 定義JmsTemplate的Topic類型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 這個connectionFactory對應的是我們定義的Spring提供的那個ConnectionFactory對象 -->
<constructor-arg ref="connectionFactory" />
<!-- pub/sub模型(發佈/訂閱) -->
<property name="pubSubDomain" value="true" />
</bean>
<!--Spring JmsTemplate 的消息生產者 end -->
<!-- 消息監聽器 -->
<bean id="queueReceiver1"
class="com.sgcc.SpringActivemq.mq.consumer.queue.QueueReceiver1"></bean>
<bean id="queueReceiver2"
class="com.sgcc.SpringActivemq.mq.consumer.queue.QueueReceiver2"></bean>
<bean id="topicReceiver1"
class="com.sgcc.SpringActivemq.mq.consumer.topic.TopicReceiver1"></bean>
<bean id="topicReceiver2"
class="com.sgcc.SpringActivemq.mq.consumer.topic.TopicReceiver2"></bean>
<!-- 消息消費者 start -->
<!-- 定義Queue監聽器容器 -->
<jms:listener-container destination-type="queue"
container-type="default" connection-factory="connectionFactory"
acknowledge="auto">
<jms:listener destination="testqueue" ref="queueReceiver1" />
<jms:listener destination="testqueue" ref="queueReceiver2" />
</jms:listener-container>
<!-- 定義Topic監聽器容器 -->
<jms:listener-container destination-type="topic"
container-type="default" connection-factory="connectionFactory"
acknowledge="auto">
<jms:listener destination="topic" ref="topicReceiver1" />
<jms:listener destination="topic" ref="topicReceiver2" />
</jms:listener-container>
<!-- 消息消費者 end -->
<!-- 定義隊列目的地 ,點對點 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
<!-- 定義主題目的地 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg>
<value>topic</value>
</constructor-arg>
</bean>
</beans>
程序配置好之後run as -> JUnit Test 可以看到運行結果:
可以看出queue的消息只能被消費一次,topic的消息可以由多個訂閱的消費者消費。