Working through the confirmation example

albeit with code that works rather than what came with the book.  Hmm...
This commit is contained in:
Dan Buch 2012-11-14 20:37:37 -05:00
parent 26ec865dd7
commit 18a9674ec2
3 changed files with 67 additions and 10 deletions

View File

@ -8,6 +8,6 @@ setup(
description='crap accumulated while reading through RabbitMQ in Action',
packages=['sylvilagus'],
install_requires=[
'pika >= 0.9.6'
'pika == 0.9.6'
]
)

View File

@ -1,16 +1,36 @@
import pika
def get_channel():
def get_conn_params():
credentials = pika.PlainCredentials('guest', 'guest')
conn_params = pika.ConnectionParameters('localhost',
credentials=credentials)
channel = pika.BlockingConnection(conn_params).channel()
channel.exchange_declare(exchange='hello-exchange',
type='direct',
passive=False,
durable=True,
auto_delete=False)
return pika.ConnectionParameters('localhost', credentials=credentials)
def get_nonblocking_channel(declare_exchange=True):
channel = pika.SelectConnection(get_conn_params()).channel()
if declare_exchange:
channel.exchange_declare(
exchange='hello-exchange',
exchange_type='direct',
passive=False,
durable=True,
auto_delete=False
)
return channel
def get_channel(declare_exchange=True):
channel = pika.BlockingConnection(get_conn_params()).channel()
if declare_exchange:
channel.exchange_declare(
exchange='hello-exchange',
exchange_type='direct',
passive=False,
durable=True,
auto_delete=False
)
return channel

View File

@ -0,0 +1,37 @@
from __future__ import print_function
import logging
import sys
import pika
from sylvilagus.chapter02 import hello_world
def main(sysargs=sys.argv[:]):
msg = sysargs[1]
logging.basicConfig()
channel = hello_world.get_channel()
if channel.basic_publish(
body=msg,
exchange='hello-exchange',
properties=pika.BasicProperties(
content_type='text/plain',
delivery_mode=1
),
routing_key='hola',
mandatory=True):
print('Message delivered!')
else:
print('Message returned!')
channel.close()
return 0
if __name__ == '__main__':
sys.exit(main())