Page 78 - Python rat la co ban
P. 78
import pika
connection = pika.BlockingConnection(pika.Connectio
nParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hel
lo', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
Để gởi một message, chúng ta cần kết nối đến server và
khai báo một channel, ở đây là channel có tên là hello .
Sau khi khai báo channel, tao tiến hành gởi message có
nội dung Hello World! thông qua channel này, kèm theo
khai báo routing_key là hello . Routing Key sẽ giúp điều
hướng message này đến đúng các worker được khai báo
nhận message theo routing key (Consumer)
12.3. Nhận message - Consumer