在RabbitMQ3.5.7及以后的版本提供了一个插件(rabbitmq-delayed-message-exchange)来实现延迟队列功能(Linux和Windows都可用)。同时插件依赖Erlang/OPT18.0及以上。
插件源码地址:https://github.com/rabbitmq/rabbitmq-delayed-message-exchange
插件下载地址:https://bintray.com/rabbitmq/community-plugins/rabbitmq_delayed_message_exchange
注意rabbitmq的版本与rabbitmq_delayed_message_exchange插件的版本
rabbitmq_delayed_message_exchange-0.0.1.ez只支持rabbimq 3.5.x、3.6.x、3.7.x,不支持3.8.x
rabbitmq_delayed_message_exchange-20171215-3.6.x插件虽然在3.8.4版本下虽然可以安装,但是不生效
如果rabbitmq不支持rabbitmq_delayed_message_exchange插件可能就是版本问题
rabbitmq_delayed_message_exchange-20171215-3.6
Updated Jan 17, 2018
https://bintray.com/rabbitmq/community-plugins/download_file?file_path=3.6.x%2Frabbitmq_delayed_message_exchange%2Frabbitmq_delayed_message_exchange-20171215-3.6.x.zip
rabbitmq_delayed_message_exchange-20171201-3.7.x
Updated Jan 15, 2018
https://bintray.com/rabbitmq/community-plugins/download_file?file_path=3.7.x%2Frabbitmq_delayed_message_exchange%2Frabbitmq_delayed_message_exchange-20171201-3.7.x.zip
rabbitmq_delayed_message_exchange-0.0.1.ez
Updated Sep 14, 2016
https://bintray.com/rabbitmq/community-plugins/download_file?file_path=rabbitmq_delayed_message_exchange-0.0.1.ez
RabbitMQ 3.7.6.
June 13 2018
https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.6
1、进入插件目录
[root@localhost /]# cd /usr/local/mc/rabbitmq_server-3.7.6/plugins
2、下载插件
[root@localhost plugins]# wget https://bintray.com/rabbitmq/community-plugins/download_file?file_path=rabbitmq_delayed_message_exchange-0.0.1.ez
更改名称
2020-10-27 18:12:59 (150 KB/s) - ‘download_file?file_path=rabbitmq_delayed_message_exchange-0.0.1.ez’ saved [32019/32019]
[root@localhost plugins]# mv download_file\?file_path\=rabbitmq_delayed_message_exchange-0.0.1.ez rabbitmq_delayed_message_exchange-0.0.1.ez
3、启用插件
启用插件管理
[root@localhost sbin]# ./rabbitmq-plugins enable rabbitmq_management
启用延迟队列插件
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
[root@localhost plugins]# rabbitmq-plugins enable rabbitmq_delayed_message_exchange
Enabling plugins on node rabbit@localhost:
rabbitmq_delayed_message_exchange
The following plugins have been configured:
rabbitmq_delayed_message_exchange
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
rabbitmq_delayed_message_exchange
4、关闭插件
rabbitmq-plugins disable rabbitmq_delayed_message_exchange
[root@localhost plugins]# rabbitmq-plugins disable rabbitmq_delayed_message_exchange
Disabling plugins on node rabbit@localhost:
rabbitmq_delayed_message_exchange
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been disabled:
rabbitmq_delayed_message_exchange
stopped 1 plugins.
5、java代码
// 延迟的时间
Map headers = new HashMap();
headers.put("x-delay", 1000*60*30);//30分钟
AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder().headers(headers);
// 声明x-delayed-message类型的exchange
Map argss = new HashMap();
argss.put("x-delayed-type", "direct");
channel.exchangeDeclare("DELAY_EXCHANGE", "x-delayed-message", false,false, argss);
// 声明队列
channel.queueDeclare("DELAY_QUEUE", false,false,false,null);
// 绑定交换机与队列
channel.queueBind("DELAY_QUEUE", "DELAY_EXCHANGE", "DELAY_KEY");
channel.basicPublish("DELAY_EXCHANGE", "DELAY_KEY", props.build(),msg.getBytes());