def _retry_message( name: str, retry_exchange: str, channel: channel.Channel, method: frame.Method, properties: spec.BasicProperties, body: str, delay: float, ) -> None: delay = int(delay * 1000) # Create queue that should be automatically deleted shortly after # the last message expires. The queue is re-declared for each retry # which resets the queue expiry. delay_name = '{}-delay-{}'.format(name, delay) result = channel.queue_declare( queue=delay_name, durable=True, arguments={ 'x-dead-letter-exchange': retry_exchange, 'x-message-ttl': delay, 'x-expires': delay + 10000, }, ) queue_name = result.method.queue # Bind the wait queue to the delay exchange before publishing channel.exchange_declare( exchange=delay_name, durable=True, auto_delete=True, # Delete exchange when queue is deleted exchange_type='topic') channel.queue_bind(exchange=delay_name, routing_key='#', queue=queue_name) channel.basic_publish(exchange=delay_name, routing_key=method.routing_key, body=body, properties=properties)
def __init__(self): _logger.info("Initializing Pika connection") self._connect() channel = self.get_channel() channel.exchange_declare(exchange="internal.edustor", exchange_type="topic", durable=True) channel.close() _logger.info("Pika initialization finished")
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])
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])
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 connect(self) -> None: """ For now we use a synchronous connection - caller is blocked until a message is added to the queue. We might switch to asynch connections should this incur noticable latencies. """ params = URLParameters(self.connection_settings) connection = BlockingConnection(params) channel = connection.channel() # set up the Exchange (if it does not exist) channel.exchange_declare(exchange=self.exchange, exchange_type=self.exchange_type, durable=True, auto_delete=False) self.connection = connection self.channel = channel
import pika, sys from pika import channel connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.exchange_declare(exchange='logs', exchange_type='fanout') message = ' '.join(sys.argv[1:]) or 'Hello World!' channel.basic_publish(exchange='logs', routing_key='', body=message) print(' [x] Sent %r' % message) connection.close()
usrname = credentials.split(':').__getitem__(0) password = credentials.split(':').__getitem__(1) # Check if there is a user with "usrname" else create it pika_credentials = pika.PlainCredentials(usrname, password) else: pika_credentials = None pika_parameters = pika.ConnectionParameters(host=host, virtual_host=vhost, credentials=pika_credentials) message_broker = pika.BlockingConnection(pika_parameters) print "A blocking connection named message_broker successfully created, now creating a channel" # Setup the channel and exchange channel = message_broker.channel() print "Channel created, now declaring exchange 'pi_utilization' with type 'direct'" # Exchange declaration, NOTE that this exchange is not a durable one channel.exchange_declare(exchange='pi_utilization', type='direct') print "Exchange declared successfully, now declaring an exclusive queue" # Setup signal handlers to shutdown this app when SIGINT or SIGTERM # is sent to this app signal_num = signal.SIGINT try: # Create a StatsClientChannelEvents object to store a reference # to the channel that will need to be shutdown if a signal is caught 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
message_broker = None channel = None try: # TODO: Connect to the message broker using the given broker address (host) # Use the virtual host (vhost) and credential information (credentials), # if provided msg_broker = pika.BlockingConnection( pika.ConnectionParameters(host=host, virtual_host=vhost, credentials=credentials)) # TODO: Setup the channel and exchange channel = msg_broker.channel() channel.exchange_declare( exchange="pi_utiization", type="direct") # Setup channel from connected message broker print "connected to message broker" # Setup signal handlers to shutdown this app when SIGINT or SIGTERM is # sent to this app # For more info about signals, see: https://scholar.vt.edu/portal/site/0a8757e9-4944-4e33-9007-40096ecada02/page/e9189bdb-af39-4cb4-af04-6d263949f5e2?toolstate-701b9d26-5d9a-4273-9019-dbb635311309=%2FdiscussionForum%2Fmessage%2FdfViewMessageDirect%3FforumId%3D94930%26topicId%3D3507269%26messageId%3D2009512 signal_num = signal.SIGINT try: # Create a StatsClientChannelEvents object to store a reference to # the channel that will need to be shutdown if a signal is caught channel_manager = StatsClientChannelHelper(channel) signal.signal(signal_num, channel_manager.stop_info_client) signal_num = signal.SIGTERM signal.signal(signal_num, channel_manager.stop_info_client)