Beispiel #1
0
    def __init__(self, 
            connection=None,
            exchange=None, 
            queue=None,
            default_timeout = 3000, # milliseconds
            ):

        for param in ('exchange', 'queue'):
            if not getparam(self, param, None):
                raise ParameterMissing("%s is a required parameter" % param)

        if connection:
            self.connection = connection
        else:
            self.connection = mb_connection # defined in the messaging.messagebus module
            
        self.amqp_channel =  self.connection.channel()
        
        self.amqp_channel.exchange_declare(exchange, 'direct')
        self.amqp_channel.queue_declare(queue, auto_delete=True)
        
        self.incoming_messages = Queue()
        self.consumption_thread = ConsumptionThread(connection=self.connection)
        self.consumption_thread.set_callback(queue=self.queue, callback=self.incoming_message_callback, no_ack=True)
        self.consumption_thread.start()
Beispiel #2
0
class AmqpRpcEndpoint(object):

    class ParameterMissing(Exception):
        pass
        
    def __init__(self, 
            connection=None,
            exchange=None, 
            queue=None,
            default_timeout = 3000, # milliseconds
            ):

        for param in ('exchange', 'queue'):
            if not getparam(self, param, None):
                raise ParameterMissing("%s is a required parameter" % param)

        if connection:
            self.connection = connection
        else:
            self.connection = mb_connection # defined in the messaging.messagebus module
            
        self.amqp_channel =  self.connection.channel()
        
        self.amqp_channel.exchange_declare(exchange, 'direct')
        self.amqp_channel.queue_declare(queue, auto_delete=True)
        
        self.incoming_messages = Queue()
        self.consumption_thread = ConsumptionThread(connection=self.connection)
        self.consumption_thread.set_callback(queue=self.queue, callback=self.incoming_message_callback, no_ack=True)
        self.consumption_thread.start()
    
    def serialize_message(self, pb_message):
        ''' Serializes a protobuf message into an array of bytes, ready for transport '''
        bytes = pb_message.SerializeToString()
        return bytes
        
    def parse_message(self, messageclass, bytes):
        ''' Parse an array of bytes into a protobuf message. '''
        message = messageclass()
        message.ParseFromString(bytes)
        return message
        
    def send_message(self, message, routing_key):
        ''' Serialize and send a protobuf message along a specified AMQP route '''
        self.messagebus.publish(self.serialize_message(message), exchange=self.exchange, routing_key=routing_key)
        
    def get_message(self,messageclass, timeout=-2):
        '''
            Read and deserialize a protobuf message from the wire.
            For timeout, -2 means "use default", -1 means "none", and anything
            else means "timeout" milliseconds
        '''
        bytes = self.get_bytes(timeout=timeout)
        self.parse_message(messageclass, bytes)
        return message
        
    def send_bytes(self, bytes, routing_key):
        ''' Send an array of bytes along a specified AMQP route '''
        self.amqp_channel.basic_publish(bytes, exchange=self.exchange, routing_key=routing_key)
        
    def incoming_message_callback(self, msg):
        self.incoming_messages.put(msg)
    
    def get_bytes(self, timeout = -2):
        '''
        Get an array of bytes from the incoming queue.
        For timeout, -2 means "use default", -1 means "none", and anything
        else means "timeout" milliseconds
        '''
        if timeout == -1:
            return self.incoming_messages.get()
        if timeout == -2:
            timeout = self.default_timeout
        return self.incoming_messages.get(True, timeout / 1000)
        
    def bind_service(self, pb_service, routing_key):
        ''' Bind an rpc service to a specified routing key '''
        self.service = pb_service
        self.routing_key = routing_key
        self.amqp_channel.queue_bind(self.queue, self.exchange, routing_key=self.routing_key)
        
    def unbind_service(self):
        raise NotImplementedError
    
    def incoming_message_queue_size(self):
        ''' NOTE: qsize() is approximate. '''
        return self.incoming_messages.qsize()
        
    def Reset(self):
        raise NotImplementedError
        
# Attribute setters are not so useful in python.
#    def set_default_timeout(self, timeout = -1):
#        ''' Change the default global timeout. -1 means "no timeout" '''
#        self.default_timeout = timeout
        
    def Failed(self):
        raise NotImplementedError