无法接收消息 RabbitMQ
Not able to recieve the messages RabbitMQ
我正在学习教程 4 (Routing)。以下是 send.py
的代码
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
type='direct')
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
下面是 receive.py
的代码
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
type='direct')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
severities = sys.argv[1:]
if not severities:
sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
sys.exit(1)
for severity in severities:
channel.queue_bind(exchange='direct_logs',
queue=queue_name,
routing_key=severity)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print "Hello"
print(" [x] %r:%r" % (method.routing_key, body))
channel.basic_consume(callback,queue=queue_name,no_ack=True)
channel.start_consuming()
但是当我运行正在运行脚本时,我无法从临时队列中获取任何消息。我想我的 callback
函数没有被调用。代码是从网站上获取的。
我运行将代码设置为:
python send.py
为此我得到:
[x] Sent 'info':'Hello World!'
而当我 运行
python rec.py info
我得到:
[*] Waiting for logs. To exit press CTRL+C
否则什么都不打印。
我什至使用
重新启动了 RabbitMQ
rabbitmqctl stop_app
rabbitmqctl start_app
请让我知道哪里出错了,我怎样才能收到消息
运行 receive.py 先
问题是,rabbitmq 不会保留未路由到队列的消息。
您的 send.py 没有声明队列或绑定到交换器,因此当您通过交换器发送消息时,没有可以将消息传递到的队列
如果您 运行 您的 receive.py 首先,您将创建所需的交换、队列和绑定
然后您的 send.py 将能够发送消息,并且消息将被路由到队列以供 receive.py 接收
如果代码和教程中的一模一样,需要先运行rec.py
再send.py
我正在学习教程 4 (Routing)。以下是 send.py
的代码import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
type='direct')
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
下面是 receive.py
的代码import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
type='direct')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
severities = sys.argv[1:]
if not severities:
sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
sys.exit(1)
for severity in severities:
channel.queue_bind(exchange='direct_logs',
queue=queue_name,
routing_key=severity)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print "Hello"
print(" [x] %r:%r" % (method.routing_key, body))
channel.basic_consume(callback,queue=queue_name,no_ack=True)
channel.start_consuming()
但是当我运行正在运行脚本时,我无法从临时队列中获取任何消息。我想我的 callback
函数没有被调用。代码是从网站上获取的。
我运行将代码设置为:
python send.py
为此我得到:
[x] Sent 'info':'Hello World!'
而当我 运行
python rec.py info
我得到:
[*] Waiting for logs. To exit press CTRL+C
否则什么都不打印。 我什至使用
重新启动了 RabbitMQrabbitmqctl stop_app
rabbitmqctl start_app
请让我知道哪里出错了,我怎样才能收到消息
运行 receive.py 先
问题是,rabbitmq 不会保留未路由到队列的消息。
您的 send.py 没有声明队列或绑定到交换器,因此当您通过交换器发送消息时,没有可以将消息传递到的队列
如果您 运行 您的 receive.py 首先,您将创建所需的交换、队列和绑定
然后您的 send.py 将能够发送消息,并且消息将被路由到队列以供 receive.py 接收
如果代码和教程中的一模一样,需要先运行rec.py
再send.py