SpringMVC和rabbitmq集成使用

1.添加maven依赖
<dependency>
  <groupId>com.rabbitmq</groupId>
  <artifactId>amqp-client</artifactId>
  <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>1.4.5.RELEASE</version>
</dependency>

2.spring主配置文件中加入rabbitMQ  xml文件的配置
<!-- rabbitMQ  配置   -->
 <import resource="/application-mq.xml"/>

3.jdbc配置文件中加入 rabbitmq的链接配置
#rabbitMQ配置
mq.host=localhost
mq.username=donghao
mq.password=donghao
mq.port=5672
mq.vhost=testMQ

4.新建application-mq.xml文件,添加配置信息
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rabbit="http://www.springframework.org/schema/rabbit" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" >

    <description>rabbitmq 连接服务配置</description>

    <!-- 连接配置 -->
    <rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}"  virtual-host="${mq.vhost}"/>
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- spring template声明-->
    <rabbit:template exchange="koms" id="amqpTemplate"  connection-factory="connectionFactory"  message-converter="jsonMessageConverter" />

    <!-- 消息对象json转换类 -->
    <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />  
    <!-- 
        durable:是否持久化

        exclusive: 仅创建者可以使用的私有队列,断开后自动删除

        auto_delete: 当所有消费客户端连接断开后,是否自动删除队列
     -->

     <!--  申明一个消息队列Queue   -->
    <rabbit:queue id="order" name="order" durable="true" auto-delete="false" exclusive="false" />
     <rabbit:queue id="activity" name="activity" durable="true" auto-delete="false" exclusive="false" />
     <rabbit:queue id="mail" name="mail" durable="true" auto-delete="false" exclusive="false" />
     <rabbit:queue id="stock" name="stock" durable="true" auto-delete="false" exclusive="false" />
     <rabbit:queue id="autoPrint" name="autoPrint" durable="true" auto-delete="false" exclusive="false" />
    <!--
     rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。 

    rabbit:binding:设置消息queue匹配的key
     -->
    <!-- 交换机定义 -->
    <rabbit:direct-exchange name="koms" durable="true" auto-delete="false" id="koms">
    <rabbit:bindings>
        <rabbit:binding queue="order" key="order"/>
         <rabbit:binding queue="activity" key="activity"/>
         <rabbit:binding queue="mail" key="mail"/>
         <rabbit:binding queue="stock" key="stock"/>
         <rabbit:binding queue="autoPrint" key="autoPrint"/>
    </rabbit:bindings>
</rabbit:direct-exchange>

    <!--
         queues:监听的队列,多个的话用逗号(,)分隔 

        ref:监听器
     -->
    <!-- 配置监听  acknowledeg = "manual"   设置手动应答  当消息处理失败时:会一直重发  直到消息处理成功 -->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
    <!-- 配置监听器 -->
        <rabbit:listener queues="activity" ref="activityListener"/>
         <rabbit:listener queues="order" ref="orderListener"/>
        <rabbit:listener queues="mail" ref="mailListener"/>
        <rabbit:listener queues="stock" ref="stockListener"/>
        <rabbit:listener queues="autoPrint" ref="autoPrintListener"/>
    </rabbit:listener-container>
</beans>

5.新增公共入队类

@Service
public class MQProducerImpl{
@Resource
    private AmqpTemplate amqpTemplate;

    private final static Logger logger = LoggerFactory.getLogger(MQProducerImpl.class);

    //公共入队方法
    public void sendDataToQueue(String queueKey, Object object) {
        try {
            amqpTemplate.convertAndSend(queueKey, object);
        } catch (Exception e) {
            logger.error(e.toString());
        }

    }
}

6.创建监听类
这里写图片描述


import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.cn.framework.domain.BaseDto;
import com.cn.framework.util.ConstantUtils;
import com.cn.framework.util.RabbitMq.producer.MQProducer;
import com.kxs.service.activityService.IActivityService;
import com.kxs.service.messageService.IMessageService;
import com.rabbitmq.client.Channel;

/**
 * 活动处理listener
* @author
* @date 2017年6月30日
**/
@Component
public class ActivityListener implements ChannelAwareMessageListener {
    private static final  Logger log =  LoggerFactory.getLogger(ActivityListener.class);


    @Override
    @Transactional
    public void onMessage(Message message,Channel channel) {


    }

}

项目启动后 控制台会打印出监听的日志信息 这里写图片描述

结尾:仅供参考,自己用作学习记录,不喜勿喷,共勉!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值