package com.yida.data.rabbit.util;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RabbitUtil {
    @Resource
    private AmqpTemplate amqpTemplate;

    public void sendMsg(String exchange, String key, String msg) {
        amqpTemplate.send(exchange, key, MessageBuilder.withBody(msg.getBytes()).build());
    }

    public void sendMsg(String exchange, String key, Message msg) {
        amqpTemplate.send(exchange, key, msg);
    }

    /**
     * mq发送消息方法
     *
     * @param exchange   交换机
     * @param routingKey routingKey值
     * @param msg        发送的消息, 对象必须实现序列化接口
     * @author ZYJ
     * @date 2021/9/7 10:50
     */
    public void convertAndSendMsg(String exchange, String routingKey, Object msg) {
        amqpTemplate.convertAndSend(exchange, routingKey, msg);
    }
}
