When RabbitMq sends a message, it also accepts the returned data

Producer code, pay attention to set it in the queue where the returned data is put;

In this way, synchronous blocking can be achieved to get the return data

package com.boot.springbootnew.component;

import com.alibaba.fastjson.JSON;
import com.boot.springbootnew.config.ReturnComponent;
import com.boot.springbootnew.pojo.MqFailLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqComponent implements RabbitTemplate.ConfirmCallback{

    private Logger logger = LoggerFactory.getLogger(RabbitMqComponent.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    private ReturnComponent returnComponent;

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(ack){
            logger.info(" 回调id:{} 发送成功", correlationData.getId());
        }else {
            logger.error(" 回调id:{} 消费失败, {} " ,correlationData.getId(),cause);
        }
    }

    public void sendMessage(MqFailLog msgLog) {
        //设置回调为当前类对象
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(returnComponent);
        //构建回调id为uuid
        CorrelationData correlationId = new CorrelationData(JSON.toJSONString(msgLog));
        //发送消息到消息队列
//        rabbitTemplate.convertAndSend(msgLog.getExchange(),msgLog.getRouteKey(),msgLog.getMessage(),correlationId);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setCorrelationId(correlationId.getId());
        messageProperties.setReplyTo("user.reg.response.queue");
        messageProperties.setConsumerQueue("user.reg.response.queue");
        byte[] bytes = JSON.toJSONString(msgLog.getMessage()).getBytes();
        Message message = rabbitTemplate.sendAndReceive(msgLog.getExchange(), msgLog.getRouteKey(), new Message(bytes, messageProperties), correlationId);
        String response = new String(message.getBody());
        System.out.println(response);

    }
}

consumer code

package com.listener;
import com.rabbitmq.client.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


//将该监听器注入ioc容器
@Component
public class MyRabbitListener {

    //监听的队列名为queue.news
    @RabbitListener(queues = "user.reg.queue")
    @RabbitHandler
    public String listener(byte[] bytes,Channel channel){
        String request = new String(bytes);

        System.out.println("收到消息:"+request);
        return "我处理完成了  MyRabbitListener";
    }

}

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324275921&siteId=291194637