1.maven依賴
<!-- Active MQ 開始 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
</dependency>
<!-- Active MQ 結束 -->
2.生產者配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- spring整合JMS 生產者 -->
<!-- 配置JMS服務提供商 ActiveMQ -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- 連接ActiveMQ服務地址 -->
<property name="brokerURL" value="tcp://192.168.200.128:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!-- 配置ActiveMQ的連接池工廠 -->
<bean id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
<property name="connectionFactory" ref="activeMQConnectionFactory"/>
</bean>
<!-- spring管理JMS(ActiveMQ) -->
<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="pooledConnectionFactoryBean"/>
</bean>
<!-- spring管理jsmTemplate用於發送消息值ActiveMQ服務 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 指定ActiveMQ連接工廠 -->
<property name="connectionFactory" ref="singleConnectionFactory"/>
<!-- 指定消息隊列名稱 -->
<property name="defaultDestinationName" value="productIds"/>
</bean>
</beans>
3.生產者使用方式
@Resource
private JmsTemplate jmsTemplate;
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(“消息1”);
return textMessage;
}
});
4.消費者mq消息監聽器類
package com.kenick.sport.solr.mq;
import org.apache.activemq.command.ActiveMQTextMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
*
* @ClassName: CustomMessageListener
* @Company: http://www.itcast.cn/
* @Description: activemq消費者---監聽容器中的消息並且進行消費
* @author JD
* @date 2016年12月14日 下午3:51:11
*/
public class CustomMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
// 該消息就在message中---取出消息並且消費
ActiveMQTextMessage activeMQTextMessage = (ActiveMQTextMessage) message;
try {
// 獲取消息
String msg = activeMQTextMessage.getText();
// 進行業務邏輯處理 ...
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.消費者配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- spring整合JMS 消費者 -->
<!-- 配置JMS服務提供商 ActiveMQ -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- 連接ActiveMQ服務地址 -->
<property name="brokerURL" value="tcp://192.168.200.128:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!-- 配置ActiveMQ的連接池工廠 -->
<bean id="pooledConnectionFactoryBean" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
<property name="connectionFactory" ref="activeMQConnectionFactory"/>
</bean>
<!-- spring管理JMS(ActiveMQ) -->
<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="pooledConnectionFactoryBean"/>
</bean>
<!-- 自定義消息監聽器,處理隊列中的消息 -->
<bean id="customMessageListener" class="com.kenick.sport.solr.mq.CustomMessageListener"/>
<bean id="defaultMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!-- 注入連接工廠 -->
<property name="connectionFactory" ref="singleConnectionFactory"/>
<!-- 注入自定義監聽器,處理隊列中消息 -->
<property name="messageListener" ref="customMessageListener"/>
<!-- 指定隊列名稱 -->
<property name="destinationName" value="productIds"/>
</bean>
</beans>