def test_delete(self): name = 'delete-queue-name' rabbitpy.create_queue(queue_name=name) rabbitpy.delete_queue(queue_name=name) with rabbitpy.Connection() as conn: with conn.channel() as channel: obj = rabbitpy.Queue(channel, name) self.assertRaises(exceptions.AMQPNotFound, obj.declare, True)
def test_create(self): name = 'headers-exchange-name' rabbitpy.create_headers_exchange(exchange_name=name) with rabbitpy.Connection() as conn: with conn.channel() as channel: obj = rabbitpy.HeadersExchange(channel, name) obj.declare(True) obj.delete()
def test_create(self): name = 'topic-exchange-name' rabbitpy.create_topic_exchange(exchange_name=name) with rabbitpy.Connection() as conn: with conn.channel() as channel: obj = rabbitpy.TopicExchange(channel, name) obj.declare(True) obj.delete()
def setUp(self): self.connection = rabbitpy.Connection() self.channel = self.connection.channel() self.queue = rabbitpy.Queue(self.channel, exclusive=True, auto_delete=True) self.queue.declare() self.input = b'\n'.join(self.INPUT) + b'\n'
def test_create(self): name = 'fanout-exchange-name' rabbitpy.create_fanout_exchange(exchange_name=name) with rabbitpy.Connection() as conn: with conn.channel() as channel: obj = rabbitpy.FanoutExchange(channel, name) obj.declare(True) obj.delete()
def _connect(self): assert self.active_connections == 0 self.active_connections = 1 qs = urllib.parse.urlencode({ 'cacertfile': self.config['sslopts']['ca_certs'], 'certfile': self.config['sslopts']['certfile'], 'keyfile': self.config['sslopts']['keyfile'], 'verify': 'ignore', 'heartbeat': self.config['heartbeat'], 'connection_timeout': self.config['socket_timeout'], }) uri = '{scheme}://{username}:{password}@{host}:{port}/{virtual_host}?{query_str}'.format( scheme='amqps', username=self.config['userid'], password=self.config['password'], host=self.config['host'].split(":")[0], port=self.config['host'].split(":")[1], virtual_host=self.config['virtual_host'], query_str=qs, ) self.log.info("Initializing AMQP connection.") self.connection = rabbitpy.Connection(uri) # self.connection.connect() self.log.info("Connected. Creating channel.") # Channel and exchange setup self.channel = rabbitpy.AMQP( self.connection.channel(blocking_read=True)) self.log.info("Setting QoS.") self.log.info("Connection established. Setting up consumer.") if self.config['flush_queues']: self.log.info("Flushing items in queue.") self.channel.queue_purge(self.config['task_queue_name']) self.channel.queue_purge(self.config['response_queue_name']) self.log.info("Configuring queues.") self._setupQueues() if self.config['master']: self.in_queue = self.config['response_queue_name'] else: self.in_queue = self.config['task_queue_name'] qchan = self.connection.channel() qchan.prefetch_count(self.config['prefetch']) self.in_q = rabbitpy.Queue(qchan, self.in_queue)
def __init__(self, username, password, host, port, virtual_host, heartbeat=0): rabbit_url = f'amqp://{username}:{password}@{host}:{port}/{virtual_host}?heartbeat={heartbeat}' self.connection = rabbitpy.Connection(rabbit_url)
def test_create_queue(self): name = 'simple-create-queue' rabbitpy.create_queue(os.environ['RABBITMQ_URL'], queue_name=name) with rabbitpy.Connection(os.environ['RABBITMQ_URL']) as conn: with conn.channel() as channel: queue = rabbitpy.Queue(channel, name) response = queue.declare(True) self.assertEqual(response, (0, 0)) queue.delete()
def test_create(self): name = 'direct-exchange-name' rabbitpy.create_direct_exchange(os.environ['RABBITMQ_URL'], exchange_name=name) with rabbitpy.Connection(os.environ['RABBITMQ_URL']) as conn: with conn.channel() as channel: obj = rabbitpy.DirectExchange(channel, name) obj.declare(True) obj.delete()
def __init__(self, host: str = 'localhost', port: int = 5672): """ Wrapper class to store the connection to server globally. :param str host: Hostname of RabbitMQ Server :param int port: Port of RabbitMQ Server """ self.connection = rabbitpy.Connection('amqp://' + host + ':' + str(port))
def _connect(self): self._connection = rabbitpy.Connection( "{scheme}://{username}:{password}@" "{host}:{port}/{virtual_host}".format(scheme='amqp', username=self._user, password=self._passwd, host=self._host, port=self._port, virtual_host=self._vhost)) self._channel = self._connection.channel()
def publisher(conn): if not conn: conn = rabbitpy.Connection('amqp://*****:*****@localhost:5672/%2F') chan = conn.channel() for i in range(10000): routing_key = "teste_mira{}".format(randrange(10)) message = rabbitpy.Message(chan, '{}'.format(i)) message.publish('', routing_key) print("foi")
def test_delete(self): name = 'delete-exchange-name' rabbitpy.create_topic_exchange(os.environ['RABBITMQ_URL'], exchange_name=name) rabbitpy.delete_exchange(os.environ['RABBITMQ_URL'], exchange_name=name) with rabbitpy.Connection(os.environ['RABBITMQ_URL']) as conn: with conn.channel() as channel: obj = rabbitpy.TopicExchange(channel, name) self.assertRaises(exceptions.AMQPNotFound, obj.declare, True)
def __init__(self, exchange_name, uri=DEFAULT_RABBIT_URI): self.conn = rabbitpy.Connection(uri) self.channel = self.conn.channel() self.exchange = rabbitpy.Exchange(self.channel, exchange_name, exchange_type='fanout') self.exchange.declare() self.queue = rabbitpy.Queue(self.channel, exclusive=True) self.queue.declare() self.queue.bind(self.exchange)
def setup(self): self.connection = rabbitpy.Connection(config.get(AMQP_URI_KEY)) self.channel = self.connection.channel() self.exchange = rabbitpy.Exchange(self.channel, 'exch_pi') self.exchange.declare() queue = rabbitpy.Queue( self.channel, 'q_pi_buzzer', arguments={'x-message-ttl': 3600000}) # 1h queue.declare() queue.bind(self.exchange, ROUTING_KEY)
def consume_message(queue_name='hello', all=False): with rabbitpy.Connection(API_KEY) as conn: with conn.channel() as channel: queue = rabbitpy.Queue(channel, queue_name) if all: # Consume all the message for message in queue: message.pprint(True) message.ack() else: queue[0].pprint(True) return queue[0]
def __init__(self, server_uri): self.logger = logging.getLogger(__name__) self.server_uri = server_uri self._conn = rabbitpy.Connection(self.server_uri) #self._channel = self.conn.channel() self.thread_actions = [] self._threads = [] self.cmd_queue = Queue() self.add_thread_action(self._cmd_consume)
def moveToTop(config, queue, task_id): import threading lock = threading.Lock() lock.acquire() rc = 0 try: with rabbitpy.Connection('amqp://{}:{}@{}:{}/%2F'.format( g_username, g_password, config["rabbitMQ_address"], config["rabbitMQ_port"])) as conn: with conn.channel() as channel: # dedicated queue would be consumed firstly q = rabbitpy.Queue(channel, queue) q.durable = True q.declare() channel.enable_publisher_confirms() _t = [] _r = '' for i in range(0, q.__len__()): msg = q.get() msg.ack() task = json.loads(msg.body) if task["task_id"] == task_id: _r = msg.body else: _t.append(msg.body) _t.insert(0, _r) for i in _t: msg = rabbitpy.Message(channel, i) # Publish the message, looking for the return value to be a bool True/False if msg.publish("", queue, mandatory=True): LOGGER.debug( 'Message {} publish confirmed by RabbitMQ'.format( msg.body)) else: LOGGER.error( 'Message {} publish not confirmed by RabbitMQ'. format(msg.body)) rc = -1 except: LOGGER.error(traceback.format_exc()) rc = -1 finally: lock.release() return rc #print queryTask({"rabbitMQ_address":'127.0.0.1', 'rabbitMQ_port':5672}, '127.0.0.1', 'APL', ['PIT', 'CIT'], ['OTM', 'PV']) #print queryTask1({"rabbitMQ_address":'10.239.111.152', 'rabbitMQ_port':5672},"10.239.132.227", "APL",["CIT", "PIT"], ["OTM", "PV"]) #a = getDedicatedTaskQueue({"rabbitMQ_address":'10.239.153.126', 'rabbitMQ_port':5672},"10.239.132.227") #b = getDedicatedTaskQueue_pika({"rabbitMQ_address":'10.239.153.126', 'rabbitMQ_port':5672},"APL_OTM_CIT") #print(type(b[0]), b)
def _connect(self): self._connection = rabbitpy.Connection() self._channel = self._connection.channel() self._channel.enable_publisher_confirms() self._exchange = rabbitpy.Exchange(self._channel, self.EXCHANGE_NAME, self.EXCHANGE_TYPE) self._exchange.declare() self._queue = rabbitpy.Queue(self._channel, self.QUEUE_NAME) self._queue.declare() self._queue.bind(self._exchange, self.ROUTING_KEY)
def test_ha_queues(self): with self.subTest("HA policy for all"): with rabbitpy.Connection(URL) as connection: with connection.channel() as channel: exchange = declare_exchange(channel, 'ha-policy-all-exchange') arguments = {'x-ha-policy': 'all'} queue = declare_queue(channel, '', arguments=arguments) with self.subTest("HA policy for nodes"): with rabbitpy.Connection(URL) as connection: with connection.channel() as channel: exchange = declare_exchange(channel, 'ha-policy-nodes-exchange') arguments = { 'x-ha-policy': 'nodes', 'x-ha-nodes': ['rabbit@node1', 'rabbit@node2' ] # node1 and node2 are not necessarily to be there } queue = declare_queue(channel, '', arguments=arguments)
def setUp(self): self.connection = rabbitpy.Connection(os.environ['RABBITMQ_URL']) self.channel = self.connection.channel() self.queue = rabbitpy.Queue(self.channel, 'redeliver-test') self.queue.declare() # Publish the message that will be rejected message = rabbitpy.Message(self.channel, 'Payload Value') message.publish('', 'redeliver-test') # Get and reject the message msg1 = self.queue.get() msg1.reject(requeue=True)
def __init__(self, retries=100): self._uri = Config.get('rabbit', 'uri') tries = 0 connected = False while tries < retries and not connected: try: self._conn = rabbitpy.Connection(self._uri) connected = True except RuntimeError: time.sleep(0.1) if not connected: raise RuntimeError("Could not connect to RabbitMQ.") self._ch = self._conn.channel() self._ch.prefetch_count(value=1, all_channels=True)
def test_delivery_mode_2(self): with rabbitpy.Connection(URL) as connection: with connection.channel() as channel: exchange = declare_exchange(channel, 'test-delivery-mode2-exchange') queue = declare_queue(channel, "") origin_len = len(queue) bind(queue, exchange, "my-routing-key") message = rabbitpy.Message(channel, 'test', { **properties(), 'delivery_mode': 2 }) message.publish(exchange, 'my-routing-key') time.sleep(1) self.assertEqual(len(queue), origin_len + 1)
def setUp(self): self.connection = rabbitpy.Connection(os.environ['RABBITMQ_URL']) self.channel = self.connection.channel() self.channel.enable_publisher_confirms() self.exchange = rabbitpy.TopicExchange(self.channel, 'pql-test') self.exchange.declare() self.queue = rabbitpy.Queue(self.channel, 'pql-queue') self.queue.declare() self.queue.bind(self.exchange, 'test.#') for iteration in range(0, self.ITERATIONS): message = rabbitpy.Message(self.channel, str(uuid.uuid4())) if not message.publish(self.exchange, 'test.publish.pql'): LOGGER.error('Error publishing message %i', iteration)
def setup_connection(queuename): """Setup and run.""" def consume(message): run(message.body) message.ack() # Use context managers as we had some strange thread issues otherwise? with rabbitpy.Connection(URL) as conn: with conn.channel() as channel: channel.prefetch_count(10) queue = rabbitpy.Queue(channel, name=queuename, durable=True) print(queue) for message in queue: consume(message) print("done")
def deliver_etl(result): queue_name = 'ml_events' with rabbitpy.Connection() as conn: logger.debug("Connected to RabbitMQ") with conn.channel() as channel: channel.enable_publisher_confirms() queue = rabbitpy.Queue(channel, queue_name) queue.durable = True queue.declare() message = rabbitpy.Message(channel, json.dumps(result)) logger.info("ML Event message pushed to RabbitMQ") if not message.publish('', queue_name, mandatory=True): logger.error("Message failed to publish data for stream id: {0}")
def getConnection(url,moduleType): print '@@@@ Getting a connection' print moduleType try: connection = connections[moduleType]['connection'] connections[moduleType]['count'] += 1 return connection except KeyError,e: print '!!!! Instantiating a new connection' print url connections[moduleType] = {} connections[moduleType]['connection'] = rabbitpy.Connection(url) connections[moduleType]['count'] = 1 my_globals['connection_count'] += 1 return connections[moduleType]['connection']
def test_publisher_confirms(self): with rabbitpy.Connection(URL) as connection: with connection.channel() as channel: exchange = declare_exchange(channel, 'publisher-comfirms') queue = declare_queue(channel, "test-publisher-confirms") bind(queue, exchange, "my-routing-key") # Prior to publishing any messages, a message publisher must issue a Confirm.Select RPC request to RabbitMQ and # wait for a Confirm.SelectOk response to know that delivery confirmations are enabled channel.enable_publisher_confirms() body = 'test' message = rabbitpy.Message(channel, body, properties('text/plain')) ack = message.publish(exchange, 'my-routing-key') self.assertTrue(ack) ack = message.publish(exchange, 'no-routing-key') self.assertTrue(ack) # although no route, still ack
def test_exception_is_raised(self): url_parts = parse.urlsplit(os.environ['RABBITMQ_URL']) netloc = '{}:{}@{}:{}'.format( uuid.uuid4().hex, uuid.uuid4().hex, url_parts.hostname, url_parts.port or 5672, ) url = parse.urlunsplit(( url_parts.scheme, netloc, url_parts.path, url_parts.query, url_parts.fragment, )) with self.assertRaises(exceptions.AMQPAccessRefused): rabbitpy.Connection(url)
def __init__(self, exchange_name, queue_name): self.exchange_name = exchange_name self.queue_name = queue_name self.routing_key = queue_name self.connection = rabbitpy.Connection(RABITMQ_URL) self.channel = self.connection.channel() self.exchange = rabbitpy.Exchange( self.channel, self.exchange_name, durable=True) self.exchange.declare() self.queue = rabbitpy.Queue( self.channel, self.queue_name, durable=True) self.queue.declare() self.queue.bind(self.exchange_name, routing_key=self.routing_key)