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>