SpringMVC和rabbitmq集成使用

1.添加maven依赖

  com.rabbitmq
  amqp-client
  3.5.1


    org.springframework.amqp
    spring-rabbit
    1.4.5.RELEASE


2.spring主配置文件中加入rabbitMQ  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"/>

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

    
    <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />  
    

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

    
    
    <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.创建监听类
SpringMVC和rabbitmq集成使用_第1张图片


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


    }

}

项目启动后 控制台会打印出监听的日志信息 SpringMVC和rabbitmq集成使用_第2张图片

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

你可能感兴趣的:(MQ)