activemq配置。

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值