package com.ruoyi.cai.mq; import com.alibaba.fastjson.JSON; import com.ruoyi.cai.mq.consumer.AmqpConsumer; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class AmqpProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendCalculateSales(String message){ rabbitTemplate.convertAndSend(AmqpConsumer.CALCULATE_SALES_EXCHANGE, AmqpConsumer.CALCULATE_SALES_KEY, message); } public void sendCheckTimeOut(String message,Integer timeout){ rabbitTemplate.convertAndSend(CheckTimeOutMqConfig.EXCHANGE_NAME, CheckTimeOutMqConfig.ROUTING_KEY, message, messagePostProcessor -> { messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间,单位毫秒 return messagePostProcessor; }); } public void sendCommonDelayMq(CommonDelayDto dto,Integer timeout){ rabbitTemplate.convertAndSend(CommonDelayMqConfig.EXCHANGE_NAME, CommonDelayMqConfig.ROUTING_KEY, JSON.toJSONString(dto), messagePostProcessor -> { messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间,单位毫秒 return messagePostProcessor; }); } public void sendCommonDelayMq(Integer type,Long roomId,Integer timeout){ CommonDelayDto dto = new CommonDelayDto(); dto.setType(type); dto.setRoomId(roomId); rabbitTemplate.convertAndSend(CommonDelayMqConfig.EXCHANGE_NAME, CommonDelayMqConfig.ROUTING_KEY, JSON.toJSONString(dto), messagePostProcessor -> { messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间,单位毫秒 return messagePostProcessor; }); } }