springboot+rabbitmq实现延迟队列

不说废话,直接上代码。

1、创建队列和交换机

@Configuration
public class RabbitConfig {
	
	@Bean
	public Queue hello() {
		return new Queue(MqConstant.HELLO_QUEUE_NAME);
	}
	
	@Bean
	public Binding binding() {
		return BindingBuilder.bind(hello()).to(defaultChange()).with(MqConstant.HELLO_QUEUE_NAME);
	}
	@Bean
	public DirectExchange defaultChange() {
		return new DirectExchange(MqConstant.DEFAULT_EXCHANGE, true, false);
	}
	
	@Bean
	public Queue repeatTradeQueue() {
		return new Queue(MqConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME, true, false, false);
	}
	
	@Bean
	public Binding drepeatTradeBinding() {
		return BindingBuilder.bind(repeatTradeQueue()).to(defaultChange()).with(MqConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
	}
	
	@Bean
	public Queue deadLetterQueue() {
		Map map = new HashMap<>();
		map.put("x-dead-letter-exchange", MqConstant.DEFAULT_EXCHANGE);
		map.put("x-dead-letter-routing-key", MqConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
		Queue queue = new Queue(MqConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME, true, false, false, map);
		System.out.println("arguments :" + queue.getArguments());
		return queue;
	}
	
	@Bean
	public Binding deadLetterBinding() {
		return BindingBuilder.bind(deadLetterQueue()).to(defaultChange()).with(MqConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME);
	}
}

2、创建生产者

@Component
public class Producr {
	private static final Logger LOGGER = LoggerFactory.getLogger(Producr.class);
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	public void send(String queueName, String message) {
		amqpTemplate.convertAndSend(MqConstant.DEFAULT_EXCHANGE, queueName, message);
		LOGGER.info("send message:" + message + "----" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}
	
	public void send(String queueName, String msg, long time) {
		LOGGER.info("startTime is {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		DLXMessage dlxMessage = new DLXMessage(queueName, msg, time);
		MessagePostProcessor processor = new MessagePostProcessor() {
			
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				message.getMessageProperties().setExpiration(time+"");
				return message;
			}
		};
		dlxMessage.setExchange(MqConstant.DEFAULT_EXCHANGE);
		amqpTemplate.convertAndSend(MqConstant.DEFAULT_EXCHANGE, MqConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME, msg, processor);
		LOGGER.info("endTime is {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}
}

3、创建中间(转发)队列

@Component
public class TradeProcess {
	private static final Logger LOGGER = LoggerFactory.getLogger(TradeProcess.class);
	@Autowired
	private Producr producr;
	
	@RabbitListener(queues=MqConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME)
	@RabbitHandler
	public void process(String content) {
		producr.send(MqConstant.HELLO_QUEUE_NAME, content);
		LOGGER.info("process time is {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}
}
4、创建消费者
@Component
public class ReceiverTest {
	private static final Logger LOGGER = LoggerFactory.getLogger(ReceiverTest.class);
		
	@RabbitListener(queues=MqConstant.HELLO_QUEUE_NAME)
	@RabbitHandler
	public void process(String message) {
	   LOGGER.info("message is " + message + "---" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

最后,根据官方文档还有一种方法,使用延时队列插件

有个rabbitmq的插件,可以直接使用延时队列,有兴趣的可以自己去实践。

传送门:Dead Letter Exchanges

           Time-To-Live Extensions


你可能感兴趣的:(原创)