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) {


    }

}

項目啓動後 控制檯會打印出監聽的日誌信息 這裏寫圖片描述

結尾:僅供參考,自己用作學習記錄,不喜勿噴,共勉!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章