def main(argv): ledthread = Thread(target=led) ledthread.start() broker = "netapps.ece.vt.edu" vhost = "/2014/fall/sentry" login = "******" password = "******" rkey = "bennybene" message_broker = None channel = None try: # Connect to message broker message_broker = pika.BlockingConnection( pika.ConnectionParameters(host=broker, virtual_host=vhost, credentials=pika.PlainCredentials( login, password, True))) print "Connected to message broker" channel = message_broker.channel() channel.exchange_declare(exchange="alarms", type="direct") signal_num = signal.SIGINT # Create eventg handler if signal is caught try: channel_manager = StatsClientChannelHelper(channel) signal.signal(signal_num, channel_manager.stop_stats_client) signal_num = signal.SIGTERM signal.signal(signal_num, channel_manager.stop_stats_client) except ValueError, ve: print "Warning: Graceful shutdown may not be possible: Unsupported signal: " + signal_num # Create a queue to receive messages my_queue = channel.queue_declare(exclusive=True) # Bind the queue to the stats exchange channel.queue_bind(exchange="alarms", queue=my_queue.method.queue, routing_key=rkey) # Setup callback for when a subscribed message is received channel.basic_consume(on_new_msg, queue=my_queue.method.queue, no_ack=True) print "New message from broker" channel.start_consuming()
def main(): url = 'amqps://*****:*****@fish.rmq.cloudamqp.com/kyiirjzl' parametros = pika.URLParameters(url) connection = pika.BlockingConnection(parametros) channel = connection.channel() channel.queue_declare(queue='Queue_Data') def callback(ch, method, properties, body): print(body.decode()) channel.basic_consume(queue='Queue_Data', auto_ack=True, on_message_callback=callback) channel.start_consuming()
notify_admin_by_email(message) def main(): # Check motes status every 60 seconds interval_monitor = setInterval(checkMotesInactive, 60) try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=EVENT_SERVER)) channel = connection.channel() channel.exchange_declare(exchange=EXCHANGE_READINGS, type='fanout') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange=EXCHANGE_READINGS, queue=queue_name) except Exception, e: print("Error %s:" % e.args[0]) finally: print 'Waiting for readings. To exit press CTRL+C' channel.basic_consume(callback, queue=queue_name, no_ack=True) try: channel.start_consuming() except: message = 'Error in connection to the event server' print message notify_admin_by_email(message) if __name__ == "__main__": main()
import pika from pika import channel params = pika.URLParameters('amqps://*****:*****@beaver.rmq.cloudamqp.com/oljdtgfk') print(params) connection = pika.BlockingConnection(params) channel = connection.channel() channel.queue_declare(queue='admin') def callback(ch,method,properties,body): print('Received in admin') print(body) channel.basic_consume(queue='admin',on_message_callback=callback) print('Started') channel.start_consuming() channel.close()
import pika from pika import connection from pika import channel params = pika.URLParameters( "amqps://*****:*****@hornet.rmq.cloudamqp.com/lrtmtglx" ) connection = pika.BlockingConnection(params) channel = connection.channel() channel.queue_declare(queue="admin") def callback(ch, method, properties, body): print("Received in admin") id = json.loads(body) print(id) product = Product.objects.get(id=id) product.likes = product.likes + 1 product.save() print("Product likes increased!") channel.basic_consume(queue="admin", on_message_callback=callback, auto_ack=True) print("Started consuming") channel.start_consuming() channel.close()
# Create a queue # -------------------- # Creating an exclusive queue NOTE that will only exists # as long as the client is connected result = channel.queue_declare(exclusive=True) queue_name = result.method.queue print "An exclusive queue declared successfully, now binding the queue and the exchange with the given routing key(s)" # Bind you queue to the message exchange, and register your new message event handler for topic in topics: channel.queue_bind(exchange='pi_utilization', queue=queue_name, routing_key=topic) print "Binding of exchange with the declared queue successful, now start pika's event loop by calling channel.basic_consume" # Start pika's event loop channel.basic_consume(on_new_msg, queue=queue_name, no_ack=True) print "Pika's event loop started" channel.start_consuming() except pika.exceptions.ProbableAccessDeniedError, pade: print >> sys.stderr, "Error: A Probable Access Denied Error occured: " + str(pade.message) except pika.exceptions.ProbableAuthenticationError, aue: print >> sys.stderr, "Error: A Probable Authentication error occured: " + str(aue.message) except pika.exceptions.AMQPConnectionError, acoe: print >> sys.stderr, "Error: An AMQP Connection Error occured: " + str(acoe.message) except pika.exceptions.AMQPChannelError, ache: print >> sys.stderr, "Error: An AMQP Channel Error occured: " + str(ache.message)
def callback(ch, method, properties, body): print(f'Receive in main: {body}') data = json.loads(body) print(data) if properties.content_type == 'product created': product = Product(id=data['id'], title=data['title'], image=data['image']) db.session.add(product) db.session.commit() print("Product created") elif properties.content_type == 'product updated': product = Product.query.get(data['id']) product.title = data['title'] product.image = data['image'] db.session.commit() print("Product updated") elif properties.content_type == 'product deleted': product = Product.query.get(data) db.session.delete(product) db.session.commit() print("Product deleted") channel.basic_consume(queue='main', on_message_callback=callback, auto_ack=True) print('Started Consuming') channel.start_consuming() channel.close()
notify_admin_by_email(message) def main(): # Check motes status every 60 seconds interval_monitor = setInterval(checkMotesInactive, 60) try: connection = pika.BlockingConnection(pika.ConnectionParameters(host=EVENT_SERVER)) channel = connection.channel() channel.exchange_declare(exchange=EXCHANGE_READINGS, type='fanout') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange=EXCHANGE_READINGS, queue=queue_name) except Exception, e: print("Error %s:" % e.args[0]) finally: print 'Waiting for readings. To exit press CTRL+C' channel.basic_consume(callback, queue=queue_name, no_ack=True) try: channel.start_consuming() except: message = 'Error in connection to the event server' print message notify_admin_by_email(message) if __name__ == "__main__": main()
# # In the short report, you should document what type of queue you create # and the series of events that occur at your client and the message broker # when your client connects or disconnects. (Slide 10). channel = msg_broker.channel() my_queue = channel.queue_declare(exclusive=True) # TODO: Bind your queue to the message exchange, and register your # new message event handler channel.queue_bind(exchange="pi_utilization", queue=my_queue.method.queue, routing_key=topic) # TODO: Start pika's event loop channel.basic_consume(on_new_msg, queue=my_queue.method.queue, no_ack=True) print "Connecting to Server." channel.start_consuming() except pika.exceptions.AMQPError, ae: print "Error: An AMQP Error occured: " + ae.message except pika.exceptions.ChannelError, ce: print "Error: A channel error occured: " + ce.message except Exception, eee: print "Error: An unexpected exception occured: " + eee.message finally: # TODO: Attempt to gracefully shutdown the connection to the message broker