def send_message(self, exchange, routing_key, msg, mandatory=False, headers=None, declareExchange=True): body = msg headers = headers if headers else {} # it is a protobuf if not isinstance(msg, basestring): body = msg.SerializeToString() headers['X-Protobuf-FullName'] = msg.DESCRIPTOR.full_name queueSchema = self.factory.queueSchema exchangeConfig = queueSchema.getExchange(exchange) if declareExchange: # Declare the exchange to which the message is being sent yield getAdapter(self.chan, IAMQPChannelAdapter).declareExchange(exchangeConfig) if exchangeConfig.compression == 'deflate': body = zlib.compress(body) content = Content(body) content.properties['delivery-mode'] = exchangeConfig.delivery_mode content.properties['headers'] = headers content.properties['content-type'] = 'application/x-protobuf' if exchangeConfig.compression == 'deflate': content.properties['content-encoding'] = 'deflate' # Publish away yield self.chan.basic_publish(exchange=exchangeConfig.name, routing_key=routing_key, content=content, mandatory=mandatory) returnValue("SUCCESS")
def __init__(self, body = "", children = None, properties = None, pickleProtocol = 2, prePickle = False): self.pickleProtocol = pickleProtocol if prePickle == True: body = pickle.dumps(body, self.pickleProtocol) Content.__init__(self, body, children, properties)
def __init__(self, message_status, msgid, dlr_url, dlr_level, id_smsc='', sub='', dlvrd='', subdate='', donedate='', err='', text='', method='POST', trycount=0): # ESME_* statuses are returned from SubmitSmResp # Others are returned from DeliverSm, values must be the same as Table B-2 if message_status[:5] != 'ESME_' and message_status not in [ 'DELIVRD', 'EXPIRED', 'DELETED', 'UNDELIV', 'ACCEPTD', 'UNKNOWN', 'REJECTD']: raise InvalidParameterError("Invalid message_status: %s" % message_status) if dlr_level not in [1, 2, 3]: raise InvalidParameterError("Invalid dlr_level: %s" % dlr_level) if method not in ['POST', 'GET']: raise InvalidParameterError('Invalid method: %s' % method) properties = {'message-id': msgid, 'headers': {'try-count': 0, 'url': dlr_url, 'method': method, 'message_status': message_status, 'level': dlr_level, 'id_smsc': id_smsc, 'sub': sub, 'dlvrd': dlvrd, 'subdate': subdate, 'donedate': donedate, 'err': err, 'text': text}} Content.__init__(self, msgid, properties=properties)
def __init__(self, bid, uid, amount): if not isinstance(amount, float) and not isinstance(amount, int): raise InvalidParameterError('Amount is not float or int: %s' % amount) if amount < 0: raise InvalidParameterError('Amount cannot be a negative value: %s' % amount) properties = {'message-id': bid, 'headers': {'user-id': uid, 'amount': str(amount)}} Content.__init__(self, bid, properties=properties)
def __init__(self, bid, uid, amount): if type(amount) != float and type(amount) != int: raise InvalidParameterError('Amount is not float or int: %s' % amount) if amount < 0: raise InvalidParameterError('Amount cannot be a negative value: %s' % amount) properties = {} properties['message-id'] = bid properties['headers'] = {'user-id': uid, 'amount': str(amount)} Content.__init__(self, bid, properties = properties)
def __init__(self, body="", children=None, properties=None, pickleProtocol=2, prePickle=False): self.pickleProtocol = pickleProtocol if prePickle is True: body = pickle.dumps(body, self.pickleProtocol) # Add creation date in header if 'headers' not in properties: properties['headers'] = {} properties['headers']['created_at'] = str(datetime.datetime.now()) Content.__init__(self, body, children, properties)
def __init__(self, message_status, msgid, system_id, source_addr, destination_addr): properties = {} # ESME_* statuses are returned from SubmitSmResp # Others are returned from DeliverSm, values must be the same as Table B-2 if message_status[:5] != 'ESME_' and message_status not in ['DELIVRD', 'EXPIRED', 'DELETED', 'UNDELIV', 'ACCEPTD', 'UNKNOWN', 'REJECTD']: raise InvalidParameterError("Invalid message_status: %s", message_status) properties['message-id'] = msgid properties['headers'] = {'try-count': 0, 'message_status': message_status, 'system_id': system_id, 'source_addr': source_addr, 'destination_addr': destination_addr} Content.__init__(self, msgid, properties = properties)
def __init__(self, pdu_type, msgid, status, smpp_msgid=None, cid=None, dlr_details=None): pdu_type_s = '%s' % pdu_type status_s = '%s' % status if pdu_type_s not in ['deliver_sm', 'data_sm', 'submit_sm_resp']: raise InvalidParameterError('Invalid pdu_type: %s' % pdu_type_s) if pdu_type_s == 'submit_sm_resp' and status_s == 'ESME_ROK' and smpp_msgid is None: raise InvalidParameterError('submit_sm_resp with ESME_ROK dlr must have smpp_msgid arg defined') elif pdu_type_s in ['deliver_sm', 'data_sm'] and (cid is None or dlr_details is None): raise InvalidParameterError('deliver_sm dlr must have cid and dlr_details args defined') properties = {'message-id': str(msgid), 'headers': {'type': pdu_type_s}} if pdu_type_s == 'submit_sm_resp' and smpp_msgid is not None: # smpp_msgid is used to define mapping between msgid and smpp_msgid (when receiving submit_sm_resp ESME_ROK) properties['headers']['smpp_msgid'] = str(smpp_msgid).upper().lstrip('0') elif pdu_type_s in ['deliver_sm', 'data_sm']: properties['headers']['cid'] = cid for k, v in dlr_details.iteritems(): properties['headers']['dlr_%s' % k] = v Content.__init__(self, status_s, properties=properties)
def assertPublishGet(self, queue, exchange="", routing_key="", properties=None): """ Publish to exchange and assert queue.get() returns the same message. """ body = self.uniqueString() self.channel.basic_publish(exchange=exchange, content=Content(body, properties=properties), routing_key=routing_key) msg = yield queue.get(timeout=1) self.assertEqual(body, msg.content.body) if (properties): self.assertEqual(properties, msg.content.properties)
def readContent(queue): frame = yield queue.get() header = frame.payload children = [] for i in range(header.weight): content = yield readContent(queue) children.append(content) size = header.size read = 0 buf = StringIO() while read < size: body = yield queue.get() content = body.payload.content buf.write(content) read += len(content) defer.returnValue(Content(buf.getvalue(), children, header.properties.copy()))
def test_channel_flow(self): channel = self.channel yield channel.queue_declare(queue="flow_test_queue", exclusive=True) yield channel.basic_consume(consumer_tag="my-tag", queue="flow_test_queue") incoming = yield self.client.queue("my-tag") yield channel.channel_flow(active=False) channel.basic_publish(routing_key="flow_test_queue", content=Content("abcdefghijklmnopqrstuvwxyz")) try: yield incoming.get(timeout=1) self.fail("Received message when flow turned off.") except Empty: None yield channel.channel_flow(active=True) msg = yield incoming.get(timeout=1) self.assertEqual("abcdefghijklmnopqrstuvwxyz", msg.content.body)
def publish3(result, message, count_): connection, channel = result yield channel.exchange_declare(exchange=EXCHANGE_NAME, type='fanout') for i in range(count_): msg = Content('%s [%04d]' % ( message, i, )) yield channel.basic_publish(exchange=EXCHANGE_NAME, routing_key='', content=msg) print ' [x] Sent "%s [%04d]"' % ( message, i, ) returnValue(result)
def publish1(result, message, count_): connection, channel = result yield channel.queue_declare(queue=QUEUE_NAME) for i in range(count_): msg = Content('%s [%04d]' % ( message, i, )) yield channel.basic_publish(exchange='', routing_key=QUEUE_NAME, content=msg) print ' [x] Sent "%s [%04d]"' % ( message, i, ) returnValue(result)
def test_reject_message(self): """ C{reject_message} puts back a message in the queue so that it's available to subsequent C{get_message} calls. """ yield self.manager.connected((self.client, self.channel)) yield self.channel.queue_declare(queue=self.queue_prefix + "uuid1") content = Content("some content") yield self.channel.basic_publish(routing_key=self.queue_prefix + "uuid1", content=content) message, tag = yield self.manager.get_message("uuid1", "0") yield self.manager.reject_message(tag) message2, tag2 = yield self.manager.get_message("uuid1", "1") self.assertEqual(message2, "some content")
def writeMetric(metric_path, value, timestamp, host, port, username, password, vhost, exchange, spec=None, channel_number=1, ssl=False): if not spec: spec = txamqp.spec.load( os.path.normpath( os.path.join(os.path.dirname(__file__), 'amqp0-8.xml'))) delegate = TwistedDelegate() connector = ClientCreator(reactor, AMQClient, delegate=delegate, vhost=vhost, spec=spec) if ssl: from twisted.internet.ssl import ClientContextFactory conn = yield connector.connectSSL(host, port, ClientContextFactory()) else: conn = yield connector.connectTCP(host, port) yield conn.authenticate(username, password) channel = yield conn.channel(channel_number) yield channel.channel_open() yield channel.exchange_declare(exchange=exchange, type="topic", durable=True, auto_delete=False) message = Content("%f %d" % (value, timestamp)) message["delivery mode"] = 2 channel.basic_publish(exchange=exchange, content=message, routing_key=metric_path) yield channel.channel_close()
def test_requeue_all_restart_then_reconsume(self): "Related to #67, Starting consuming with a " yield self.connect() yield self.amqp.chan.exchange_declare(exchange='messaging', type='topic') # Setup Consumer yield self.amqp.named_queue_declare(queue="submit.sm_all_2") yield self.amqp.chan.queue_bind(queue="submit.sm_all_2", exchange="messaging", routing_key="submit.sm.*") yield self.amqp.chan.basic_consume(queue="submit.sm_all_2", no_ack=False, consumer_tag='qtag') queue = yield self.amqp.client.queue('qtag') # Start consuming through _callback_reject_and_requeue_all queue.get().addCallback(self._callback_reject_and_requeue_all, queue).addErrback(self._errback) # Publish for i in range(50): yield self.amqp.publish(exchange='messaging', routing_key="submit.sm.connector01", content=Content(str(i))) # Wait for 2 seconds # (give some time to the consumer to get its work done) yield waitFor(2) # Stop consuming and assert yield self.amqp.chan.basic_cancel(consumer_tag='qtag') self.assertGreaterEqual(self.rejectedMessages, 50) self.assertEqual(self.consumedMessages, 0) # Wait for 2 seconds # (give some time to the consumer to get its work done) yield waitFor(2) # Start consuming again yield self.amqp.chan.basic_consume(queue="submit.sm_all_2", no_ack=False, consumer_tag='qtag') queue = yield self.amqp.client.queue('qtag') # Consuming through _callback queue.get().addCallback(self._callback, queue).addErrback(self._errback) # Wait for 2 seconds # (give some time to the consumer to get its work done) yield waitFor(2) # Stop consuming and assert yield queue.close() self.assertEqual(self.consumedMessages, 50) yield self.amqp.disconnect()
def test_wb_heartbeat(self): """ If heartbeat checks fail due to network issues, we keep re-trying until the network recovers. """ self.service.stopService() # Put a TCP proxy between NotificationSource and RabbitMQ, to simulate # packets getting dropped on the floor. proxy = ProxyService(self.rabbit.config.hostname, self.rabbit.config.port) proxy.startService() self.addCleanup(proxy.stopService) self.endpoint._port = proxy.port self.service = ClientService(self.endpoint, self.factory, retryPolicy=self.policy) self.connector._service = self.service self.service.startService() # This will make the connector setup the channel before we call # get(), so by the time we call it in the next line all # connector-related deferreds will fire synchronously and the # code will block on basic-consume. channel = yield self.connector() deferred = self.source.get("uuid", 0) # Start dropping packets on the floor proxy.block() # Publish a notification, which won't be delivered just yet. yield self.channel.basic_publish(routing_key="uuid", content=Content("hello")) # Wait for the first connection to terminate, because heartbeat # checks will fail. yield channel.client.disconnected.wait() # Now let packets flow again. proxy.unblock() # The situation got recovered. notification = yield deferred self.assertEqual("hello", notification.payload) self.assertEqual(2, proxy.connections)
def test_get_message(self): """ C{get_message} returns the message exposed to a previously created queue. """ yield self.manager.connected((self.client, self.channel)) yield self.channel.queue_declare(queue=self.queue_prefix + "uuid1", auto_delete=True) content = Content("some content") yield self.channel.basic_publish(routing_key=self.queue_prefix + "uuid1", content=content) message = yield self.manager.get_message("uuid1", "0") self.assertEqual(message[0], "some content") self.assertNotIn(self.tag_prefix + "uuid1.0", self.client.queues)
def send_msg_to_worker(self, data): try: routing_keys_worker = config.WORKER_QUEUE_NAME channel = self.get_channel() msg = Content(json_helper.dumps(data)) msg["delivery mode"] = 1 exchange_name_worker = config.EXCHANGE_NAME_WORKER d = channel.basic_publish(exchange=exchange_name_worker, content=msg, routing_key=routing_keys_worker) print '=====send msg to worker====' print msg print '===========================' d.addErrback(self.errb) except: print traceback.format_exc()
def test_qos_prefetch_count(self): """ Test that the prefetch count specified is honoured """ #setup: declare queue and subscribe channel = self.channel yield channel.queue_declare(queue="test-prefetch-count", exclusive=True) subscription = yield channel.basic_consume(queue="test-prefetch-count", no_ack=False) queue = yield self.client.queue(subscription.consumer_tag) #set prefetch to 5: yield channel.basic_qos(prefetch_count=5) #publish 10 messages: for i in range(1, 11): channel.basic_publish(routing_key="test-prefetch-count", content=Content("Message %d" % i)) #only 5 messages should have been delivered: for i in range(1, 6): msg = yield queue.get(timeout=1) self.assertEqual("Message %d" % i, msg.content.body) try: extra = yield queue.get(timeout=1) self.fail("Got unexpected 6th message in original queue: " + extra.content.body) except Empty: None #ack messages and check that the next set arrive ok: channel.basic_ack(delivery_tag=msg.delivery_tag, multiple=True) for i in range(6, 11): msg = yield queue.get(timeout=1) self.assertEqual("Message %d" % i, msg.content.body) channel.basic_ack(delivery_tag=msg.delivery_tag, multiple=True) try: extra = yield queue.get(timeout=1) self.fail("Got unexpected 11th message in original queue: " + extra.content.body) except Empty: None
def test_delete_ifempty(self): """ Test that if_empty field of queue_delete is honoured """ channel = self.channel #create a queue and add a message to it (use default binding): yield channel.queue_declare(queue="delete-me-2") yield channel.queue_declare(queue="delete-me-2", passive="True") channel.basic_publish(routing_key="delete-me-2", content=Content("message")) #try to delete, but only if empty: try: yield channel.queue_delete(queue="delete-me-2", if_empty="True") self.fail("Expected delete if_empty to fail for non-empty queue") except Closed, e: self.assertChannelException(406, e.args[0])
def perform_txn_work(self, channel, name_a, name_b, name_c): """ Utility method that does some setup and some work under a transaction. Used for testing both commit and rollback """ # setup: yield channel.queue_declare(queue=name_a, exclusive=True) yield channel.queue_declare(queue=name_b, exclusive=True) yield channel.queue_declare(queue=name_c, exclusive=True) key = "my_key_" + name_b topic = "my_topic_" + name_c yield channel.queue_bind(queue=name_b, exchange="amq.direct", routing_key=key) yield channel.queue_bind(queue=name_c, exchange="amq.topic", routing_key=topic) for i in range(1, 5): channel.basic_publish(routing_key=name_a, content=Content("Message %d" % i)) channel.basic_publish(routing_key=key, exchange="amq.direct", content=Content("Message 6")) channel.basic_publish(routing_key=topic, exchange="amq.topic", content=Content("Message 7")) yield channel.tx_select() # consume and ack messages sub_a = yield channel.basic_consume(queue=name_a, no_ack=False) queue_a = yield self.client.queue(sub_a.consumer_tag) for i in range(1, 5): msg = yield queue_a.get(timeout=1) self.assertEqual(b"Message %d" % i, msg.content.body) channel.basic_ack(delivery_tag=msg.delivery_tag, multiple=True) sub_b = yield channel.basic_consume(queue=name_b, no_ack=False) queue_b = yield self.client.queue(sub_b.consumer_tag) msg = yield queue_b.get(timeout=1) self.assertEqual(b"Message 6", msg.content.body) channel.basic_ack(delivery_tag=msg.delivery_tag) sub_c = yield channel.basic_consume(queue=name_c, no_ack=False) queue_c = yield self.client.queue(sub_c.consumer_tag) msg = yield queue_c.get(timeout=1) self.assertEqual(b"Message 7", msg.content.body) channel.basic_ack(delivery_tag=msg.delivery_tag) # publish messages for i in range(1, 5): channel.basic_publish(routing_key=topic, exchange="amq.topic", content=Content("TxMessage %d" % i)) channel.basic_publish(routing_key=key, exchange="amq.direct", content=Content("TxMessage 6")) channel.basic_publish(routing_key=name_a, content=Content("TxMessage 7")) returnValue((queue_a, queue_b, queue_c))
def publish4(result, message, count_): connection, channel = result exchange_name = '%s_4' % EXCHANGE_NAME yield channel.exchange_declare(exchange=exchange_name, type='direct') for i in range(count_): msg = Content('%s [%04d]' % ( message, i, )) severity = SEVERITIES[i % len(SEVERITIES)] yield channel.basic_publish(exchange=exchange_name, routing_key=severity, content=msg) print ' [x] Sent "%s [%04d]" with severity [%s]' % ( message, i, severity, ) returnValue(result)
def call(n): corr_id = str(uuid.uuid4()) reply = yield channel.queue_declare(exclusive=True) callback_queue = reply.queue msg = Content(str(n)) msg['correlation id'] = corr_id msg['reply to'] = callback_queue yield channel.basic_publish(exchange='', routing_key='rpc_queue', content=msg) print ' [x] Sent "%s"' % n yield channel.basic_consume(queue=callback_queue, no_ack=True, consumer_tag='qtag') queue = yield connection.queue('qtag') while True: response = yield queue.get() if response.content.properties['correlation id'] == corr_id: returnValue(response.content.body)
def writeMetric(metricList, host, port, username, password, vhost, exchange, queue, spec=None, channel_number=1, ssl=False): global conn if not spec: spec = txamqp.spec.load(os.path.normpath( os.path.join(os.path.dirname(__file__), 'amqp0-8.xml'))) delegate = TwistedDelegate() connector = ClientCreator(reactor, AMQClient, delegate=delegate, vhost=vhost, spec=spec) if ssl: from twisted.internet.ssl import ClientContextFactory conn = yield connector.connectSSL(host, port, ClientContextFactory()) else: conn = yield connector.connectTCP(host, port, timeout=130) yield conn.authenticate(username, password) channel = yield conn.channel(channel_number) yield channel.channel_open() yield channel.exchange_declare(exchange=exchange, type="topic", durable=True, auto_delete=False) #reply = yield channel.queue_declare(queue = queue, durable = True) #my_queue = reply.queue #Pickup settings.BIND_PATTERNS somewhere else #for bind_pattern in settings.BIND_PATTERNS: # yield channel.queue_bind(exchange=exchange, queue=my_queue, routing_key=bind_pattern) for (metric, datapoints) in metricList: body = "" for point in datapoints: temp = "%f %d\n"%(point[1], point[0]) body = body + temp message = Content(body) message["delivery mode"] = 2 channel.basic_publish(exchange=exchange, content=message, routing_key=metric) yield channel.channel_close() yield conn.close("Publish Done. Closing Connection.")
def test_get_concurrent_with_error(self): """ If an error occurs in a call to get(), other calls don't fail, and are retried on reconnection instead. """ client1 = yield self.service.whenConnected() deferred = self.source.get("uuid", 0) with self.assertRaises(NotFound): yield self.source.get("uuid-unknown", 0) yield self.channel.basic_publish(routing_key="uuid", content=Content("hello")) notification = yield deferred self.assertEqual("hello", notification.payload) client2 = yield self.service.whenConnected() # The ClientService has reconnected, yielding a new client. self.assertIsNot(client1, client2)
def test_fragment_body(self): """ Body frames must be fragmented according to the broker frame_max parameter. """ channel = self.channel yield channel.queue_declare(queue="test-get", exclusive=True) largeString = ''.join('x' for x in range(self.client.MAX_LENGTH * 2)) msg = Content(largeString) msg["delivery mode"] = 2 channel.basic_publish(routing_key="test-get", content=msg) reply = yield channel.basic_get(no_ack=True) self.assertEqual(reply.method.klass.name, "basic") self.assertEqual(reply.method.name, "get-ok") self.assertEqual(largeString, reply.content.body)
def test_start_consuming_later(self): """Related to #67, will start consuming after publishing messages, this will imitate starting a connector with some pending messages for it""" yield self.connect() yield self.amqp.chan.exchange_declare(exchange='messaging', type='topic', durable='true', arguments=arguments) # Consume consumerTag = 'lateConsumerTest-%s' % (str(uuid.uuid4())) yield self.amqp.named_queue_declare(queue="submit.sm.connector01", durable='true', arguments=arguments) yield self.amqp.chan.queue_bind(queue="submit.sm.connector01", exchange="messaging", routing_key="submit.sm.*") yield self.amqp.chan.basic_consume(queue="submit.sm.connector01", no_ack=False, consumer_tag=consumerTag) queue = yield self.amqp.client.queue(consumerTag) # Publish for i in range(5000): yield self.amqp.publish(exchange='messaging', routing_key="submit.sm.connector01", content=Content(str(i))) # Start consuming (same as starting a connector) queue.get().addCallback(self._callback, queue, ack=True).addErrback(self._errback) # Wait for 15 seconds # (give some time to the consumer to get its work done) yield waitFor(15) yield queue.close() yield self.amqp.disconnect() self.assertEqual(self.consumedMessages, 5000)
def test_get_properties_with_unicode(self): """ Test basic_get method with a property having unicode content Refs #4 """ channel = self.channel yield channel.queue_declare(queue="test-get-header-unicode", exclusive=True) # publish a message (no_ack=True) with persistent messaging msg = Content("some value") msg["delivery mode"] = 2 msg['headers'] = {"somekey": u"any thing in unicode"} channel.basic_publish(routing_key="test-get-header-unicode", content=msg) # use basic_get to read back the messages, and check that we get an empty at the end reply = yield channel.basic_get(no_ack=True) self.assertEqual(reply.method.klass.name, "basic") self.assertEqual(reply.method.name, "get-ok") self.assertEqual("some value", reply.content.body) self.assertEqual({"somekey": u"any thing in unicode"}, reply.content.properties['headers'])
def sendEvent(self, event): """ Write an event to Logstash. """ if not self.chan: log.msg("No AMQP channel. Dropping event.") return event = copy.copy(event) # Pass timestamp on as a string, to work around Logstash bug 279. event['timestamp'] = repr(event['timestamp']) # Make sure isError is always a boolean if 'isError' in event: event['isError'] = bool(event['isError']) body = simplejson.dumps(event) content = Content(body) return self.chan.basic_publish(exchange=self.exchange, content=content)
def test_basic_delivery_queued(self): """ Test basic message delivery where publish is issued before consume (i.e. requires queueing of the message) """ channel = self.channel yield self.exchange_declare(channel, exchange="test-exchange", type="direct") yield self.queue_declare(channel, queue="test-queue") yield channel.queue_bind(queue="test-queue", exchange="test-exchange", routing_key="key") body = "Queued Delivery" channel.basic_publish(exchange="test-exchange", routing_key="key", content=Content(body)) reply = yield channel.basic_consume(queue="test-queue", no_ack=True) queue = yield self.client.queue(reply.consumer_tag) msg = yield queue.get(timeout=5) self.assert_(msg.content.body == body)
def test_consume_all_requeued_messages(self): "Related to #67, test for consuming all requeued messages" yield self.connect() yield self.amqp.chan.exchange_declare(exchange='messaging', type='topic', durable='true', arguments=arguments) # Consume yield self.amqp.named_queue_declare(queue="submit.sm_all_1", durable='true', arguments=arguments) yield self.amqp.chan.queue_bind(queue="submit.sm_all_1", exchange="messaging", routing_key="submit.sm.*") yield self.amqp.chan.basic_consume(queue="submit.sm_all_1", no_ack=False, consumer_tag='qtag') queue = yield self.amqp.client.queue('qtag') queue.get().addCallback(self._callback_reject_once, queue, reject=True).addErrback(self._errback) # Publish for i in range(3000): yield self.amqp.publish(exchange='messaging', routing_key="submit.sm.connector01", content=Content(str(i))) # Wait for 20 seconds # (give some time to the consumer to get its work done) yield waitFor(20) yield queue.close() yield self.amqp.disconnect() self.assertEqual(self.rejectedMessages, 3000) self.assertEqual(self.consumedMessages, 3000)
def test_publish_pickled_binary_content(self): """Refs #640 As of 12/5/2017 txamqp 0.8 were released and it caused an outage to Jasmin. """ yield self.connect() yield self.amqp.chan.exchange_declare(exchange='messaging', type='topic', durable='true', arguments=arguments) # Consume yield self.amqp.named_queue_declare(queue="submit.sm_all", durable='true', arguments=arguments) yield self.amqp.chan.queue_bind(queue="submit.sm_all", exchange="messaging", routing_key="submit.sm.*") yield self.amqp.chan.basic_consume(queue="submit.sm_all", no_ack=True, consumer_tag='qtag') queue = yield self.amqp.client.queue('qtag') queue.get().addCallback(self._callback, queue).addErrback(self._errback) # Publish a pickled binary content with v2 protocol yield self.amqp.publish(exchange='messaging', routing_key="submit.sm.connector01", content=Content(pickle.dumps('\x53', 2))) # Wait for 2 seconds # (give some time to the consumer to get its work done) yield waitFor(2) yield queue.close() yield self.amqp.disconnect() self.assertEqual(self.consumedMessages, 1)
def _send_message(self, exchange, routing_key, msg, deferred, type="direct", durable=True, auto_delete=False): """Send a single message.""" # First declare the exchange just in case it doesn't exist. yield self.chan.exchange_declare(exchange=exchange, type=type, durable=durable, auto_delete=auto_delete) msg = Content(msg) msg["delivery mode"] = 2 # 2 = persistent delivery. d = self.chan.basic_publish(exchange=exchange, routing_key=routing_key, content=msg) d.addErrback(self._send_message_err) d.chainDeferred(deferred)
def test_wb_get_with_broker_shutdown_during_message_wait(self): """ If rabbitmq gets shutdown while we wait for messages, we transparently wait for the reconnection and try again. """ # This will make the connector setup the channel before we call # get(), so by the time we call it in the next line all # connector-related deferreds will fire synchronously and the # code will block on basic-consume. yield self.connector() d = self.source.get("uuid", 0) # Acquiring the channel lock makes sure that basic-consume has # succeeded and we started waiting for the message. yield self.source._channel_lock.acquire() self.source._channel_lock.release() # Restart rabbitmq yield self.client.close() yield self.client.disconnected.wait() self.rabbit.cleanUp() self.rabbit.config = RabbitServerResources( port=self.rabbit.config.port) # Ensure that we use the same port self.rabbit.setUp() # Get a new channel and re-declare the queue, since the restart has # destroyed it. self.client = yield self.endpoint.connect(self.factory) self.channel = yield self.client.channel(1) yield self.channel.channel_open() yield self.channel.queue_declare(queue="uuid") # Publish a message in the queue yield self.channel.basic_publish(routing_key="uuid", content=Content("hello")) notification = yield d self.assertEqual("hello", notification.payload)
def test_simple_publish_consume(self): yield self.connect() yield self.amqp.named_queue_declare(queue="submit.sm.connector01") # Consume yield self.amqp.chan.basic_consume(queue="submit.sm.connector01", no_ack=True, consumer_tag='qtag') queue = yield self.amqp.client.queue('qtag') queue.get().addCallback(self._callback, queue).addErrback(self._errback) # Publish yield self.amqp.publish(routing_key="submit.sm.connector01", content=Content(self.message)) # Wait for 2 seconds # (give some time to the consumer to get its work done) yield waitFor(2) yield queue.close() yield self.amqp.disconnect() self.assertEqual(self.consumedMessages, 1)
def test_delete_ifempty(self): """ Test that if_empty field of queue_delete is honoured """ channel = self.channel # create a queue and add a message to it (use default binding): yield channel.queue_declare(queue="delete-me-2") yield channel.queue_declare(queue="delete-me-2", passive="True") channel.basic_publish(routing_key="delete-me-2", content=Content("message")) # try to delete, but only if empty: try: yield channel.queue_delete(queue="delete-me-2", if_empty="True") self.fail("Expected delete if_empty to fail for non-empty queue") except Closed as e: self.assertChannelException(406, e.args[0]) # need new channel now: channel = yield self.client.channel(2) yield channel.channel_open() # empty queue: reply = yield channel.basic_consume(queue="delete-me-2", no_ack=True) queue = yield self.client.queue(reply.consumer_tag) msg = yield queue.get(timeout=1) self.assertEqual(b"message", msg.content.body) yield channel.basic_cancel(consumer_tag=reply.consumer_tag) # retry deletion on empty queue: yield channel.queue_delete(queue="delete-me-2", if_empty="True") # check that it has gone by declaring passively: try: yield channel.queue_declare(queue="delete-me-2", passive="True") self.fail("Queue has not been deleted") except Closed as e: self.assertChannelException(404, e.args[0])
def gotConnection(conn, username, password, body, count=1): print("Connected to broker.") yield conn.authenticate(username, password) print("Authenticated. Ready to send messages") chan = yield conn.channel(1) yield chan.channel_open() def send_messages(): def message_iterator(): for i in range(count): content = body + "-%d" % i msg = Content(content) msg["delivery mode"] = 2 chan.basic_publish(exchange="chatservice", content=msg, routing_key="txamqp_chatroom") print("Sending message: %s" % content) yield None return task.coiterate(message_iterator()) yield send_messages() stopToken = "STOP" msg = Content(stopToken) msg["delivery mode"] = 2 chan.basic_publish(exchange="chatservice", content=msg, routing_key="txamqp_chatroom") print("Sending message: %s" % stopToken) yield chan.channel_close() chan0 = yield conn.channel(0) yield chan0.connection_close() reactor.stop()
def cb_failure(result): content = Content(json.dumps(result)) content.properties['correlation id'] = msg.content.properties['correlation id'] self._channel.basic_publish(exchange="", content=content, routing_key="houseagent")
def __init__(self, body="", children=None, properties=None, pickleProtocol=2): self.pickleProtocol = pickleProtocol body = self.pickle(body) Content.__init__(self, body, children, properties)
def handle_msg(self, msg, queue): d = queue.get() d.addCallback(self.handle_msg, queue) d.addErrback(self.handle_err, queue) if msg: print "received message", msg replyq = msg.content.properties.get('reply to',None) message_type = msg[4] if msg.content and replyq: request = json.loads(msg.content.body) print "received custom request", request if request["type"] == "custom": if self.custom_callback: self.call_callback(self.custom_callback, msg, request["action"], request["parameters"]) else: result = self.customcallback.on_custom(request["action"], request["parameters"]) content = Content(json.dumps(result)) content.properties['correlation id'] = msg.content.properties['correlation id'] self._channel.basic_publish(exchange="", content=content, routing_key="houseagent") elif request["type"] == "poweron": if self.poweron_callback: self.call_callback(self.poweron_callback, msg, request['address']) else: result = self.poweroncallback.on_poweron(request["address"]) content = Content(json.dumps(result)) content.properties['correlation id'] = msg.content.properties['correlation id'] self._channel.basic_publish(exchange="", content=content, routing_key="houseagent") elif request["type"] == "poweroff": if self.poweroff_callback: self.call_callback(self.poweroff_callback, msg, request['address']) else: result = self.poweroncallback.on_poweroff(request["address"]) content = Content(json.dumps(result)) content.properties['correlation id'] = msg.content.properties['correlation id'] self._channel.basic_publish(exchange="", content=content, routing_key="houseagent") elif request["type"] == "dim": result = self.dimcallback.on_dim(request["address"], request["level"]) content = Content(json.dumps(result)) content.properties['correlation id'] = msg.content.properties['correlation id'] self._channel.basic_publish(exchange="", content=content, routing_key="houseagent") elif request["type"] == "thermostat_setpoint": if self.thermostat_setpoint_callback: self.call_callback(self.thermostat_setpoint_callback, msg, request['address'], request['temperature']) else: result = self.thermostatcallback.on_thermostat_setpoint(request['address'], request['temperature']) content = Content(json.dumps(result)) content.properties['correlation id'] = msg.content.properties['correlation id'] self._channel.basic_publish(exchange="", content=content, routing_key="houseagent") if message_type == 'crud': # Handle CRUD callback request = json.loads(msg.content.body) try: self.crud_callback(request['type'], request['action'], request['parameters']) except Exception as e: print "Failed to do CRUD callback, fix the plugin function: %s" % e