Example #1
0
    def basic_publish(self, exchange, routing_key, message, properties=None,
                      mandatory=True, immediate=False):
        ''' Publish a message to an exchange.
        Arguments:
            - `exchange` - The exchange to publish to.
            - `routing_key` - The routing key to bind on.
            - `message` - The message to send. If this is not a string it will
                be serialized as JSON, and the properties.content_type field
                will be forced to be application/json.
            - `properties` - Dict of AMQP message properties.
            - `mandatory` - AMQP Mandatory flag.
            - `immediate` - AMQP Immediate flag.
        '''
        properties = BasicProperties(properties or {})

        if not isinstance(message, basestring):
            # Convert to JSON string if it's not a string
            message = json.dumps(to_serializable(message))
            properties.content_type = "application/json"

        if not isinstance(routing_key, basestring):
            raise InvalidRoutingKeyException("'%s' is not a valid routing key!" %
                                             routing_key)

        return self.channel.basic_publish(exchange, routing_key, message,
                                          properties=properties,
                                          mandatory=mandatory,
                                          immediate=immediate)
Example #2
0
    def publish(self, exchange, routing_key, payload, properties=None):
        """Publish a message to RabbitMQ on the same channel the original
        message was received on, automatically serializing the message payload.

        :param str exchange: the exchange to publish to
        :param str routing_key: the routing key to publish with
        :param pikachewie.data.Properties: the message properties
        :param dict|list: the message body to publish

        """
        if not properties:
            properties = BasicProperties()
        properties.content_type = 'application/json'
        super(JSONPublisherMixin, self).publish(
            exchange,
            routing_key,
            self._serialize(payload),
            properties,
        )