插件的安装,本文就不做描述了,插件安装后如下,就说明安装成功了
1 添加pom依赖,yml配置就不讲了
2 核心类,定义交换机的代码改成如下,其它的定义队列,设置绑定关系,设置死信等,都是一样的,唯一就是Binding那里,最后添加.noargs(),idea也会自动识别处理的,就不粘代码了
@Bean
public CustomExchange delayed_Exchang_1()
{
Map<String, Object> map = new HashMap<>();
//自定义交换机的类型,后面的参数自己调整,topic,direct等
map.put("x-delayed-type","topic");
CustomExchange customExchange = new CustomExchange("delayed_Exchang_1", "x-delayed-message", true, false,map);
return customExchange;
}
3 生产者核心代码
MessageProperties messageProperties = new MessageProperties();
//延迟5秒
messageProperties.setDelay(5000);
Message message = new Message("消息".getBytes(), messageProperties);
rabbitTemplate.convertAndSend("delayed_Exchang_1",
"china.chongqing.aa",
message);