def create_connection(): connection = BlockingConnection(connection_parameters) try: yield connection finally: connection.close()
class Publisher(object): connection = None channel = None exchange = None def __init__(self, props): self.props = props def start(self, exchange): self.exchange = exchange self.connection = BlockingConnection() self.connection.set_backpressure_multiplier(self.props.backpressure) self.channel = self.connection.channel() self.channel.queue_declare(queue=exchange, durable=True, exclusive=False, auto_delete=False) def publish(self, status): self.channel.basic_publish( exchange="", routing_key=self.exchange, body=status, properties=BasicProperties(content_type="text/plain", delivery_mode=1), ) def close(self): self.connection.close()
def _run_amqp(self): url = self.output_id.amqp_host_id.connection connection = BlockingConnection(URLParameters(url)) channel = connection.channel() result = channel.basic_publish(**self._generate_amqp_data()) _logger.debug(result) connection.close()
def send_msg_to_MQ( msg_data): # Build connection -> build channel -> send message #建立连接,然后发起通道,然后再发送信息 connection = BlockingConnection( ConnectionParameters(host=HOST_NAME, port=HOST_PORT, virtual_host='/', credentials=credentials)) channel = connection.channel() result = channel.queue_declare( queue='un_judged') # 声明消息队列,消息将在这个队列传递,如不存在,则创建 """ data: msg_data; @key='TaskID' value->str # 自动生成唯一的任务ID (自动生成) @key='studentNumber' value->str # 学号 @key='code' value->str # 需要评判的代码 @key='time' value->str # 当前的时间 (自动生成) """ TID = gen_task_ID(msg_data['studentNumber']) message = json.dumps({ 'TaskID': TID, 'code': msg_data['code'], 'time': current_datetime(), 'studentNumber': msg_data['studentNumber'] }) # build msg channel.basic_publish(exchange='', routing_key='un_judged', body=message) # 向队列插入数值 routing_key是队列名 connection.close() return TID
class Publisher(object): connection = None channel = None exchange = None def __init__(self, props): self.props = props def start(self, exchange): self.exchange = exchange self.connection = BlockingConnection() self.connection.set_backpressure_multiplier(self.props.backpressure) self.channel = self.connection.channel() self.channel.queue_declare(queue=exchange, durable=True, exclusive=False, auto_delete=False) def publish(self, status): self.channel.basic_publish(exchange="", routing_key=self.exchange, body=status, properties=BasicProperties( content_type="text/plain", delivery_mode=1)) def close(self): self.connection.close()
def connection_thread(url, results, hide_password=False): from oslo_config import cfg from oslo_messaging.transport import TransportURL from pika import exceptions as pika_exceptions from pika import URLParameters as PikaUrlParameters from pika import BlockingConnection as PikaBlockingConnection try: parsed_url = TransportURL.parse(cfg.CONF, url) if hide_password: url = re.sub(':+[^:@]+@', ':******@', url) except Exception as e: results.append({'url': url, 'exception': e}) else: test_url, driver = parse_test_url(parsed_url) try: if driver == 'kombu': connection = Connection(test_url) connection.connect() connection.close() elif driver == 'pika': params = PikaUrlParameters(test_url) params.socket_timeout = 5 conn = PikaBlockingConnection(params) conn.close() except (OSError, pika_exceptions.ConnectionClosed): results.append({'url': url, 'exception': _('Url not reachable')}) except (AccessRefused, pika_exceptions.ProbableAuthenticationError): results.append({ 'url': url, 'exception': _('Credentials incorrect') }) except Exception as e: results.append({'url': url, 'exception': force_text(e)}) else: results.append({'url': url})
class Service( object ): def __init__( self, topics, name, host = 'localhost', verbose = True, port = 5672, user = '', password = '' ): credentials = PlainCredentials(user, password) self.connection = BlockingConnection( ConnectionParameters( host, port, vhost, credentials ) ) self.channel = self.connection.channel() self.channel.exchange_declare( exchange = exchange_name, type = 'topic' ) self.queueID = self.channel.queue_declare( exclusive = True ).method.queue self.name = name for topic in topics: self.channel.queue_bind( exchange = exchange_name, queue = self.queueID, routing_key = topic) def _handle( self, c, m, p, b ): routingKey = self.name #'.'.join( [ 'SS', self.name ] ) print routingKey, b self.channel.basic_publish( exchange = exchange_name, routing_key = routingKey, body = b) def close( self ): self.channel.stop_consuming() print 'done', datetime.datetime.now() #for key, val in self._deposit.iteritems(): # print key, len( val ) self.connection.close() def run( self ): #_callback = lambda c, m, p, d: self._handle( d ) self.channel.basic_consume( self._handle, queue = self.queueID, no_ack = True ) self.channel.start_consuming()
class Covid19MQUtils: MQ_EXCHANGE_TYPE = "direct" MQ_EXCHANGE_NAME = '1606875806' MQ_HOST = "152.118.148.95" MQ_PORT = 5672 MQ_USERNAME = "******" MQ_PASSWORD = "******" MQ_VIRTUAL_HOST = "/0806444524" def __init__(self, routing_key, is_in_production=False): self.routing_key = routing_key self.pika_connection = BlockingConnection( ConnectionParameters(host=self.MQ_HOST, virtual_host=self.MQ_VIRTUAL_HOST, port=self.MQ_PORT, credentials=PlainCredentials( self.MQ_USERNAME, self.MQ_PASSWORD))) self.connection_channel = self.pika_connection.channel() self.connection_channel.exchange_declare( exchange=self.MQ_EXCHANGE_NAME, exchange_type=self.MQ_EXCHANGE_TYPE) def send_message(self, message): self.connection_channel.basic_publish(exchange=self.MQ_EXCHANGE_NAME, routing_key=self.routing_key, body=message) def close_connection(self): self.pika_connection.close()
class TopicClient(object): host = config["RABBIT_HOST"] port = config["RABBIT_PORT"] exchange = config["EXCHANGE"] def __init__(self): self.conn = BlockingConnection(ConnectionParameters(host=self.host, port=self.port)) self.channel = self.conn.channel() self.channel.exchange_declare(exchange=self.exchange, type="topic") def subscribe_to_topic(self, callback, topic): result = self.channel.queue_declare(exclusive=True) queue_name = result.method.queue self.channel.queue_bind(exchange=self.exchange, queue=queue_name, routing_key=topic) self.channel.basic_consume(callback, queue=queue_name, no_ack=True) def publish_to_topic(self, message, topic): self.channel.basic_publish(exchange=self.exchange, routing_key=topic, body=message) def run(self): print "Start something" try: self.channel.start_consuming() except KeyboardInterrupt: print "Closing" self.channel.stop_consuming() self.conn.close()
def send_msg_to_MQ( msg_data): # Build connection -> build channel -> send message #建立连接,然后发起通道,然后再发送信息 connection = BlockingConnection( ConnectionParameters(host=HOST_NAME, port=HOST_PORT, virtual_host='/', credentials=credentials)) channel = connection.channel() result = channel.queue_declare( queue='judged') # 声明消息队列,消息将在这个队列传递,如不存在,则创建 """ data: msg_data; @key='TaskID' value->str # 任务ID @key='studentNumber' value->str # 学号 @key='result' value->str # 代码评判和编译结果 @key='time' value->str # 代码提交的时间 """ message = json.dumps({ 'TaskID': msg_data['TaskID'], 'result': msg_data['result'], 'time': msg_data['time'], 'studentNumber': msg_data['studentNumber'] }) # build msg channel.basic_publish(exchange='', routing_key='judged', body=message) # 向队列插入数值 routing_key是队列名 connection.close()
def send_msg_example(queue: str, message: str): """Пример отправки сообщения в очередь :param queue: название очереди :param message: тело сообщения """ # подключаемся к серверу connection = BlockingConnection(ConnectionParameters('localhost')) channel = connection.channel() # проверяем, что очередь сущетсвует, или создаем новую # method = channel.queue_declare('') создаст временную очередь со случайным именем channel.queue_declare( queue=queue, # название durable=True, # объявить устойчивой ) # необязательно: создаём обменник и связываем с очередью # channel.exchange_declare('logs', exchange_type='fanout') # channel.queue_bind(method.method.queue, 'logs') # с типом fanout при отправке сообщения routing_key можно не указывать # отправляем сообщение channel.basic_publish( exchange='', # точка обмена routing_key=queue, # имя очереди body=message, # сообщение properties=BasicProperties( delivery_mode=2, # объявить устойчивым ) ) connection.close()
class Service( object ): def __init__( self, groups, host = 'localhost', verbose = True, port = 5672, user = '', password = '' ): credentials = PlainCredentials(user, password) self._connection = BlockingConnection( ConnectionParameters( host, port, vhost, credentials ) ) self._channel = self._connection.channel() self._channel.exchange_declare( exchange = exchange_name, type = 'topic' ) self._queueID = self._channel.queue_declare( exclusive = True ).method.queue for topic in groups: self._channel.queue_bind(exchange = exchange_name, queue = self._queueID, routing_key = topic) def _handle( self, c, m, p, b): print b def close( self ): self._channel.stop_consuming() print 'done', datetime.datetime.now() self._connection.close() def run( self ): self._channel.basic_consume( self._handle, queue = self._queueID, no_ack = True ) self._channel.start_consuming()
def main(): args = get_arguments() credentials = None if args.username and args.password: credentials = PlainCredentials(args.username, args.password) parameters = ConnectionParameters(host=args.host, port=args.port, credentials=credentials) connection = BlockingConnection(parameters) channel = connection.channel() response = channel.queue_declare(exclusive=True, auto_delete=True) queue = response.method.queue channel.queue_bind(exchange=args.exchange, queue=queue, routing_key=args.routing_key) channel.basic_consume(on_message, queue) try: channel.start_consuming() except KeyboardInterrupt: channel.stop_consuming() connection.close()
class RabbitMQConn(object): """ RabbitMQ client used to manager queues """ __metaclass__ = Singleton def __init__(self): self.params = ConnectionParameters() def connect(self): self.conn = BlockingConnection(parameters=self.params) self.ch = self.conn.channel() def remove_queue(self, queue_name): try: logging.info('Removing queue: ' + queue_name) self.ch.queue_purge(queue=queue_name) self.ch.queue_delete(queue=queue_name) except (exceptions.ChannelWrongStateError, exceptions.ChannelClosed) as error: logging.error('Err: ' + str(error)) def remove_all_queues(self): queues = [ 'knot-fog-message', 'knot-cloud-message', 'knot-control-message' ] for queue in queues: self.remove_queue(queue) def close(self): if self.ch.is_open: self.ch.close() if self.conn.is_open: self.conn.close()
def amq_channel(exchange): conn = BlockingConnection(ConnectionParameters(get_broker_address())) ch = conn.channel() ch.confirm_delivery() ch.exchange_declare(exchange=exchange, exchange_type='topic', durable=True) yield ch conn.close()
def send_message_queue(data): credentials = PlainCredentials('user', 'pass') connection = BlockingConnection( ConnectionParameters('rabbit', 5672, 'vhost', credentials)) channel = connection.channel() channel.queue_declare(queue='tasks') channel.basic_publish(exchange='', routing_key='tasks', body=dumps(data)) connection.close()
def _create_topic(address: str, topic: str, ssl: bool = False): host, port = address.split(':') connection = BlockingConnection(ConnectionParameters(host=host, port=port)) channel = connection.channel() channel.queue_declare(queue=topic) logging.info('Queue %s created', topic) connection.close()
def message(topic, message): connection = BlockingConnection() try: channel = connection.channel() props = BasicProperties(content_type='text/plain', delivery_mode=1) channel.basic_publish('incoming', topic, message, props) finally: connection.close()
class RabbitMQSender(object): __slots__ = [ "_configuration", "_connection", "_channel", "_queue", "_routing_key", "_exchange" ] def __init__(self, configuration): """ Create RabbitMQ Sender :param configuration: RabbitMQConfiguration object """ self._configuration = configuration self._connection = BlockingConnection( ConnectionParameters(host=self._configuration.host, port=self._configuration.port, virtual_host=self._configuration.virtual_host, credentials=self._configuration.credentials)) self._channel = self._connection.channel() self._queue = self._configuration.queue self._channel.queue_declare(queue=self._queue) self._routing_key = '' self._exchange = '' def __enter__(self): return self def set_routing_key(self, routing_key=''): """ Set routing key """ self._routing_key = routing_key def set_exchange(self, exchange=''): """ Set exchange """ self._exchange = exchange def publish(self, message): """ Publish message :param message: JSON string :return: None """ self._channel.basic_publish(exchange=self._exchange, routing_key=self._routing_key, body=message) logging.info(f"Message published to {self._queue} queue\n") def create_masstransit_response(self, message, request_body): response = { "messageId": request_body['messageId'], "conversationId": request_body['conversationId'], "sourceAddress": request_body['sourceAddress'], "destinationAddress": request_body['destinationAddress'], "messageType": ['urn:message:' + self._exchange], "message": message } return dumps(response) def __exit__(self, exc_type, exc_val, exc_tb): self._connection.close()
def publish(self, connection: pika.BlockingConnection, channel: pika.channel.Channel) -> None: """ Publishes a message to the channel """ kwargs = self.publish_kwargs() kwargs['properties'] = pika.BasicProperties(**self.properties()) channel.basic_publish(**kwargs) connection.close()
def consume(self) -> None: host, port = self.consumers[0].actor.service.address.split(':') connection_error_logged = False queue_error_logged = False while True: try: connection = BlockingConnection( ConnectionParameters(host=host, port=port)) try: self.channel = connection.channel() queue = self.consumers[0].topic if any(consumer.enable_topic_creation for consumer in self.consumers): self.channel.queue_declare(queue=queue) logging.info('Queue %s created', queue) else: self.channel.queue_declare(queue=queue, passive=True) exchange = '%s_%s' % (EXCHANGE, queue) self.channel.exchange_declare(exchange=exchange, exchange_type=EXCHANGE_TYPE) self.channel.queue_bind(exchange=exchange, queue=queue, routing_key='#') self.channel.basic_consume( queue=queue, on_message_callback=self.callback, auto_ack=True) try: self.channel.start_consuming() except StreamLostError: pass except AttributeError: pass break except ChannelClosedByBroker as e: connection.close() if not queue_error_logged: logging.info('Queue %s does not exist: %s', queue, e) queue_error_logged = True time.sleep(1) except AMQPConnectionError: if not connection_error_logged: logging.warning( 'Couldn\'t establish a connection to AMQP instance at %s:%s', host, port) connection_error_logged = True time.sleep(1) continue
class RabbitMqClient(object): def __init__(self, broker_url=broker_config.DEFAULT_BROKER_URL): self.broker_url = broker_url self.connection = None self.channel = None def open(self): try: self.connection = BlockingConnection( ConnectionParameters(self.broker_url)) except: sleep(1) self.connection = BlockingConnection( ConnectionParameters(self.broker_url)) self.channel = self.connection.channel() self.channel.exchange_declare(exchange=broker_config.REQUEST_EXCHANGE, exchange_type="topic") self.channel.exchange_declare(exchange=broker_config.STATUS_EXCHANGE, exchange_type="fanout") def close(self): self.connection.close() self.connection = None self.channel = None def send(self, write_request): if self.channel is None: raise RuntimeError("RabbitMqClient not connected.") routing_key = "*" body_bytes = json.dumps(write_request).encode() self.channel.basic_publish(exchange=broker_config.REQUEST_EXCHANGE, routing_key=routing_key, body=body_bytes) status_header = { "action": "write_request", "source": "BrokerClient", "routing_key": routing_key } self.channel.basic_publish( exchange=broker_config.STATUS_EXCHANGE, properties=BasicProperties(headers=status_header), routing_key=routing_key, body=body_bytes)
def auto_inspect(self): neid = '100000000013414' inspect_session_id = INSPECT_SESSION_ID # inspect_msg = Message(channel, properties={ # 'content-type': 'application/json' # }) # conn = Connection( # userid='guest', password='******', # virtual_host='/', host='192.168.35.172', # port=20555) items = [{"executive": "kmScript", "ctrlColId": "1000", "function": "LINUX_MAINFRAME_linux_mainframe_cmd_check", "configneid": "", "param": ""}, {"executive": "kmScript", "ctrlColId": "1005", "function": "LINUX_LOGICALVOLUME_linux_logicalvolume_cmd_check", "configneid": "", "param": ""}, {"executive": "kmScript", "ctrlColId": "1009", "function": "LINUX_SWAPPARTITION_linux_swapparttion_cmd_check", "configneid": "", "param": ""}, {"executive": "kmScript", "ctrlColId": "1010", "function": "LINUX_VOLUMEGROUP_linux_volumegroup_cmd_check", "configneid": "", "param": ""}] body = {"msgtype": "request", "business": "kmCheckScript", "body": { "neid": "900000012103258", "inspectSessionId": "{0}".format(inspect_session_id), "items": items }, "replyto": "itm.tom" } # body = {"msgtype":"request", # "business":"cmdCtrl", # "body": # { # "neid":"{0}".format(neid), # "inspectSessionId":"{0}".format(inspect_session_id), # "items": "[{0}]".format(items) # } # } conn = BlockingConnection(self.params) channel = conn.channel() for i in xrange(10): result = channel.basic_publish(exchange=AMQ_EXCHANGE, routing_key=AMQ_ROUTINGKEY, body=json.dumps(body), properties=self.properties) if result: logging.info('delivery comfirmed') logging.info('publish result: {0}'.format(result)) else: logging.info('delivery not confirmed') # time.sleep(40) channel.close() conn.close()
def send_queue_ambient(connection: pika.BlockingConnection, body): body['date'] = datetime.now() # logger.info(body) try: channel = connection.channel() channel.queue_declare(queue=rabbit_queue_ambient) channel.basic_publish(exchange='', routing_key=rabbit_queue_ambient, body=json.dumps(body, sort_keys=True, default=str)) connection.close() except Exception as err: logger.error(err)
def send(self, topic: str, event: Event): connection_publisher = BlockingConnection(self.connection_parameters) channel = connection_publisher.channel() channel.queue_declare(queue=topic, durable=True) channel.basic_publish( exchange="", routing_key=topic, body=event.to_json(), properties=BasicProperties( delivery_mode=2), # make message persistent ) connection_publisher.close()
def work_queue(): message = ' '.join(sys.argv[1:]) or "Hello World!" connection = BlockingConnection(ConnectionParameters('localhost')) channel = connection.channel() queue_name = 'work_queue' # create a work queue and send a message directly to it, bypassing the exchange channel.queue_declare(queue='work_queue', durable=True) channel.basic_publish(exchange='', routing_key='work_queue', body=message, properties=BasicProperties(delivery_mode=2)) print " [x] Sent '%s'" % (message) connection.close()
def post(self, message: str, queue_name: str): """ Posts the given message to a queue with the given name via the message broker's default exchange. :param message: the message to post :param queue_name: the name of the queue to post to """ connection = BlockingConnection(self._connection_parameters) try: channel = connection.channel() channel.basic_publish(exchange="", routing_key=queue_name, body=message) finally: connection.close()
class Service( object ): def __init__( self, host = 'localhost', port = 5672, user = '', password = '', vhost = '/', routingKey = ''): credentials = PlainCredentials( user, password ) self._connection = BlockingConnection( ConnectionParameters( host, port, vhost, credentials ) ) #self._connection = SelectConnection( ConnectionParameters( host, port, vhost, credentials ) ) self._channel = self._connection.channel() self._channel.exchange_declare( exchange = exchange_name, type = 'topic' ) self.rkey = routingKey def close( self ): self._connection.close() def run( self ): #message = raw_input("Message : ") while True: message = """ XKRX-CS-KR-000252,13:30:48.023942,7,290.9,123.19,90.82,79.62,937.15 XKRX-CS-KR-000253,13:30:48.024171,7,28.84,93.29,67.13,234.64,149.7 XKRX-CS-KR-000254,13:30:48.024337,7,248.17,118.49,1489.54,118.45,117.42 XKRX-CS-KR-000255,13:30:48.024497,7,70.67,170.82,65.45,152.11,420.7 XKRX-CS-KR-000256,13:30:48.034801,7,160.74,82.36,260.87,104.42,384.35 XKRX-CS-KR-000257,13:30:48.034973,7,123.39,150.31,60.78,201.21,181.55 XKRX-CS-KR-000100,13:30:48.035137,8,166.66,87.45,252.83,82.03,44.02 XKRX-CS-KR-000101,13:30:48.045434,8,114.86,1023.0,37.92,65.76,61.82 XKRX-CS-KR-000102,13:30:48.045586,8,159.16,97.96,60.07,75.29,690.15 XKRX-CS-KR-000103,13:30:48.045730,8,23.52,133.91,44.0,107.83,533.96 XKRX-CS-KR-000104,13:30:48.045901,8,76.62,274.25,166.57,116.48,149.1 XKRX-CS-KR-000250,13:30:48.056203,8,105.32,254.87,158.97,21.0,59.72 XKRX-CS-KR-000251,13:30:48.056364,8,192.7,226.26,76.02,72.7,40.53 XKRX-CS-KR-000252,13:30:48.056520,8,138.58,138.76,89.68,41.78,175.83 XKRX-CS-KR-000253,13:30:48.066883,8,88.67,41.84,126.81,222.26,8.98 XKRX-CS-KR-000254,13:30:48.067103,8,156.14,126.11,46.24,24.03,57.94 XKRX-CS-KR-000255,13:30:48.067259,8,136.01,35.25,25.29,275.88,50.33 XKRX-CS-KR-000256,13:30:48.067416,8,136.89,10.51,197.03,200.62,238.65 XKRX-CS-KR-000257,13:30:48.077776,8,47.36,41.77,101.75,105.99,64.56 XKRX-CS-KR-000100,13:30:48.078006,9,26.76,231.9,104.19,117.87,24.69 XKRX-CS-KR-000101,13:30:48.078187,9,57.14,84.92,73.62,33.72,47.86 XKRX-CS-KR-000102,13:30:48.088561,9,21.85,120.6,538.69,58.24,1685.93 XKRX-CS-KR-000103,13:30:48.088819,9,450.32,417.01,210.68,121.41,27.18 XKRX-CS-KR-000104,13:30:48.088998,9,80.61,69.15,132.51,98.67,226.2 XKRX-CS-KR-000250,13:30:48.089161,9,107.44,11.22,80.1,85.93,125.1 XKRX-CS-KR-000251,13:30:48.099518,9,43.86,51.79,282.43,101.35,946.29 XKRX-CS-KR-000252,13:30:48.099705,9,170.75,242.6,74.15,323.43,28.48 XKRX-CS-KR-000253,13:30:48.099871,9,53.27,36.47,81.75,50.96,46.73 XKRX-CS-KR-000254,13:30:48.110195,9,136.93,17.66,77.64,253.57,66.8 XKRX-CS-KR-000255,13:30:48.110408,9,65.49,72.59,39.59,63.07,74.31 XKRX-CS-KR-000256,13:30:48.110575,9,63.16,44.29,36.04,119.36,21.78 XKRX-CS-KR-000257,13:30:48.110733,9,125.17,54.65,374.91,219.27,136.63 """ self._channel.basic_publish( exchange = exchange_name, routing_key = self.rkey, body = message ) print 'Done', datetime.datetime.now(), ", Message :", message self.close()
class EchoServer(Thread): def __init__(self, url, listened_queue, request_exchange, response_exchange, response_routing_key): super(EchoServer, self).__init__() self.connection_parameters = URLParameters(url) self.listened_queue = listened_queue self.request_exchange = request_exchange self.response_exchange = response_exchange self.response_routing_key = response_routing_key self.channel = None self.connection = None self.event = Event() def run(self): self.connection = BlockingConnection(self.connection_parameters) self.channel = self.connection.channel() self.channel.queue_declare(queue=self.listened_queue, exclusive=True, durable=True, passive=False, auto_delete=True) self.channel.queue_bind(queue=self.listened_queue, exchange=self.request_exchange, routing_key=self.listened_queue) method_frame = None while method_frame is None: if self.event.is_set(): break method_frame, header_frame, body = self.channel.basic_get( self.listened_queue) if method_frame: self.channel.publish(exchange=self.response_exchange, routing_key=self.response_routing_key, body=body, properties=header_frame) self.channel.basic_ack(method_frame.delivery_tag) method_frame = None sleep(0.1) def stop(self): self.event.set() if self.channel: self.channel.stop_consuming() self.channel.close() if self.connection: self.connection.close()
class RabbitConnection: def __init__(self, host, port): self._host = host self._port = port self._connection = None def __enter__(self): self._connection = BlockingConnection( ConnectionParameters(host=self._host, port=self._port)) return self._connection.channel() def __exit__(self, cls, exception, tb): self._connection.close()
class PubMsg(): """Publish a message to a queue with exchange and routing key""" def __init__(self, queue, rabhost, user, passwd, msg, routing_key='py-fxp-publisher'): self.queue = queue self.host = rabhost self.topic_type = 'direct' self.user = user self.passwd = passwd self.result_exchange = 'py-fxp-publisher' self.publish_connection = BlockingConnection( ConnectionParameters(host=self.host, port=5672, virtual_host='/', retry_delay=3, connection_attempts=60, credentials=PlainCredentials( self.user, self.passwd))) self.publish_channel = self.publish_connection.channel() self.result_routing = routing_key self.msg = msg self.publish_channel.queue_declare(queue=self.queue, passive=False, durable=True, exclusive=False, auto_delete=False) def __call__(self): if self.queue is not None\ and self.topic_type is not None: self.publish_channel.\ exchange_declare(exchange='py-fxp-publisher') self.publish_channel.queue_bind(queue=self.queue, exchange=self.result_exchange, routing_key=self.result_routing) self.publish_channel.basic_publish( exchange=self.result_exchange, routing_key=self.result_routing, body=self.msg, properties=BasicProperties(content_type='application/json', delivery_mode=1)) LOGGER.info('Message published to exchange: %s with routing key: %s', self.result_exchange, self.result_routing) self.publish_connection.close() return True
def fanout_exchange(): message = ' '.join(sys.argv[1:]) or "Hello World!" connection = BlockingConnection(ConnectionParameters('localhost')) channel = connection.channel() exchange_name = 'analytics' # create a fanout exchange channel.exchange_declare(exchange=exchange_name, type='fanout') # send a task channel.basic_publish(exchange=exchange_name, routing_key='', body=message) print " [x] Sent '%s'" % (message) connection.close()
def send_message(connection: pika.BlockingConnection, queue: str, message: AnyStr) -> None: channel = connection.channel() channel.queue_declare(queue=queue, durable=True) # TODO: add queue exchange channel.basic_publish( exchange="", routing_key=queue, body=message, properties=pika.BasicProperties( delivery_mode=2, ), # make message persistent ) connection.close()
def replica_channel(settings): if current_transport is not RabbitMQTransport: pytest.skip("Replica channel is implemented only for RabbitMQTransport.") connection = BlockingConnection( parameters=URLParameters(settings.CQRS['url']), ) rabbit_mq_channel = connection.channel() rabbit_mq_channel.queue_purge('replica') rabbit_mq_channel.queue_purge('dead_letter_replica') yield rabbit_mq_channel connection.close()
def direct_exchange(): severity = sys.argv[1] message = ' '.join(sys.argv[2:]) or "Hello World!" message = severity + ": " + message connection = BlockingConnection(ConnectionParameters('localhost')) channel = connection.channel() # create a direct exchange channel.exchange_declare(exchange='direct_logs', type='direct') # send a task channel.basic_publish(exchange='direct_logs', routing_key=severity, body=message) print " [x] Sent '%s'" % (message) connection.close()
class BlockingChannel: """Provides raw blocking Pika channel object for given connection id. The created connection is closed automatically after 'with' statement has been closed. Usage:: from collective.zamqp.connection import BlockingChannel with BlockingChannel(connection_id) as channel: frame = channel.declare_queue(auto_delete=True) # ... Refer to Pika's API on how to use the raw channel-object. E.g. you could use it to declare a channel to retrieve the amount of messages still waiting on that channel. """ def __init__(self, connection_id, timeout=60): # Look up the given connection connection = getUtility(IBrokerConnection, name=connection_id) # Prepare a new one-shot blocking connection credentials = PlainCredentials( connection.username, connection.password, erase_on_connect=False) parameters = ConnectionParameters( connection.hostname, connection.port, connection.virtual_host, credentials=credentials, heartbeat=True) # AMQP-heartbeat timeout must be set manually due to bug in pika 0.9.5: if parameters.heartbeat: parameters.heartbeat = timeout # Create the connection and reset channel self.connection = BlockingConnection(parameters=parameters) self.channel = None def __enter__(self): # Open and return a new channel self.channel = self.connection.channel() return self.channel def __exit__(self, type, value, traceback): from pika import spec self.connection.callbacks.add( self.channel.channel_number, spec.Channel.CloseOk, self.channel.transport._on_rpc_complete ) self.channel.close() self.connection.close()
class MessageQueueUtils: MQ_DIRECT_EXCHANGE_TYPE = "direct" MQ_TOPIC_EXCHANGE_TYPE = "topic" MQ_DIRECT_EXCHANGE_NAME = "1606875806_DIRECT" MQ_TOPIC_EXCHANGE_NAME = "1606875806_TOPIC" MQ_HOST = "152.118.148.95" MQ_PORT = 5672 MQ_USERNAME = "******" MQ_PASSWORD = "******" MQ_VIRTUAL_HOST = "/0806444524" def __init__(self, routing_key, exchange_mode): self.routing_key = routing_key self.exchange_mode = exchange_mode self.exchange_name = self.MQ_DIRECT_EXCHANGE_NAME if self.exchange_mode == self.MQ_DIRECT_EXCHANGE_TYPE \ else self.MQ_TOPIC_EXCHANGE_NAME self.pika_connection = BlockingConnection( ConnectionParameters(host=self.MQ_HOST, virtual_host=self.MQ_VIRTUAL_HOST, port=self.MQ_PORT, credentials=PlainCredentials( self.MQ_USERNAME, self.MQ_PASSWORD))) self.connection_channel = self.pika_connection.channel() self.connection_channel.exchange_declare( exchange=self.exchange_name, exchange_type=self.exchange_mode) self.connection_channel.queue_declare(queue=self.routing_key) def send_message(self, message): self.connection_channel.basic_publish(exchange=self.exchange_name, routing_key=self.routing_key, body=message) def consume_message(self, callback_function): self.connection_channel.queue_bind(exchange=self.exchange_name, queue=self.routing_key) def __callback(ch, method, properties, body): callback_function(body) self.connection_channel.basic_consume(queue=self.routing_key, on_message_callback=__callback, auto_ack=True) self.connection_channel.start_consuming() def close_connection(self): self.pika_connection.close()
class BlockingChannel: """Provides raw blocking Pika channel object for given connection id. The created connection is closed automatically after 'with' statement has been closed. Usage:: from collective.zamqp.connection import BlockingChannel with BlockingChannel(connection_id) as channel: frame = channel.declare_queue(auto_delete=True) # ... Refer to Pika's API on how to use the raw channel-object. E.g. you could use it to declare a channel to retrieve the amount of messages still waiting on that channel. """ def __init__(self, connection_id, timeout=60): # Look up the given connection connection = getUtility(IBrokerConnection, name=connection_id) # Prepare a new one-shot blocking connection credentials = PlainCredentials(connection.username, connection.password, erase_on_connect=False) parameters = ConnectionParameters(connection.hostname, connection.port, connection.virtual_host, credentials=credentials, heartbeat=True) # AMQP-heartbeat timeout must be set manually due to bug in pika 0.9.5: if parameters.heartbeat: parameters.heartbeat = timeout # Create the connection and reset channel self.connection = BlockingConnection(parameters=parameters) self.channel = None def __enter__(self): # Open and return a new channel self.channel = self.connection.channel() return self.channel def __exit__(self, type, value, traceback): from pika import spec self.connection.callbacks.add(self.channel.channel_number, spec.Channel.CloseOk, self.channel.transport._on_rpc_complete) self.channel.close() self.connection.close()
def open_channel(self, exchange_name, exchange_type): logger.debug("Connecting to the RabbitMQ server.") connection = BlockingConnection(parameters=self.connection_parameters) channel = connection.channel() try: channel.exchange_declare(exchange=exchange_name, exchange_type=exchange_type) yield channel except Exception as e: logger.error(e) finally: connection.close() logger.debug("Connection closed.")
class Service(object): def __init__(self, topics, name, host='localhost', verbose=True, port=5672, user='', password=''): credentials = PlainCredentials(user, password) self.connection = BlockingConnection( ConnectionParameters(host, port, vhost, credentials)) self.channel = self.connection.channel() self.channel.exchange_declare(exchange=exchange_name, type='topic') self.queueID = self.channel.queue_declare(exclusive=True).method.queue self.name = name for topic in topics: self.channel.queue_bind(exchange=exchange_name, queue=self.queueID, routing_key=topic) def _handle(self, c, m, p, b): routingKey = self.name #'.'.join( [ 'SS', self.name ] ) print routingKey, b self.channel.basic_publish(exchange=exchange_name, routing_key=routingKey, body=b) def close(self): self.channel.stop_consuming() print 'done', datetime.datetime.now() #for key, val in self._deposit.iteritems(): # print key, len( val ) self.connection.close() def run(self): #_callback = lambda c, m, p, d: self._handle( d ) self.channel.basic_consume(self._handle, queue=self.queueID, no_ack=True) self.channel.start_consuming()
def get_from_queue(self, queue): """Get fact from front of queue.""" connection = BlockingConnection(ConnectionParameters(host=config['rabbit_host'], port=config['rabbit_port'])) channel = connection.channel() channel.exchange_declare(exchange=config['exchange'], type='topic') fact = self.wait_on_queue(channel, queue) connection.close() if fact is not None: self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(fact) else: self.send_response(204) self.send_header("Content-Type", "text/plain") self.end_headers()
def make_queue(self, topic): """Create a queue for the given topic.""" connection = BlockingConnection(ConnectionParameters(host=config['rabbit_host'], port=config['rabbit_port'])) channel = connection.channel() channel.exchange_declare(exchange=config['exchange'], type='topic') queue = channel.queue_declare().method.queue channel.queue_bind(exchange=config['exchange'], queue=queue, routing_key=topic) connection.close() out = {'queue_name': queue, 'queue_url': QUEUE_URL_TEMPLATE % (config['web_url'], queue,)} self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() dump(out, self.wfile)
def publish(self, topic): """Publish the given fact.""" content_len = int(self.headers.getheader('content-length', 0)) fact = self.rfile.read(content_len) connection = BlockingConnection(ConnectionParameters(host=config['rabbit_host'], port=config['rabbit_port'])) channel = connection.channel() channel.exchange_declare(exchange=config['exchange'], type='topic') channel.basic_publish(exchange=config['exchange'], routing_key=topic, body=fact) connection.close() self.send_response(202) self.send_header("Content-Type", "text/plain") self.end_headers()
class MQConnection(object): def __init__(self): self.connection = None self.channel = None pika_logger = logging.getLogger('pika') pika_logger.setLevel(logging.CRITICAL) def connect(self): self.connection = BlockingConnection(URLParameters(config.MQ_CONFIG)) self.channel = self.connection.channel() log.info("MQ: Connected") def is_closed(self): return self.connection.is_closed def publish(self, message): self.channel.basic_publish( exchange=config.MQ_EXCHANGE, routing_key=config.MQ_FROM_LISTENER, body=ujson.dumps(message, ensure_ascii=False), properties=BasicProperties( content_type="application/json", delivery_mode=2)) def consume(self): return self.channel.consume(config.MQ_TO_LISTENER, no_ack=False, inactivity_timeout=0.1) def cancel_consumer(self): self.channel.cancel() def ack(self, tag): self.channel.basic_ack(tag) def nack(self, tag): self.channel.basic_nack(tag, requeue=False) def close(self): self.channel.close() self.connection.close() log.info("MQ: Connection closed")
class SyncManager(BaseManager): def connect(self, **kwargs): try: self._connection = BlockingConnection( ConnectionParameters(**kwargs), ) self._channel = self._connection.channel() self._channel.queue_declare( queue = "" if self._dynamic_queue else self._queue, exclusive = self._exclusive, durable = self._persistent ) except AMQPConnectionError as e: logging.exception(e) self.reconnect(**kwargs) def reconnect(self, **kwargs): sleep(CONNECTION_RETRY_INTERVAL) self.connect(**kwargs) def close_connection(self): self._connection.close()
class Service( object ): def __init__( self, partition, interval = 5, duration = 360, seed = 0, host = 'localhost', verbose = True ): self._connection = BlockingConnection( ConnectionParameters( host = host ) ) self._channel = self._connection.channel() self._channel.exchange_declare( exchange = 'topic_logs', type = 'topic' ) self._partition = partition self._duration = duration self._interval = interval self._verbose = verbose random.seed( seed ) def _generate( self ): return tuple( [ int( 10000 * math.exp( random.gauss( 0, 1 ) ) ) * 0.01 for i in range( 0, 5 ) ] ) def close( self ): self._connection.close() def run( self ): for serial in range( 0, self._duration ): current = str( serial ) for group, codes in self._partition.iteritems(): routingKey = '.'.join( [ 'DS', group ] ) for code in codes: timeStamp = str( datetime.datetime.now().time() ) message = ','.join( [ code, timeStamp, current ] + [ str( t ) for t in self._generate() ] ) self._channel.basic_publish( exchange = 'topic_logs', routing_key = routingKey, body = message ) if self._verbose: print message time.sleep( self._interval ) print 'done', datetime.datetime.now() self.close()
class HunterRunner(object): def __init__(self, amqp_server, queue_name): self.parameters = ConnectionParameters(amqp_server) self.queue_name = queue_name self.done = False self.connection = None def halt_processing(self): if self.connection: self.connection.close() def process(self, channel, method, properties, body): try: packet = pickle.loads(body) closure = tuple(_make_cell(x) for x in packet[0]) code_object = marshal.loads(packet[1]) generated_function = types.FunctionType(code_object, globals(), '<network>', closure = closure) generated_function() except: traceback.print_exc() finally: channel.basic_ack(delivery_tag = method.delivery_tag) def run(self, *argv): self.connection = BlockingConnection(self.parameters) self.channel = self.connection.channel() try: self.channel.queue_declare(self.queue_name, durable = True) self.channel.basic_consume(self.process, queue = self.queue_name) except exceptions.ConnectionClosed: pass finally: if not self.connection.is_closed: self.connection.close()
class Service( object ): def __init__( self, topics, duration = 360, host_ = 'localhost', verbose = True ): self._connection = BlockingConnection( ConnectionParameters( host = host_ ) ) self._channel = self._connection.channel() self._channel.exchange_declare( exchange = 'topic_logs', type = 'topic' ) self._queueID = self._channel.queue_declare( exclusive = True ).method.queue for topic in topics: self._channel.queue_bind( exchange = 'topic_logs', queue = self._queueID, routing_key = topic ) self._duration = duration self._verbose = verbose def _handle( self, d ): v = d.split( ',' ) code = v[ 0 ] src = tuple( [ float( t ) for t in v[ 3: ] ] ) print code, v[ 2 ], max( src ), min( src ) def close( self ): self._channel.stop_consuming() print 'done', datetime.datetime.now() self._connection.close() def run( self ): _callback = lambda c, m, p, d: self._handle( d ) self._channel.basic_consume( _callback, queue = self._queueID, no_ack = True ) self._channel.start_consuming()
class Service( object ): def __init__( self, name, topics, duration = 360, dimension = 20, host_ = 'localhost', verbose = True ): self._connection = BlockingConnection( ConnectionParameters( host = host_ ) ) self._channel = self._connection.channel() self._channel.exchange_declare( exchange = 'topic_logs', type = 'topic' ) self._queueID = self._channel.queue_declare( exclusive = True ).method.queue for topic in topics: self._channel.queue_bind( exchange = 'topic_logs', queue = self._queueID, routing_key = topic ) self._name = name self._duration = duration self._dimension = dimension self._deposit = dict() self._verbose = verbose def _mockupInitialState( self, code ): return [ 100. ] * self._dimension def _mockupUpdateState( self, state, x ): p = tuple( [ 1. / max( 1 + t * t, 1.05 ) for t in range( 0, self._dimension ) ] ) z = [] for i in range( 0, self._dimension ): z.append( state[ i ] * ( 1 - p[ i ] ) + x * p[ i ] ) return tuple( z ) def _handle( self, d ): v = d.split( ',' ) code = v[ 0 ] if not self._deposit.has_key( code ): self._deposit[ code ] = [] prev = self._mockupInitialState( code ) else: prev = self._deposit[ code ][ -1 ][ -1 ] src = tuple( [ float( t ) for t in v[ 3: ] ] ) ups = self._mockupUpdateState( prev, sum( src ) / len( src ) ) toDeposit = v[ 1 ], int( v[ 2 ] ), src, ups self._deposit[ code ].append( toDeposit ) timeStamp = str( datetime.datetime.now().time() ) message = ','.join( [ code, timeStamp, v[ 2 ] ] + [ str( t ) for t in ups ] ) routingKey = '.'.join( [ 'SS', self._name ] ) self._channel.basic_publish( exchange = 'topic_logs', routing_key = routingKey, body = message ) if self._verbose: print message def close( self ): self._channel.stop_consuming() print 'done', datetime.datetime.now() for key, val in self._deposit.iteritems(): print key, len( val ) self._connection.close() def run( self ): _callback = lambda c, m, p, d: self._handle( d ) self._channel.basic_consume( _callback, queue = self._queueID, no_ack = True ) self._channel.start_consuming()
host='127.0.0.1', port=5672, virtual_host='/', credentials=PlainCredentials('guest', 'guest') ) ) def on_message(channel, method_frame, header_frame, body): channel.queue_declare(queue=body, auto_delete=True) if body.startswith("queue:"): queue = body.replace("queue:", "") key = body + "_key" print("Declaring queue %s bound with key %s" %(queue, key)) channel.queue_declare(queue=queue, auto_delete=True) channel.queue_bind(queue=queue, exchange="test_exchange", routing_key=key) else: print("Message body", body) channel.basic_ack(delivery_tag=method_frame.delivery_tag) channel = conn.channel() channel.exchange_declare(exchange="test", exchange_type="direct", passive=False, durable=True, auto_delete=False) channel.queue_declare(queue='queue1', durable=True) channel.queue_bind(queue="queue1", exchange="test", routing_key="key1") channel.basic_qos(prefetch_count=1) channel.basic_consume(on_message, queue='queue1') channel.start_consuming() print 'consume ok' conn.close()
QUERY_KEY = 'movement_successful_test' ROUTING_KEY = 'where_is_everyone' POSTGRES_HOST = 'microservices.cc9uedlzx2lk.eu-west-1.rds.amazonaws.com' POSTGRES_DATABASE = 'micro' POSTGRES_USER = '******' POSTGRES_PASSWORD = '******' def query(ch, method, properties, body): topic, content = method.routing_key, body conn = connect(host=POSTGRES_HOST, database=POSTGRES_DATABASE, user=POSTGRES_USER, password=POSTGRES_PASSWORD) cursor = conn.cursor() locations = cursor.execute('SELECT content->>player, content->>room FROM facts WHERE topic == QUERY_KEY;', ) cursor.close() conn.close() print 'Player, Room %s' % (locations,) host = '54.76.183.35' connection = BlockingConnection(ConnectionParameters(host=RABBIT_MQ_HOST, port=RABBIT_MQ_PORT)) channel = connection.channel() user = {'user': "******"} channel.basic_publish(exchange='alex2', routing_key=ROUTING_KEY, body=user) print 'Where is user %s' % (user,) connection.close()
class Agent(object): """Agent class""" def __init__(self, user, password, ip, port, metric_labels): """Generate metrics list, connect to RabbitMQ server""" # Add valid metrics to list self.metric_list = [] for label in metric_labels: metric = Metric.create(label) if metric is not None: self.metric_list.append(metric) # Connect to RabbitMQ server credentials = PlainCredentials(user, password) basicConfig(format='%(levelname)s:%(message)s', level=CRITICAL) connection_parameters = ConnectionParameters(ip, port, '/', credentials) self.connection = BlockingConnection(connection_parameters) self.channel = self.connection.channel() self.host = gethostname() # Prepare request queue self.processing_request = False self.channel.exchange_declare(exchange='request', type='fanout') result = self.channel.queue_declare(exclusive=True) request_queue = result.method.queue self.channel.queue_bind(exchange='request', queue=request_queue) self.channel.basic_consume(self.request_metric, queue=request_queue, no_ack=True) # Prepare reply queue self.channel.queue_declare(queue='reply') def request_metric(self, channel, method, properties, body): """Request local metrics and send them to the controller""" print 'Request received' if not self.processing_request: self.processing_request = True for metric in self.metric_list: # Get current time, host and metric metric_type = metric.get_type() value = metric.get_value() timestamp = [datetime.now().year, datetime.now().month, datetime.now().day, datetime.now().hour, datetime.now().minute, datetime.now().second, 0] # Send reply to controller reply = str([timestamp, self.host, metric_type, value]) self.channel.basic_publish(exchange='', routing_key='reply', body=reply) print 'Metric ' + metric_type + ' sent' self.processing_request = False def start_consuming(self): """Start consuming request messages""" self.channel.start_consuming() def stop_consuming(self): """Stop consuming request messages""" self.channel.stop_consuming() def disconnect(self): """Disconnect controller from RabbitMQ server""" self.connection.close()