diff --git a/sylvilagus/python/setup.py b/sylvilagus/python/setup.py index 1295d34..bf0565a 100644 --- a/sylvilagus/python/setup.py +++ b/sylvilagus/python/setup.py @@ -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' ] ) diff --git a/sylvilagus/python/sylvilagus/chapter02/hello_world.py b/sylvilagus/python/sylvilagus/chapter02/hello_world.py index 879fb9e..d5edff1 100644 --- a/sylvilagus/python/sylvilagus/chapter02/hello_world.py +++ b/sylvilagus/python/sylvilagus/chapter02/hello_world.py @@ -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 diff --git a/sylvilagus/python/sylvilagus/chapter02/hello_world_producer_with_confirms.py b/sylvilagus/python/sylvilagus/chapter02/hello_world_producer_with_confirms.py new file mode 100644 index 0000000..30c91ab --- /dev/null +++ b/sylvilagus/python/sylvilagus/chapter02/hello_world_producer_with_confirms.py @@ -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())