Beispiel #1
0
def publish(uri=None, exchange_name=None, routing_key=None,
            body=None, properties=None, confirm=False):
    """Publish a message to RabbitMQ. This should only be used for one-off
    publishing, as you will suffer a performance penalty if you use it
    repeatedly instead creating a connection and channel and publishing on that

    :param str uri: AMQP URI to connect to
    :param str exchange_name: The exchange to publish to
    :param str routing_key: The routing_key to publish with
    :param str or unicode or bytes or dict or list: The message body
    :param dict properties: Dict representation of Basic.Properties
    :param bool confirm: Confirm this delivery with Publisher Confirms
    :rtype: bool or None

    """
    if exchange_name is None:
        exchange_name = ''

    with connection.Connection(uri) as conn:
        with conn.channel() as channel:
            msg = message.Message(channel, body or '', properties or dict())
            if confirm:
                channel.enable_publisher_confirms()
                return msg.publish(exchange_name, routing_key or '')
            else:
                msg.publish(exchange_name, routing_key or '')
Beispiel #2
0
 def setUp(self):
     super(TestDeliveredMessageObject, self).setUp()
     self.method = specification.Basic.Deliver(self.CONSUMER_TAG,
                                               self.DELIVERY_TAG,
                                               self.REDELIVERED,
                                               self.EXCHANGE,
                                               self.ROUTING_KEY)
     self.msg = message.Message(self.channel, self.BODY, self.PROPERTIES)
     self.msg.method = self.method
     self.msg.name = self.method.name
Beispiel #3
0
    def _create_message(self, channel_id, method_frame, header_frame, body):
        """Create a message instance with the channel it was received on and
        the dictionary of message parts.

        :param int channel_id: The channel id the message was sent on
        :param pamqp.specification.Frame method_frame: The method frame value
        :param pamqp.header.ContentHeader header_frame: The header frame value
        :param str body: The message body
        :rtype: rabbitpy.message.Message

        """
        msg = message.Message(self._channels[channel_id], body,
                              header_frame.properties.to_dict())
        msg.method = method_frame
        msg.name = method_frame.name
        return msg
Beispiel #4
0
 def setUp(self):
     super(TestWithPropertiesCreation, self).setUp()
     self.body = uuid.uuid4()
     self.props = {
         'app_id': b'Foo',
         'content_type': b'application/json',
         'content_encoding': b'gzip',
         'correlation_id': str(uuid.uuid4()),
         'delivery_mode': 2,
         'expiration': int(time.time()) + 10,
         'headers': {
             'foo': 'bar'
         },
         'message_id': str(uuid.uuid4()),
         'message_type': b'TestCreation',
         'priority': 9,
         'reply_to': b'none',
         'timestamp': datetime.datetime.utcnow(),
         'user_id': b'guest'
     }
     self.msg = message.Message(self.channel, self.body, dict(self.props))
Beispiel #5
0
    def basic_publish(self, exchange='', routing_key='', body='',
                      properties=None, mandatory=False, immediate=False):
        """Publish a message

        This method publishes a message to a specific exchange. The message
        will be routed to queues as defined by the exchange configuration and
        distributed to any active consumers when the transaction, if any, is
        committed.

        :param str exchange: The exchange name
        :param str routing_key: Message routing key
        :param body: The message body
        :type body: str|bytes
        :param dict properties: AMQP message properties
        :param bool mandatory: Indicate mandatory routing
        :param bool immediate: Request immediate delivery
        :return: bool or None

        """
        msg = message.Message(self.channel, body,
                              properties or {}, False, False)
        return msg.publish(exchange, routing_key, mandatory, immediate)
Beispiel #6
0
    def _create_message(self, method_frame, header_frame, body):
        """Create a message instance with the channel it was received on and
        the dictionary of message parts. Will return None if no message can be
        created.

        :param pamqp.specification.Frame method_frame: The method frame value
        :param header_frame: Header frame value
        :type header_frame: pamqp.header.ContentHeader or None
        :param body: The message body
        :type body: str or None
        :rtype: rabbitpy.message.Message or None

        """
        if not method_frame:
            LOGGER.warning('Received empty method_frame, returning None')
            return None
        if not header_frame:
            LOGGER.debug('Malformed header frame: %r', header_frame)
        props = header_frame.properties.to_dict() if header_frame else dict()
        msg = message.Message(self, body, props)
        msg.method = method_frame
        msg.name = method_frame.name
        return msg
Beispiel #7
0
 def setUp(self):
     super(TestJSONDeserialization, self).setUp()
     self.expectation = json.loads(self.BODY.decode('utf-8'))
     self.msg = message.Message(self.channel, self.BODY)
Beispiel #8
0
 def setUp(self):
     super(TestCreationWithDictBodyAndProperties, self).setUp()
     self.body = {'foo': str(uuid.uuid4())}
     self.msg = message.Message(self.channel, self.body, {'app_id': 'foo'})
Beispiel #9
0
 def setUp(self):
     super(TestCreationWithIntTimestamp, self).setUp()
     self.msg = message.Message(self.channel, str(uuid.uuid4()),
                                {'timestamp': int(time.time())})
Beispiel #10
0
 def setUp(self):
     super(TestCreationWithNoneTimestamp, self).setUp()
     self.msg = message.Message(self.channel, str(uuid.uuid4()),
                                {'timestamp': None})
Beispiel #11
0
 def setUp(self, write_frames):
     super(TestPublisherConfirms, self).setUp()
     self.write_frames = write_frames
     self.channel._publisher_confirms = True
     self.channel.wait_for_confirmation = self._confirm_wait = mock.Mock()
     self.msg = message.Message(self.channel, self.BODY)
Beispiel #12
0
 def setUp(self):
     super(TestCreationWithStructTimeTimestamp, self).setUp()
     self.msg = message.Message(self.channel, str(uuid.uuid4()),
                                {'timestamp': time.localtime()})
Beispiel #13
0
 def setUp(self):
     super(TestCreationWithDictBody, self).setUp()
     self.body = {'foo': str(uuid.uuid4())}
     self.msg = message.Message(self.channel, self.body)
Beispiel #14
0
 def setUp(self, write_frames):
     super(TestPublishingUnicode, self).setUp()
     self.write_frames = write_frames
     self.msg = message.Message(self.channel, self.BODY)
     self.msg.publish(self.EXCHANGE, self.ROUTING_KEY)
Beispiel #15
0
 def setUp(self):
     super(TestCreationWithFloatTimestamp, self).setUp()
     self.msg = message.Message(self.chan, str(uuid.uuid4()),
                                {'timestamp': time.time()})
Beispiel #16
0
 def setUp(self, write_frames):
     super(TestPublishing, self).setUp()
     self.write_frames = write_frames
     self.msg = message.Message(self.channel, self.BODY, {'app_id': 'foo'})
     self.msg.publish(self.EXCHANGE, self.ROUTING_KEY)
Beispiel #17
0
 def setUp(self):
     super(TestNonDeliveredMessageObject, self).setUp()
     self.body = self.BODY
     self.msg = message.Message(self.channel, self.body, {'app_id': 'foo'})
Beispiel #18
0
 def setUp(self):
     super(TestCreation, self).setUp()
     self.body = uuid.uuid4()
     self.msg = message.Message(self.channel, self.body, opinionated=True)
Beispiel #19
0
 def setUp(self):
     super(TestNonOpinionatedCreation, self).setUp()
     self.body = str(uuid.uuid4())
     self.msg = message.Message(self.channel, self.body)