像支付宝,微信支付后的回调机制,可以使用延迟队列实现。可是创建延时不同时间的队列,死信后被消费,消费异常后,消息进入不同的队列。
/**
* 默认及时消息交换机
* @return
*/
@Bean()
public DirectExchange defaultDirectExchange() {return new DirectExchange(MessageQueueConstants.DEFAULT_DIRECT_EXCHANGE_NAME, true, false);
}/**
* 默认延迟消息死信队列
* @return
*/
@Bean
public Queue defaultDeadLetterQueue10() {Map
arguments.put("x-dead-letter-exchange",MessageQueueConstants.DEFAULT_DIRECT_EXCHANGE_NAME);//设置交换机路由
arguments.put("x-dead-letter-routing-key", MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME);//设置转发队列名称
arguments.put("x-message-ttl", 10 * 1000); // Message TTL = 10s
Queue queue = new Queue(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME10,true,false,false,arguments);
return queue;
}/**
* 默认延迟消息死信队列
* @return
*/
@Bean
public Queue defaultDeadLetterQueue30() {Map
arguments.put("x-dead-letter-exchange",MessageQueueConstants.DEFAULT_DIRECT_EXCHANGE_NAME);//设置交换机路由
arguments.put("x-dead-letter-routing-key", MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME);//设置转发队列名称
arguments.put("x-message-ttl", 30 * 1000); // Message TTL = 30s
Queue queue = new Queue(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME30,true,false,false,arguments);
return queue;
}/**
* 默认延迟消息死信队列
* @return
*/
@Bean
public Queue defaultDeadLetterQueue180() {Map
arguments.put("x-dead-letter-exchange",MessageQueueConstants.DEFAULT_DIRECT_EXCHANGE_NAME);//设置交换机路由
arguments.put("x-dead-letter-routing-key", MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME);//设置转发队列名称
arguments.put("x-message-ttl", 1000*60*3); // Message TTL = 180s
Queue queue = new Queue(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME180,true,false,false,arguments);
return queue;
}/**
* 默认延迟消息死信队列
* @return
*/
@Bean
public Queue defaultDeadLetterQueue1200() {Map
arguments.put("x-dead-letter-exchange",MessageQueueConstants.DEFAULT_DIRECT_EXCHANGE_NAME);//设置交换机路由
arguments.put("x-dead-letter-routing-key", MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME);//设置转发队列名称
arguments.put("x-message-ttl", 1000*60*20); // Message TTL = 1200s
Queue queue = new Queue(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME1200,true,false,false,arguments);
return queue;
}/**
* 默认延迟消息死信队列
* @return
*/
@Bean
public Queue defaultDeadLetterQueue3600() {Map
arguments.put("x-dead-letter-exchange",MessageQueueConstants.DEFAULT_DIRECT_EXCHANGE_NAME);//设置交换机路由
arguments.put("x-dead-letter-routing-key", MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME);//设置转发队列名称
arguments.put("x-message-ttl", 1000*60*60); // Message TTL = 3600s
Queue queue = new Queue(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME3600,true,false,false,arguments);
return queue;
}@Bean
public Binding defaultDeadLetterBinding10() {Binding bind = BindingBuilder.bind(defaultDeadLetterQueue10()).to(defaultDirectExchange()).with(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME10);
return bind;
}
@Bean
public Binding defaultDeadLetterBinding30() {Binding bind = BindingBuilder.bind(defaultDeadLetterQueue30()).to(defaultDirectExchange()).with(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME30);
return bind;
}
@Bean
public Binding defaultDeadLetterBinding180() {Binding bind = BindingBuilder.bind(defaultDeadLetterQueue180()).to(defaultDirectExchange()).with(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME180);
return bind;
}
@Bean
public Binding defaultDeadLetterBinding1200() {Binding bind = BindingBuilder.bind(defaultDeadLetterQueue1200()).to(defaultDirectExchange()).with(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME1200);
return bind;
}
@Bean
public Binding defaultDeadLetterBinding3600() {Binding bind = BindingBuilder.bind(defaultDeadLetterQueue3600()).to(defaultDirectExchange()).with(MessageQueueConstants.DEFAULT_DEAD_LETTER_QUEUE_NAME3600);
return bind;
}/**
* 默认延迟消息死信接受转发消息队列
* @return
*/
@Bean
public Queue defaultRepeatTradeQueue() {Queue queue = new Queue(MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME,true,false,false);
return queue;
}@Bean
public Binding defaultRepeatTradeBinding() {return BindingBuilder.bind(defaultRepeatTradeQueue()).to(defaultDirectExchange()).with(MessageQueueConstants.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
}