当前位置: 首页>>代码示例>>Python>>正文


Python Stomp.begin方法代码示例

本文整理汇总了Python中stompest.sync.Stomp.begin方法的典型用法代码示例。如果您正苦于以下问题:Python Stomp.begin方法的具体用法?Python Stomp.begin怎么用?Python Stomp.begin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stompest.sync.Stomp的用法示例。


在下文中一共展示了Stomp.begin方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: StompClient

# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import begin [as 别名]
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': 'my_username',
                  'password': 'fido5'}
             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
开发者ID:unioslo,项目名称:cerebrum,代码行数:98,代码来源:stomp_client.py


注:本文中的stompest.sync.Stomp.begin方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。