Пример #1
0
class AMQFactory(protocol.ReconnectingClientFactory):
    VHOST = '/'

    def __init__(self, creds):
        self.spec = txamqp.spec.load(SPECFILE)
        self.creds = creds
        self.client = None
        self.channel = None

    def buildProtocol(self, addr):
        self.resetDelay()
        delegate = TwistedDelegate()
        self.client = AMQClient(delegate=delegate,
                                vhost=self.VHOST,
                                spec=self.spec)
        self.client.start(self.creds)
        return self.client

    @defer.inlineCallbacks
    def publish(self, exchange, msg, routing_key):
        if not self.client:
            raise NotImplementedError
        if not self.channel:
            yield self._createChannel()
        content = Content(msg)
        yield self.channel.basic_publish(exchange=exchange,
                                         content=content,
                                         routing_key=routing_key)

    @defer.inlineCallbacks
    def _createChannel(self):
        self.channel = yield self.client.channel(1)
        yield self.channel.channel_open()
Пример #2
0
class AMQFactory(protocol.ReconnectingClientFactory):
    VHOST = '/'

    def __init__(self, creds):
        self.spec = txamqp.spec.load(SPECFILE)
        self.creds = creds
        self.client = None
        self.channel  = None

    def buildProtocol(self, addr):
        self.resetDelay()
        delegate = TwistedDelegate()
        self.client = AMQClient(delegate=delegate, vhost=self.VHOST, spec=self.spec)
        self.client.start(self.creds)
        return self.client

    @defer.inlineCallbacks
    def publish(self, exchange, msg, routing_key):
        if not self.client:
            raise NotImplementedError
        if not self.channel:
            yield self._createChannel()
        content = Content(msg)
        yield self.channel.basic_publish(exchange=exchange, content=content, routing_key=routing_key)

    @defer.inlineCallbacks
    def _createChannel(self):
        self.channel = yield self.client.channel(1)
        yield self.channel.channel_open()
Пример #3
0
    def start(self, *args, **kargs):
        yield AMQClient.start(self, *args, **kargs)
        self.channel_conn = yield self.channel(0)
        self.channel_rx = yield self.channel(1)
        yield self.channel_rx.channel_open()
        #todo: it's not correct for server side
        #self.queue_rx = yield self.channel_rx.queue_declare(exclusive=True, auto_delete=True)
        yield self._declare_queue_rx()

        self.replies = {}  # correlation_id:deferred
        yield self.channel_rx.basic_consume(queue=self.queue_rx.queue,
                                            no_ack=True,
                                            consumer_tag='qtag')
        self.queue = yield AMQClient.queue(self, 'qtag')
        self.finish_defer = Deferred()
        self._update_work_defer()