コード例 #1
0
class BlockingQueuePublisher(object):
    """
    Class that is responsible for sending messages to the amqp exchange.
    """
    implements(IQueuePublisher)

    def __init__(self):
        self.reconnect()

    def reconnect(self):
        connectionInfo = getUtility(IAMQPConnectionInfo)
        queueSchema = getUtility(IQueueSchema)
        self._client = BlockingPublisher(connectionInfo, queueSchema)

    def publish(self, exchange, routing_key, message, createQueues=None,
                mandatory=False, immediate=False, headers=None,
                declareExchange=True):
        if createQueues:
            for queue in createQueues:
                self._client.createQueue(queue)
        self._client.publish(exchange, routing_key, message,
                             mandatory=mandatory, immediate=immediate,
                             headers=headers, declareExchange=declareExchange)

    @property
    def channel(self):
        return self._client.getChannel()

    def close(self):
        """
        Closes the channel and connection
        """
        self._client.close()
コード例 #2
0
ファイル: publisher.py プロジェクト: zenoss/zenoss-prodbin
class BlockingQueuePublisher(object):
    """
    Class that is responsible for sending messages to the amqp exchange.
    """
    implements(IQueuePublisher)

    def __init__(self):
        self.reconnect()

    def reconnect(self):
        connectionInfo = getUtility(IAMQPConnectionInfo)
        queueSchema = getUtility(IQueueSchema)
        self._client = BlockingPublisher(connectionInfo, queueSchema)

    def publish(self, exchange, routing_key, message, createQueues=None,
                mandatory=False, headers=None,
                declareExchange=True):
        if createQueues:
            for queue in createQueues:
                if not self._client.queueExists(queue):
                    self.reconnect()
                    self._client.createQueue(queue)
        self._client.publish(exchange, routing_key, message,
                             mandatory=mandatory, headers=headers,
                             declareExchange=declareExchange)

    @property
    def channel(self):
        return self._client.getChannel()

    def close(self):
        """
        Closes the channel and connection
        """
        self._client.close()