Rabbit整合spring(ssm)开发生产者

Rabbit整合spring(ssm)开发生产者

本文做RabbitMQ和spring(ssm)整合是在原有的ssm项目框架的基础之上进行的,相关ssm的整合在这里我就不做详细的讲解,直接整合RabbitMQ

1.收先加入整合RabbitMQ所需要的jar包,如果使用maven管理项目则添加jar包依赖

        <!--rabbitmq依赖 -->  
        <dependency>  
            <groupId>org.springframework.amqp</groupId>  
            <artifactId>spring-rabbit</artifactId>  
            <version>1.3.5.RELEASE</version>  
        </dependency>

 

2.开发rabbitMQ.properties和rabbitmq.xml并将rabbitmq.xml引入到spring主配置文件中去

rabbitMQ.properties

mq.host=10.199.203.161
mq.username=crs
mq.password=crs
mq.port=5672
#\u4e0b\u5355\u961f\u5217
mq.insertOrderQueueName=insertOrderQueue
mq.insertOrderQueueKey=551B3D4A88C47EDAE0530100007FC2EA
#\u652f\u4ed8\u8ba2\u5355\u961f\u5217
mq.orderPayQueueName=orderPayQueue
mq.orderPayQueueKey=f6da59e9b9434f25b4800b3363dec3e4

 

rabbitmq.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" >

     <!--配置connection-factory,指定连接rabbit server参数 -->
    <rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}"/>
    
    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->  
    <rabbit:admin id="connectAdmin" connection-factory="connectionFactory"/>  
    
    <!-- 设置Ack模式为手动 -->    
    <bean id="ackManual"
        class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField"
            value="org.springframework.amqp.core.AcknowledgeMode.MANUAL" />
    </bean>
    
    <!--定义queue -->  
    <!-- durable:是否持久化
        exclusive: 仅创建者可以使用的私有队列,断开后自动删除
        auto_delete: 当所有消费客户端连接断开后,是否自动删除队列 -->

      <rabbit:queue name="${mq.insertOrderQueueName}" durable="true" auto-delete="false" exclusive="false"/>
      <rabbit:queue name="${mq.orderPayQueueName}" durable="true" auto-delete="false" exclusive="false"/>

  
    <!-- 定义direct exchange,绑定queueTest -->  
    <!-- 交换机的四种模式:
        direct:转发消息到 routigKey 指定的队列。
        topic:按规则转发消息(最灵活)。
        headers:(这个还没有接触到)
        fanout:转发消息到所有绑定队列 -->
    <rabbit:direct-exchange name="amqpExchange" durable="true" auto-delete="false">  
           <!-- 设置消息queue匹配的key -->
        <rabbit:bindings>

                <rabbit:binding queue="${mq.insertOrderQueueName}" key="${mq.insertOrderQueueKey}"></rabbit:binding>
                <rabbit:binding queue="${mq.orderPayQueueName}" key="${mq.orderPayQueueKey}"></rabbit:binding>

 
        </rabbit:bindings>  
    </rabbit:direct-exchange>  
  
     <!-- 消息对象json转换类 -->
    <!-- <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
     -->
    
    <!--定义rabbit template用于数据的接收和发送 -->  
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"  
                     exchange="amqpExchange"/> 
      
</beans>  

spring-config.xml

<import resource="rabbitmq.xml" />

3.发送消息

 //MQ发送消息
 PropertyUtil propertyUtil = new PropertyUtil();
 String queueKey=propertyUtil.getProperty("/rabbitMQ.properties", "mq.queueKey");
 mqProducer.sendDataToQueue(queueKey, JSONObject.toJSONString(order.getMqOrderInfo()));

MqProducterImpl.java

package com.wanda.crs.standard.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wanda.crs.common.BaseException;
import com.wanda.crs.standard.service.MQProducer;
import com.wanda.crs.utils.Contants;
@Service("mqProducer")
public class MQProducerImpl implements MQProducer {
    
    private static final Logger logger = LoggerFactory.getLogger(MQProducerImpl.class);
    
    @Autowired
    private AmqpTemplate amqpTemplate;
    
    @Override
    public void sendDataToQueue(String queueKey, Object object) throws Exception {
        try {
            amqpTemplate.convertAndSend(queueKey, object);
        } catch (Exception e) {
            logger.info(e.getLocalizedMessage());
            throw new Exception(Contants.MSG_MQ_SEND_ERROR);
        }
    }

}

 

MessageProducer.java文件(两种都可以)

package com.crs.ticket.utils.rubbitmq;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 功能概要:消息产生,提交到队列中去
 * 
 * @author pypua
 * @since  2017年7月16日 
 */
@Service
public class MessageProducer {
    
    private Logger logger = LoggerFactory.getLogger(MessageProducer.class);
@Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(Object message){
      logger.info("to send message:{}",message);
      rabbitTemplate.convertAndSend("queuemqKey",message);
    }
}

 

posted @ 2017-07-17 16:22  十月围城小童鞋  阅读(1409)  评论(0编辑  收藏  举报