def cancel(self, consumer_tag='', nowait=True, consumer=None, cb=None): ''' Cancel a consumer. Can choose to delete based on a consumer tag or the function which is consuming. If deleting by function, take care to only use a consumer once per channel. Callbacks only apply if nowait=False ''' if consumer: for (tag,func) in self._consumer_cb.iteritems(): if func==consumer: consumer_tag = tag break args = Writer() args.write_shortstr(consumer_tag) args.write_bit(nowait) self.send_frame( MethodFrame(self.channel_id, 60, 30, args) ) if not nowait: self.channel.add_synchronous_cb( self._recv_cancel_ok ) self._cancel_cb.append( cb ) else: try: del self._consumer_cb[consumer_tag] except KeyError: self.logger.warning( 'no callback registered for consumer tag " %s "', consumer_tag )
def _send_open(self): args = Writer() args.write_shortstr(self.connection._vhost) args.write_shortstr('') args.write_bit(True) # insist flag for older amqp, not used in 0.9.1 self.send_frame(MethodFrame(self.channel_id, 10, 40, args))
def _send_open(self): args = Writer() args.write_shortstr(self.connection._vhost) args.write_shortstr('') args.write_bit(True) # insist flag for older amqp, not used in 0.9.1 self.send_frame( MethodFrame(self.channel_id, 10, 40, args) )
def _send_flow(self, active): ''' Send a flow control command. ''' args = Writer() args.write_bit(active) self.send_frame(MethodFrame(self.channel_id, 20, 20, args)) self.channel.add_synchronous_cb(self._recv_flow_ok)
def reject(self, delivery_tag, requeue=False): ''' Reject a message. ''' args = Writer() args.write_longlong( delivery_tag ) args.write_bit( requeue ) self.send_frame( MethodFrame(self.channel_id, 60, 90, args) )
def recover_async(self, requeue=False): ''' Redeliver all unacknowledged messaages on this channel. This method is deprecated in favour of the synchronous recover/recover-ok ''' args = Writer() args.write_bit( requeue ) self.send_frame( MethodFrame(self.channel_id, 60, 100, args) )
def ack(self, delivery_tag, multiple=False): ''' Acknowledge delivery of a message. If multiple=True, acknowledge up-to and including delivery_tag. ''' args = Writer() args.write_longlong(delivery_tag) args.write_bit(multiple) self.send_frame( MethodFrame(self.channel_id, 60, 80, args) )
def qos(self, prefetch_size=0, prefetch_count=0, is_global=False): ''' Set QoS on this channel. ''' args = Writer() args.write_long(prefetch_size) args.write_short(prefetch_count) args.write_bit(is_global) self.send_frame( MethodFrame(self.channel_id, 60, 10, args) ) self.channel.add_synchronous_cb( self._recv_qos_ok )
def _recv_flow(self, method_frame): ''' Receive a flow control command from the broker ''' self.channel._active = method_frame.args.read_bit() args = Writer() args.write_bit(self.channel.active) self.send_frame(MethodFrame(self.channel_id, 20, 21, args)) if self._flow_control_cb is not None: self._flow_control_cb()
def recover(self, requeue=False, cb=None): ''' Ask server to redeliver all unacknowledged messages. ''' args = Writer() args.write_bit( requeue ) # The XML spec is incorrect; this method is always synchronous # http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2011-January/010738.html self._recover_cb.append( cb ) self.send_frame( MethodFrame(self.channel_id, 60, 110, args) ) self.channel.add_synchronous_cb( self._recv_recover_ok )
def get(self, queue, consumer, no_ack=True, ticket=None): ''' Ask to fetch a single message from a queue. The consumer will be called if an actual message exists, but if not, the consumer will not be called. ''' args = Writer() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(queue) args.write_bit(no_ack) self._get_cb.append( consumer ) self.send_frame( MethodFrame(self.channel_id, 60, 70, args) ) self.channel.add_synchronous_cb( self._recv_get_response )
def select(self, nowait=True, cb=None): ''' Set this channel to use publisher confirmations. ''' nowait = nowait and self.allow_nowait() and not cb if not self._enabled: self._enabled = True self.channel.basic._msg_id = 0 self.channel.basic._last_ack_id = 0 args = Writer() args.write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 85, 10, args)) if not nowait: self._select_cb.append(cb) self.channel.add_synchronous_cb(self._recv_select_ok)
def select(self, nowait=True, cb=None): ''' Set this channel to use publisher confirmations. ''' nowait = nowait and self.allow_nowait() and not cb if not self._enabled: self._enabled = True self.channel.basic._msg_id = 0 self.channel.basic._last_ack_id = 0 args = Writer() args.write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 85, 10, args)) if not nowait: self._select_cb.append(cb) self.channel.add_synchronous_cb(self._recv_select_ok)
def purge(self, queue, nowait=True, ticket=None, cb=None): """ Purge all messages in a queue. """ # If a callback is defined, then we have to use synchronous transactions. if cb: nowait = False args = Writer() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(queue) args.write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 50, 30, args)) if not nowait: self.channel.add_synchronous_cb(self._recv_purge_ok) self._purge_cb.append(cb)
def bind(self, queue, exchange, routing_key="", nowait=True, arguments={}, ticket=None, cb=None): """ bind to a queue. """ # If a callback is defined, then we have to use synchronous transactions. if cb: nowait = False args = Writer() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(queue) args.write_shortstr(exchange) args.write_shortstr(routing_key) args.write_bit(nowait) args.write_table(arguments) self.send_frame(MethodFrame(self.channel_id, 50, 20, args)) if not nowait: self.channel.add_synchronous_cb(self._recv_bind_ok) self._bind_cb.append(cb)
def test_write_bit(self): w = Writer() assert_true(w is w.write_bit(True)) assert_equals(bytearray('\x01'), w._output_buffer)
def test_write_bit(self): w = Writer() assert_true( w is w.write_bit(True) ) assert_equals( bytearray('\x01'), w._output_buffer )