RabbitMQ如何实现延迟队列?( 二 )

(3)发送延迟消息import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMApping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/delayed")public class DelayedMessageController {@Autowiredprivate DelayedMessageProducer delayedMessageProducer;@GetMapping("/send")public String sendDirectMessage(@RequestParam String message) {delayedMessageProducer.sendDelayedMessage(message);return "Delayed message sent to Exchange: " + message;}}(4)接收延迟消息import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Componentpublic class DelayedMessageConsumer {@RabbitListener(queues = DelayedExchangeConfig.QUEUE_NAME)public void receiveDelayedMessage(String message) {System.out.println("Received delayed message: " + message);}}小结实现 RabbitMQ 延迟队列目前主流的实现方式,是采用官方提供的延迟插件来实现 。而延迟插件需要先下载插件、然后配置并重启 RabbitMQ 服务,之后就可以通过编写代码的方式实现延迟队列了 。




推荐阅读