RabbitMQ案例七之延时任务



环境要求

Github: 
https://github.com/rabbitmq/rabbitmq-delayed-message-exchange 
Rabbitmq版本要求: RabbitMQ 3.5.7 and later versions. 
Erlang版本要求: Erlang/OTP 18.0 and later versions 
插件列表: 
http://www.rabbitmq.com/community-plugins.html 
延迟插件下载地址: 
https://bintray.com/rabbitmq/community-plugins/rabbitmq_delayed_message_exchange/v3.6.x#files

安装

拷贝rabbitmq_delayed_message_exchange到plugins 
启动插件: 
rabbitmq-plugins enable rabbitmq_delayed_message_exchange

Rabbitmq设置

测试结果

生产者代码

# encoding: utf-8
import pika, time

'''创建rabbitmq连接'''
credentials = pika.PlainCredentials("michael", "michael")
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='192.168.12.171', credentials=credentials))


delay_channel = connection.channel()
'''发送消息确认'''
delay_channel.confirm_delivery()

'''消息延迟时间(ms)'''
headers = {
    "x-delay": 10000
}
delay_channel.basic_publish(exchange='delay',
                      routing_key='delay',
                      body=str( time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))),
                      properties=pika.BasicProperties(headers=headers, delivery_mode=2))

print " [x] Sent"


你可能感兴趣的:(RabbitMQ)