Exemplo n.º 1
0
class StompClient(object):
    def __init__(self, config):
        """Init the Stompest wrapper client.

        :type config: dict
        :param config: The configuration for the STOM client.
            I.e. {'host': 'tcp://127.0.0.1',
                  'queue': '/queue/test',
                  'transaction': True,
                  'username': '******',
                  'password': '******'}
             The transaction attribute defines if messages should be published
             in transactions.
        """
        self.host = config['host']
        self.queue = config['queue']
        self.transactions_enabled = config['transaction']
        self.transaction = None

        auth_header = {}
        if 'username' in config and 'password' in config:
            auth_header.update(
                {StompSpec.LOGIN_HEADER: config['username'],
                 StompSpec.PASSCODE_HEADER: config['password']})

        self.client = Stomp(StompConfig(self.host))
        try:
            self.client.connect(headers=auth_header)
        except (error.StompConnectTimeout, error.StompProtocolError) as e:
            raise ClientErrors.ConnectionError(
                "Could not connect to broker: %s" % e)

    def close(self):
        """Close the connection."""
        try:
            self.client.disconnect()
        except error.StompConnectionError as e:
            raise ClientErrors.ConnectionError(
                "Could not close connection: %s" % e)

    def publish(self, messages, omit_transaction=False, durable=True):
        """Publish a message to the queue.

        :type message: string or list of strings.
        :param message: The message(s) to publish.

        :type omit_transaction: bool
        :param omit_transaction: Set to True if you would like to publish a
            message outside a transaction.

        :type durable: bool
        :param durable: If this message should be durable.
        """
        if omit_transaction:
            header = None
        elif self.transactions_enabled:
            if not self.transaction:
                self.transaction = str(uuid.uuid4())
                try:
                    self.client.begin(transaction=self.transaction)
                except error.StompProtocolError as e:
                    raise ClientErrors.ProtocolError(
                        "Could not start transaction: %s" % e)
            header = {StompSpec.TRANSACTION_HEADER: self.transaction,
                      'durable': 'true' if durable else 'false'}
        else:
            header = None

        if isinstance(messages, (basestring, dict, scim.Event)):
            messages = [messages]
        for msg in messages:
            try:
                if isinstance(msg, dict):
                    del msg['routing-key']
                    msg = json.dumps(msg)
                elif isinstance(msg, scim.Event):
                    msg = json.dumps(msg.get_payload())

                self.client.send(self.queue,
                                 msg,
                                 header)
            except error.StompConnectionError as e:
                raise ClientErrors.ConnectionError(
                    "Could not publish '%s' to broker: %s" % (msg, e))

    def commit(self):
        """Commit the current transaction."""
        if self.transaction:
            self.client.commit(transaction=self.transaction)
            self.transaction = None

    def rollback(self):
        """Roll back (ABORT) the current transaction."""
        if self.transaction:
            self.client.abort(transaction=self.transaction)
            self.transaction = None
Exemplo n.º 2
0
class StompClient(object):
    def __init__(self, config):
        """Init the Stompest wrapper client.

        :type config: dict
        :param config: The configuration for the STOM client.
            I.e. {'host': 'tcp://127.0.0.1',
                  'queue': '/queue/test',
                  'transaction': True,
                  'username': '******',
                  'password': '******'}
             The transaction attribute defines if messages should be published
             in transactions.
        """
        self.host = config['host']
        self.queue = config['queue']
        self.transactions_enabled = config['transaction']
        self.transaction = None

        auth_header = {}
        if 'username' in config and 'password' in config:
            auth_header.update(
                {StompSpec.LOGIN_HEADER: config['username'],
                 StompSpec.PASSCODE_HEADER: config['password']})

        self.client = Stomp(StompConfig(self.host))
        try:
            self.client.connect(headers=auth_header)
        except (error.StompConnectTimeout, error.StompProtocolError) as e:
            raise ClientErrors.ConnectionError(
                "Could not connect to broker: %s" % e)

    def close(self):
        """Close the connection."""
        try:
            self.client.disconnect()
        except error.StompConnectionError as e:
            raise ClientErrors.ConnectionError(
                "Could not close connection: %s" % e)

    def publish(self, messages, omit_transaction=False, durable=True):
        """Publish a message to the queue.

        :type message: string or list of strings.
        :param message: The message(s) to publish.

        :type omit_transaction: bool
        :param omit_transaction: Set to True if you would like to publish a
            message outside a transaction.

        :type durable: bool
        :param durable: If this message should be durable.
        """
        if omit_transaction:
            header = None
        elif self.transactions_enabled:
            if not self.transaction:
                self.transaction = six.text_type(uuid.uuid4())
                try:
                    self.client.begin(transaction=self.transaction)
                except error.StompProtocolError as e:
                    raise ClientErrors.ProtocolError(
                        "Could not start transaction: %s" % e)
            header = {StompSpec.TRANSACTION_HEADER: self.transaction,
                      'durable': 'true' if durable else 'false'}
        else:
            header = None

        if isinstance(messages, (basestring, dict, scim.Event)):
            messages = [messages]
        for msg in messages:
            try:
                if isinstance(msg, dict):
                    del msg['routing-key']
                    msg = json.dumps(msg)
                elif isinstance(msg, scim.Event):
                    msg = json.dumps(msg.get_payload())

                self.client.send(self.queue,
                                 msg,
                                 header)
            except error.StompConnectionError as e:
                raise ClientErrors.ConnectionError(
                    "Could not publish '%s' to broker: %s" % (msg, e))

    def commit(self):
        """Commit the current transaction."""
        if self.transaction:
            self.client.commit(transaction=self.transaction)
            self.transaction = None

    def rollback(self):
        """Roll back (ABORT) the current transaction."""
        if self.transaction:
            self.client.abort(transaction=self.transaction)
            self.transaction = None