1.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"/> <rabbit:queue name="${mq.orderCancleQueueName}" durable="true" auto-delete="false" exclusive="false"/> <rabbit:queue name="${mq.orderUpdateDateQueueName}" durable="true" auto-delete="false" exclusive="false"/> <rabbit:queue name="${mq.insertCardOrderQueueName}" durable="true" auto-delete="false" exclusive="false"/> <rabbit:queue name="${mq.insertHotelOrderQueueName}" 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:binding queue="${mq.orderCancleQueueName}" key="${mq.orderCancleQueueKey}"></rabbit:binding> <rabbit:binding queue="${mq.orderUpdateDateQueueName}" key="${mq.orderUpdateDateQueueKey}"></rabbit:binding> <rabbit:binding queue="${mq.insertCardOrderQueueName}" key="${mq.insertCardOrderQueueKey}"></rabbit:binding> <rabbit:binding queue="${mq.insertHotelOrderQueueName}" key="${mq.insertHotelOrderQueueKey}"></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"/> <!-- 消息接收者1:下单 --> <bean id="messageReceiverForInsertOrder" class="com.wanda.crs.standard.service.impl.MCInsertOrderServiceImpl"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener queues="${mq.insertOrderQueueName}" ref="messageReceiverForInsertOrder"/> </rabbit:listener-container> <!-- 消息接收者2:订单支付 --> <bean id="messageReceiverForOrderPay" class="com.wanda.crs.standard.service.impl.MCOrderPayServiceImpl"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener queues="${mq.orderPayQueueName}" ref="messageReceiverForOrderPay"/> </rabbit:listener-container> <!-- 消息接收者3:取消订单 --> <bean id="messageReceiverForOrderCancle" class="com.wanda.crs.standard.service.impl.MCOrderCancleServiceImpl"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener queues="${mq.orderCancleQueueName}" ref="messageReceiverForOrderCancle"/> </rabbit:listener-container> <!-- 消息接收者4:订单改期 --> <bean id="messageReceiverForOrderUpdateDate" class="com.wanda.crs.standard.service.impl.MCOrderUpdateDateServiceImpl"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener queues="${mq.orderUpdateDateQueueName}" ref="messageReceiverForOrderUpdateDate"/> </rabbit:listener-container> <!-- 消息接收者5:卡下单 --> <bean id="messageReceiverForInsertCardOrder" class="com.wanda.crs.standard.service.impl.MCInsertCardOrderServiceImpl"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener queues="${mq.insertCardOrderQueueName}" ref="messageReceiverForInsertCardOrder"/> </rabbit:listener-container> <!-- 消息接收者6:酒店套餐下单 --> <bean id="messageReceiverForInsertHotelOrder" class="com.wanda.crs.standard.service.impl.MCInsertHotelOrderServiceImpl"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener queues="${mq.insertHotelOrderQueueName}" ref="messageReceiverForInsertHotelOrder"/> </rabbit:listener-container> </beans>
2.MessageConsumerServiceImpl.java
package com.wanda.crs.standard.service.impl; import java.util.List; import javax.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; import com.rabbitmq.client.Channel; import com.wanda.crs.standard.entity.Order; import com.wanda.crs.standard.entity.OrderChild; import com.wanda.crs.standard.entity.OrderChilds; import com.wanda.crs.standard.entity.OrderContact; import com.wanda.crs.standard.entity.OrderDetail; import com.wanda.crs.standard.entity.OrderMerchant; import com.wanda.crs.standard.entity.TempChannel; import com.wanda.crs.standard.mapper.OrderChildMapper; import com.wanda.crs.standard.mapper.OrderChildsMapper; import com.wanda.crs.standard.mapper.OrderContactMapper; import com.wanda.crs.standard.mapper.OrderDetailMapper; import com.wanda.crs.standard.mapper.OrderMapper; import com.wanda.crs.standard.mapper.OrderMerchantMapper; import com.wanda.crs.standard.mapper.TempChannelMapper; import com.wanda.crs.standard.model.MQOrderInfo; import com.wanda.crs.utils.jotmutil.CustomerContextHolder; /** * @author hcsu * 2017年8月11日 下午2:50:37 * MQ消费者-下单 */ public class MessageConsumerServiceImpl implements ChannelAwareMessageListener{ private static final Logger logger = LoggerFactory.getLogger(MessageConsumerServiceImpl.class); @Autowired private OrderContactMapper orderContactMapper; @Autowired private OrderMapper orderMapper; @Autowired private TempChannelMapper tempChannelMapper; @Autowired private OrderChildMapper orderChildMapper; @Resource OrderMerchantMapper orderMerchantMapper; @Resource OrderChildsMapper orderChildsMapper; @Autowired private OrderDetailMapper orderDetailMapper; @Override public void onMessage(Message message, Channel channel) throws Exception { try { String mqOrderInfoStr=new String(message.getBody(),"utf-8"); logger.info("消息消费者body="+mqOrderInfoStr); MQOrderInfo mqOrderInfo=JSONObject.parseObject(mqOrderInfoStr, MQOrderInfo.class); //处理业务逻辑:向中间库插入订单数据 insertOrder(mqOrderInfo); //消息标示,false只确认当前一个消息确认,true确认所有consumer获得的消息 channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (Exception e) { e.printStackTrace(); //ack返回false,并重新回到队列 channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true); } } /** * 向中间库插入订单信息 * @param mqOrderInfo */ private void insertOrder(MQOrderInfo mqOrderInfo) { if(mqOrderInfo!=null){ Order order=mqOrderInfo.getOrder(); if(order!=null){ CustomerContextHolder.setContextType(order.getIdCompany()); //订单联系人 OrderContact person=mqOrderInfo.getOrderContact(); if(person!=null){ orderContactMapper.insertSelective(person); } //订单主表 orderMapper.insertSelective(order); //临时大客户 TempChannel tcc=mqOrderInfo.getTempChannel(); if(tcc!=null){ tempChannelMapper.insertSelective(tcc); } //订单分项 List<OrderChild> orderChildList=mqOrderInfo.getOrderchildList(); if(CollectionUtils.isNotEmpty(orderChildList)){ for (OrderChild orderChild : orderChildList) { orderChildMapper.insertSelective(orderChild); } } //价格拆分 List<OrderMerchant> orderMerchantList=mqOrderInfo.getOrderMerchantList(); if(CollectionUtils.isNotEmpty(orderMerchantList)){ for (OrderMerchant orderMerchant : orderMerchantList) { orderMerchantMapper.insertSelective(orderMerchant); } } //子产品价格拆分 List<OrderChilds> orderChildsList=mqOrderInfo.getOrderChildsList(); if(CollectionUtils.isNotEmpty(orderChildsList)){ for (OrderChilds orderChilds : orderChildsList) { orderChildsMapper.insertSelective(orderChilds); } } //订单明细 List<OrderDetail> orderDetailList=mqOrderInfo.getOrderDetailList(); if(CollectionUtils.isNotEmpty(orderDetailList)){ for (OrderDetail orderDetail : orderDetailList) { orderDetailMapper.insertSelective(orderDetail); } } } } } }