async def new_ampq_sender(): # connect to the RabbitMQ broker connect = lambda: asynqp.connect('rabbitmq', 5672, username=os.getenv('RABBITMQ_USER', 'yo'), password=os.getenv('RABBITMQ_PASS', 'yo')) connection = await retry_fut( connect, (ConnectionError, ConnectionRefusedError, OSError)) # Open a communications channel channel = await connection.open_channel() # Create a queue and an exchange on the broker exchange = await channel.declare_exchange('email.exchange', 'direct') queue = await channel.declare_queue('email.queue') # Bind the queue to the exchange, so the queue will get messages published to the exchange await queue.bind(exchange, 'routing.key') def sender(payload): msg = asynqp.Message(payload) exchange.publish(msg, 'routing.key') async def finalizer(): await channel.close() await connection.close() return sender, finalizer
def setup_connection(loop): # connect to the RabbitMQ broker connection = yield from asynqp.connect('localhost', 5672, username='******', password='******') return connection
def set_up(): self.connection = yield from asynqp.connect(loop=self.loop) self.channel = yield from self.connection.open_channel() self.exchange = yield from self.channel.declare_exchange( '', 'direct') self.queue = yield from self.channel.declare_queue( durable=False, exclusive=True, auto_delete=True)
def hello_world(): """ Sends a 'hello world' message and then reads it from the queue. """ # connect to the RabbitMQ broker connection = yield from asynqp.connect('localhost', 5672, username='******', password='******') # Open a communications channel channel = yield from connection.open_channel() # Create a queue and an exchange on the broker exchange = yield from channel.declare_exchange('test.exchange', 'direct') queue = yield from channel.declare_queue('test.queue') # Bind the queue to the exchange, so the queue will get messages published to the exchange yield from queue.bind(exchange, 'routing.key') # If you pass in a dict it will be automatically converted to JSON msg = asynqp.Message({'hello': 'world'}) exchange.publish(msg, 'routing.key') # Synchronously get a message from the queue received_message = yield from queue.get() print(received_message.json()) # get JSON from incoming messages easily # Acknowledge a delivered message received_message.ack() yield from channel.close() yield from connection.close()
def setup(self): LOG.debug("Connecting to %s", self._config) self._connection = yield from asynqp.connect(host=self._config['host'], username=self._config['username'], password=self._config['password'], virtual_host=self._config['virtual_host']) self._channel = yield from self._connection.open_channel() exchange_type = self._config.get('exchange_type', 'topic') self._exchange = yield from self._channel.declare_exchange(self._config.get('exchange_name', "amq.{}".format(exchange_type)), exchange_type, durable=False, auto_delete=False)
def create_connection(): connection = yield from asynqp.connect(RABBIT_HOST, RABBIT_PORT, username=RABBIT_USERNAME, password=RABBIT_PASS, loop=asyncio.get_event_loop()) channel = yield from connection.open_channel() exchange = yield from channel.declare_exchange(RABBIT_EXCHANGE_NAME, 'topic') queue = yield from channel.declare_queue(RABBIT_QUEUE_NAME) yield from queue.bind(exchange, RABBIT_ROUTING_KEY) return channel, connection, queue, exchange, RABBIT_ROUTING_KEY
def init_logger(): from logger.config import MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, MESSAGE_QUEUE_USER, MESSAGE_QUEUE_PASSWORD messagequeue_conn = yield from asynqp.connect(MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, username=MESSAGE_QUEUE_USER, password=MESSAGE_QUEUE_PASSWORD) messagequeue_channel = yield from messagequeue_conn.open_channel() global messagequeue_exchange messagequeue_exchange = yield from messagequeue_channel.declare_exchange('peacock_job.exchange', 'direct') from config import BIND_ADDRESS yield from aiozmq.create_zmq_connection( lambda: LoggerZmqProtocol(), zmq.REP, bind='tcp://0.0.0.0:6000')
def connect(self): # connect to the RabbitMQ broker self.connection = yield from asynqp.connect(rabbitmq_host, 5672, username='******', password='******') # Open a communications channel self.channel = yield from self.connection.open_channel() # Create a queue and an exchange on the broker self.exchange = yield from self.channel.declare_exchange('test.exchange', 'direct') self.queue = yield from self.channel.declare_queue('test.queue') # Bind the queue to the exchange, so the queue will get messages published to the exchange yield from self.queue.bind(self.exchange, 'routing.key') yield from self.queue.purge()
def init_mq(): from jobserver.config import MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, MESSAGE_QUEUE_USER, MESSAGE_QUEUE_PASSWORD messagequeue_conn = yield from asynqp.connect(MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, username=MESSAGE_QUEUE_USER, password=MESSAGE_QUEUE_PASSWORD) messagequeue_channel = yield from messagequeue_conn.open_channel() messagequeue_exchange = yield from messagequeue_channel.declare_exchange('peacock_job.exchange', 'direct') import logging from jobserver.config import JOB_RING_SIZE for i in range(JOB_RING_SIZE): logging.info("Init Queue - %d" % i) queue = yield from messagequeue_channel.declare_queue('peacock_job_%d' % i) yield from queue.bind(messagequeue_exchange, 'peacock_job_%d' % i)
def init_monitor(): import asynqp global mq_connection, mq_channel from monitor.config import MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, MESSAGE_QUEUE_USER, MESSAGE_QUEUE_PASSWORD mq_connection = yield from asynqp.connect(MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, username=MESSAGE_QUEUE_USER, password=MESSAGE_QUEUE_PASSWORD) mq_channel = yield from mq_connection.open_channel() exchange = yield from mq_channel.declare_exchange('monitor_queue', 'topic') from monitor.messagequeue import set_channel, set_exchange set_channel(mq_channel) set_exchange(exchange)
def connect_and_consume(queue): # connect to the RabbitMQ broker connection = yield from asynqp.connect('localhost', 5672, username='******', password='******') try: channel = yield from connection.open_channel() amqp_queue = yield from channel.declare_queue('test.queue') consumer = Consumer(connection, queue) yield from amqp_queue.consume(consumer) except asynqp.AMQPError as err: print("Could not consume on queue", err) yield from connection.close() return None return connection
async def init_queue(self): # connect to the RabbitMQ broker connect = lambda: asynqp.connect('rabbitmq', 5672, username=os.getenv('RABBITMQ_USER', 'yo'), password=os.getenv('RABBITMQ_PASS', 'yo')) connection = await retry_fut(connect, (ConnectionError, ConnectionRefusedError, OSError)) # Open a communications channel channel = await connection.open_channel() # Create a queue and an exchange on the broker exchange = await channel.declare_exchange('email.exchange', 'direct') queue = await channel.declare_queue('emails.queue') await queue.bind(exchange, 'routing.key') self.queue = queue return queue
def init_jobserver(): """ Init Job Server MessageQueue랑 커넥션을 맺는다. """ global mq_connection, mq_channel from jobserver.config import MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, MESSAGE_QUEUE_USER, MESSAGE_QUEUE_PASSWORD mq_connection = yield from asynqp.connect(MESSAGE_QUEUE_ADDRESS, MESSAGE_QUEUE_PORT, username=MESSAGE_QUEUE_USER, password=MESSAGE_QUEUE_PASSWORD) mq_channel = yield from mq_connection.open_channel() from jobserver.messagequeue import set_exchange, set_monitor_queue exchange = yield from mq_channel.declare_exchange('peacock_job.exchange', 'direct') monitor_queue = yield from mq_channel.declare_exchange('monitor_queue', 'topic') set_exchange(exchange) set_monitor_queue(monitor_queue)
def connect_to_message_queue(self): """Connects to the amqp exchange and queue""" def log_returned_message(message): """Log when message has no handler in message queue""" LOGGER.info("Nobody cared for {0} {1}".format(message.routing_key, message.json())) self.connection = yield from asynqp.connect( RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_USERNAME, RABBITMQ_PASSWORD, RABBITMQ_VIRTUAL_HOST ) self.channel = yield from self.connection.open_channel() self.channel.set_return_handler(log_returned_message) self.exchange = yield from self.channel.declare_exchange(EXCHANGE, 'topic') self.queue = yield from self.channel.declare_queue(QUEUE, auto_delete=True) self.consumer = yield from self.queue.consume(self.handle_message)
def setup(self, user=None, password=None, exchange=None, host=None, port=None, queue_size=None): if host is None: host = 'localhost' if port is None: port = 5672 if user is None: user = '******' if password is None: password = '******' if exchange is None: exchange = 'snmp' self.connection = yield from asynqp.connect(host, port, username=user, password=password) self.channel = yield from self.connection.open_channel() if queue_size: yield from self.channel.set_qos(prefetch_count=queue_size) self.exchange = yield from self.channel.declare_exchange(exchange, 'direct', durable=True) self.task_queue = yield from self.channel.declare_queue('tasks', durable=True) yield from self.task_queue.bind(self.exchange, 'pending_task')
def setup(self, user=None, password=None, exchange=None, host=None, port=None, queue_size=None): if host is None: host = "localhost" if port is None: port = 5672 if user is None: user = "******" if password is None: password = "******" if exchange is None: exchange = "snmp" self.connection = yield from asynqp.connect(host, port, username=user, password=password) self.channel = yield from self.connection.open_channel() if queue_size: yield from self.channel.set_qos(prefetch_count=queue_size) self.exchange = yield from self.channel.declare_exchange(exchange, "direct", durable=True) self.task_queue = yield from self.channel.declare_queue("tasks", durable=True) yield from self.task_queue.bind(self.exchange, "pending_task")
async def init_queue(self): # connect to the RabbitMQ broker connect = lambda: asynqp.connect( 'rabbitmq', 5672, username=os.getenv('RABBITMQ_USER', 'yo'), password=os.getenv('RABBITMQ_PASS', 'yo')) connection = await retry_fut( connect, (ConnectionError, ConnectionRefusedError, OSError)) # Open a communications channel channel = await connection.open_channel() # Create a queue and an exchange on the broker exchange = await channel.declare_exchange('email.exchange', 'direct') queue = await channel.declare_queue('emails.queue') await queue.bind(exchange, 'routing.key') self.queue = queue return queue
def given_a_consumer(self): asynqp.routing._TEST = True self.loop = asyncio.get_event_loop() self.connection = self.loop.run_until_complete(asynqp.connect()) self.channel = self.loop.run_until_complete(self.connection.open_channel()) self.exchange = self.loop.run_until_complete( self.channel.declare_exchange(name='name', type='direct', durable=False, auto_delete=True)) self.queue = self.loop.run_until_complete( self.channel.declare_queue(name='', durable=False, exclusive=True, auto_delete=True)) self.loop.run_until_complete(self.queue.bind(self.exchange, 'name')) self.consumer = self.loop.run_until_complete( self.queue.consume(lambda x: x, exclusive=True) )
async def new_ampq_sender(): # connect to the RabbitMQ broker connect = lambda: asynqp.connect('rabbitmq', 5672, username=os.getenv('RABBITMQ_USER', 'yo'), password=os.getenv('RABBITMQ_PASS', 'yo')) connection = await retry_fut(connect, (ConnectionError, ConnectionRefusedError, OSError)) # Open a communications channel channel = await connection.open_channel() # Create a queue and an exchange on the broker exchange = await channel.declare_exchange('email.exchange', 'direct') queue = await channel.declare_queue('email.queue') # Bind the queue to the exchange, so the queue will get messages published to the exchange await queue.bind(exchange, 'routing.key') def sender(payload): msg = asynqp.Message(payload) exchange.publish(msg, 'routing.key') async def finalizer(): await channel.close() await connection.close() return sender, finalizer
def produce(): """ Sends a 'hello world' message and then reads it from the queue. """ # connect to the RabbitMQ broker connection = yield from asynqp.connect('localhost', 5672, username='******', password='******') # Open a communications channel channel = yield from connection.open_channel() # Create a queue and an exchange on the broker exchange = yield from channel.declare_exchange('test.exchange', 'direct') queue = yield from channel.declare_queue('test.queue') # Bind the queue to the exchange, so the queue will get messages published to the exchange yield from queue.bind(exchange, 'routing.key') for i in range(1,50000): # If you pass in a dict it will be automatically converted to JSON msg = asynqp.Message({'hello': 'world'}) exchange.publish(msg, 'routing.key') yield from channel.close() yield from connection.close()
def given_an_exception_handler_and_connection(self): self.loop = asyncio.get_event_loop() self.connection_lost_error_raised = False self.loop.set_exception_handler(self.exception_handler) self.connection = self.loop.run_until_complete(asynqp.connect())
def given_a_channel(self): self.loop = asyncio.get_event_loop() self.connection = self.loop.run_until_complete(asynqp.connect()) self.channel = self.loop.run_until_complete(self.connection.open_channel())
def given_a_connection(self): self.loop = asyncio.get_event_loop() self.connection = self.loop.run_until_complete(asyncio.wait_for(asynqp.connect(), 0.2))
def when_I_connect(self): self.connection = self.loop.run_until_complete(asyncio.wait_for(asynqp.connect(sock=self.sock), 0.2))
def given_a_connection(self): self.loop = asyncio.get_event_loop() self.connection = self.loop.run_until_complete( asyncio.wait_for(asynqp.connect(), 0.2))
def when_I_connect(self): self.connection = self.loop.run_until_complete( asyncio.wait_for(asynqp.connect(sock=self.sock), 0.2))
def setup(self): self.connection = yield from asynqp.connect('localhost', 5672, username='******', password='******') self.channel = yield from self.connection.open_channel() print("Connected")