3 # Copyright 2011 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials
16 # provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
31 # The views and conclusions contained in the software and
32 # documentation are those of the authors and should not be
33 # interpreted as representing official policies, either expressed
34 # or implied, of GRNET S.A.
39 from optparse import OptionParser
41 from carrot.connection import BrokerConnection
42 from carrot.messaging import Consumer
43 from carrot.messaging import Publisher
46 BROKER_HOST = 'localhost'
49 BROKER_PASSWORD = 'guest'
52 CONSUMER_QUEUE = 'feed'
53 CONSUMER_EXCHANGE = 'sample'
59 if __name__ == '__main__':
60 parser = OptionParser()
61 parser.add_option('-v', '--verbose', action='store_true', default=False,
62 dest='verbose', help='Enable verbose logging')
63 parser.add_option('--host', default=BROKER_HOST, dest='host',
64 help='RabbitMQ host (default: %s)' % BROKER_HOST)
65 parser.add_option('--port', default=BROKER_PORT, dest='port',
66 help='RabbitMQ port (default: %s)' % BROKER_PORT, type='int')
67 parser.add_option('--user', default=BROKER_USER, dest='user',
68 help='RabbitMQ user (default: %s)' % BROKER_USER)
69 parser.add_option('--password', default=BROKER_PASSWORD, dest='password',
70 help='RabbitMQ password (default: %s)' % BROKER_PASSWORD)
71 parser.add_option('--vhost', default=BROKER_VHOST, dest='vhost',
72 help='RabbitMQ vhost (default: %s)' % BROKER_VHOST)
73 parser.add_option('--queue', default=CONSUMER_QUEUE, dest='queue',
74 help='RabbitMQ queue (default: %s)' % CONSUMER_QUEUE)
75 parser.add_option('--exchange', default=CONSUMER_EXCHANGE, dest='exchange',
76 help='RabbitMQ exchange (default: %s)' % CONSUMER_EXCHANGE)
77 parser.add_option('--key', default=CONSUMER_KEY, dest='key',
78 help='RabbitMQ key (default: %s)' % CONSUMER_KEY)
79 parser.add_option('--callback', default=None, dest='callback',
80 help='Callback function to consume messages')
81 parser.add_option('--test', action='store_true', default=False,
82 dest='test', help='Produce a dummy message for testing')
83 opts, args = parser.parse_args()
87 logging.basicConfig(format='%(asctime)s [%(levelname)s] %(name)s %(message)s',
88 datefmt='%Y-%m-%d %H:%M:%S',
89 level=logging.DEBUG if DEBUG else logging.INFO)
90 logger = logging.getLogger('dispatcher')
92 conn = BrokerConnection(hostname=opts.host, port=opts.port,
93 userid=opts.user, password=opts.password,
94 virtual_host=opts.vhost)
96 publisher = Publisher(connection=conn,
97 exchange=opts.exchange, routing_key=opts.key,
98 exchange_type="topic")
99 publisher.send({"test": "0123456789"})
103 consumer = Consumer(connection=conn, queue=opts.queue,
104 exchange=opts.exchange, routing_key=opts.key,
105 exchange_type="topic")
109 cb = opts.callback.rsplit('.', 1)
112 cb_module = sys.modules[cb[0]]
113 callback = getattr(cb_module, cb[1])
115 def process_message(message_data, message):
116 logger.debug('%s', message_data)
118 callback(message_data)
121 consumer.register_callback(process_message)
124 except KeyboardInterrupt: