在pika python中设置'x-message-ttl'

时间:2016-09-04 13:30:04

标签: python-2.7 rabbitmq pika

我想使用pika为Rabbitmq队列设置TTL为1秒。 我尝试了以下代码

import ctypes
int32=ctypes.c_int
connection = pika.BlockingConnection(pika.ConnectionParameters(
       host='localhost'))
channel = connection.channel()
this=channel.queue_declare(queue='hello',
                            arguments={'x-message-ttl' : int32(1000)}
                            )

channel.basic_publish(exchange='',
               routing_key='hello',
               body=message)

print this.method.consumer_count

我收到以下错误

Traceback (most recent call last):
  File "rabt.py", line 8, in <module>
    arguments={'x-message-ttl' : int32(1000)}
  File "build\bdist.win32\egg\pika\adapters\blocking_connection.py", line 2397, in queue_declare
  File "build\bdist.win32\egg\pika\channel.py", line 815, in queue_declare
  File "build\bdist.win32\egg\pika\channel.py", line 1312, in _rpc
  File "build\bdist.win32\egg\pika\channel.py", line 1324, in _send_method
  File "build\bdist.win32\egg\pika\connection.py", line 2139, in _send_method
  File "build\bdist.win32\egg\pika\connection.py", line 2119, in _send_frame
  File "build\bdist.win32\egg\pika\frame.py", line 74, in marshal
  File "build\bdist.win32\egg\pika\spec.py", line 1015, in encode
  File "build\bdist.win32\egg\pika\data.py", line 85, in encode_table
  File "build\bdist.win32\egg\pika\data.py", line 153, in encode_value
pika.exceptions.UnsupportedAMQPFieldException: (['\x00\x00', '\x05', 'hello', '\x00', None, '\r', 'x-message-ttl'], c_long(1000))

我试图在1秒后将此特定队列中的所有消息写入死信。我可以知道如何使用Pika设置TTL吗?谢谢!

1 个答案:

答案 0 :(得分:7)

connection = pika.BlockingConnection(pika.ConnectionParameters(
       host='localhost'))
channel = connection.channel()
this=channel.queue_declare(queue='hello',
                            arguments={'x-message-ttl' : 1000}
                            )

你不需要演员