package com.dhcc.finance.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class QueueMessageServiceImpl implements RabbitTemplate.ReturnCallback
{
    /**
     * 消息队列模板
     */
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Object message, String exchange, String routingKey) throws Exception {
        this.rabbitTemplate.setReturnCallback(this);
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                log.info("消息发送到交换机失败:" + cause + correlationData);
            } else {
                log.info("消息发送到交换机成功");
            }
        });
        //发送消息到消息队列
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
    }

    @Override
    public void returnedMessage(Message message, int i, String arg2, String arg3, String arg4) {
        
        log.info("消息发送到队列失败" + message.toString()+"==i=="+i+"==3=="+arg2+"===3="+arg3+"==4=="+arg4);
    }
}