RabbitMq应用

一、广播模式交换机

 package com.dst.common.mq.constant;

/**
 * @author [email protected]
 * @version 1.0
 * @date 2022-12-30 09:46:21
 */
public class MqConstForWorkOrderBaseInfo {


    /**
     * 工单中心状态改变发送mq消息
     */
    public static final String EXCHANGE_FANOUT_WORK_ORDER_BASE = 
    "exchange.fanout.work.order.base";

    public static final String QUEUE_WORK_ORDER_BASE = "queue.work.order.base";

    public static final String ROUTINGKEY_WORK_ORDER_BASE = "routingkey.work.order.base";

}

    /**
     * 工单中心工单状态MQ绑定
     * @return
     */
    @Bean
    public FanoutExchange workOrderBaseExchange() {
        return (FanoutExchange)ExchangeBuilder
            .fanoutExchange(MqConstForWorkOrderBaseInfo.EXCHANGE_FANOUT_WORK_ORDER_BASE + 
        profile).build();
    }

    @Bean
    public Queue workOrderBaseQueue() {
        return  QueueBuilder.durable(MqConstForWorkOrderBaseInfo.QUEUE_WORK_ORDER_BASE + 
           profile)
                .build();
    }

    @Bean
    public Binding workOrderBaseBinding() {
        return BindingBuilder.bind(workOrderBaseQueue()).to(workOrderBaseExchange());
    }

二、消息生产者

package com.dst.common.mq.producer;

import com.dst.common.mq.constant.MqConstForWorkOrderBaseInfo;
import com.dstcar.common.utils.json.JsonUtil;
import com.dstcar.entitys.workorderbase.message.event.WorkOrderBaseInfoStateChangeEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 工单中心完成属性变更消息生产者
 * @Author liu wei ping
 * @Date 2022/12/29 19:40
 * @Description:
 **/
@Slf4j
@Component
public class WorkOrderBaseInfoMessageProducer {

    @Value(value = "-${spring.profiles.active}")
    private String profile;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 主题交换机:工单中心状态改变发送消息
     *
     * @param event
     */
    @EventListener(WorkOrderBaseInfoStateChangeEvent.class)
    public void workOrderBaseInfoStateChange(WorkOrderBaseInfoStateChangeEvent event) {
        log.info("WorkOrderBaseInfoMessageProducer.workOrderBaseInfoStateChange:event: 
          {}", JsonUtil.toJson(event));
       rabbitTemplate.convertAndSend(MqConstForWorkOrderBaseInfo
                 .EXCHANGE_FANOUT_WORK_ORDER_BASE + profile, "", event.getSource());
    }

}

三、消息消费者

package com.dst.common.mq.consumer;


import com.dst.common.mq.constant.MqConstForWorkOrderBaseInfo;
import com.dst.modules.business.workorder.workorderbase.service.workorderhandler.WorkOrderPostHandler;
import com.dst.modules.business.workorder.workorderbase.service.workorderhandler.WorkOrderPostHandlerFactory;
import com.dstcar.common.constant.WorkOrderStatusConst;
import com.dstcar.common.utils.json.JsonUtil;
import com.dstcar.common.utils.string.StringUtil;
import com.dstcar.entitys.workorderbase.message.WorkOrderBaseInfoMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author [email protected]
 * @version 1.0
 * @date 2022-12-30 09:46:21
 */
@Component
public class WorkOrderBaseInfoMQConsumer {

    private static final Logger log = 
       LoggerFactory.getLogger(WorkOrderBaseInfoMQConsumer.class);

    @Autowired
    private WorkOrderPostHandlerFactory workOrderPostHandlerFactory;

    /**
     * 工单中心状态改变、验车工单完成回调才会修改车辆属性状态
     * @param msg
     */
    @RabbitHandler
    @RabbitListener(queues = MqConstForWorkOrderBaseInfo.QUEUE_WORK_ORDER_BASE + 
       "-${spring.profiles.active}")
    public void process(Message msg) {
        try {
            byte[] bytes = msg.getBody();
            if(StringUtil.isEmpty(bytes)){
                log.warn("WorkOrderBaseInfoMQConsumer:接收MQ消息为空");
                return;
            }
            String data = new String(bytes, StandardCharsets.UTF_8);
            log.info("WorkOrderBaseInfoMQConsumer:接收MQ消息 ==> {}",data);
            WorkOrderBaseInfoMessage workOrderBaseInfoMessage =  
            JsonUtil.toBean(data, WorkOrderBaseInfoMessage.class);
            if (Objects.nonNull(workOrderBaseInfoMessage.getStatus()) && 
              WorkOrderStatusConst.WORK_ORDER_STATUS_BASE_3.
              equals(workOrderBaseInfoMessage.getStatus())) {
                handMessage(workOrderBaseInfoMessage);
            }
        } catch (Exception e) {
            log.error("WorkOrderBaseInfoMQConsumer:接收MQ消息处理失败 \n",e);
        }
    }

    /**
     * 处理完成工单的属性修改
     * @param workOrderBaseInfoMessage
     */
    private void handMessage(WorkOrderBaseInfoMessage workOrderBaseInfoMessage){
        // 工单模板编码
        WorkOrderPostHandler handler = 
      workOrderPostHandlerFactory.getHandler(workOrderBaseInfoMessage.getTemplateCode());
        if (Objects.nonNull(handler)) {
            handler.handlerComplete(workOrderBaseInfoMessage);
        } else {
            log.error("根据模板编码未得到执行实例: 
                    {}",workOrderBaseInfoMessage.getTemplateCode());
        }
    }
}

四、页面控制台发送mq消息

RabbitMq应用_第1张图片

 

你可能感兴趣的:(Rabbitmq,java-rabbitmq,rabbitmq,java)