package cn.sikey.websocket.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author: nelson
 * @Date: 2025/5/30
 * @Description: mq消息发送
 */
@Slf4j
@Component
public class RabbitMQMessaging {
    private final RabbitTemplate rabbitTemplate;
    private final MessagingRabbitmqConfig messagingRabbitmqConfig;

    public RabbitMQMessaging(RabbitTemplate rabbitTemplate, MessagingRabbitmqConfig messagingRabbitmqConfig) {
        this.rabbitTemplate = rabbitTemplate;
        this.messagingRabbitmqConfig = messagingRabbitmqConfig;
    }

    /**
     * mq消息发送
     *
     * @param message 消息体
     * @return
     */
    public void publish(Object message) {
        log.info("mq消息发送，消息：{}", message);
        MessagePostProcessor messagePostProcessor = messagePost -> messagePost;
        rabbitTemplate.convertAndSend(
                messagingRabbitmqConfig.getExchangeName(), messagingRabbitmqConfig.getRoutingKey(), message, messagePostProcessor);
    }
}
