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


Python Connection.session方法代码示例

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


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

示例1: run

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
    def run(self):
        # Create connection and session
        socket = connect( self.host, self.port)
        
        connection = Connection(sock=socket, username=self.username, password=self.password)
        
        print("consumer "+self.queueName+": starting connection...")
        connection.start()
        print("consumer "+self.queueName+": ...connection started")

        print("consumer "+self.queueName+": getting session...")
        session = connection.session(str(uuid4()))
        print("consumer "+self.queueName+": ...session got")

        # Define local queue
        local_queue_name = 'my_local_queue_' +self.queueName

        # Create local queue
        print("consumer "+self.queueName+": getting queue...")
        queue = session.incoming(local_queue_name)
        print("consumer "+self.queueName+": ...queue got")

        # Route messages from message_queue to my_local_queue
        print("consumer "+self.queueName+": subscribing...")
        session.message_subscribe(queue = self.queueName, destination=local_queue_name)
        print("consumer "+self.queueName+": ...subscribed")

        print("consumer "+self.queueName+": starting queue...")
        queue.start()
        print("consumer "+self.queueName+": ...queue started")

        content = ''
        index = 0
        
        while (self.running):
            try:
                # Get message from the local queue, timeout 5 seconds
                message = queue.get(timeout=5)
            except:
                break # exit this thread, consumer

            # Get body of the message
            content = message.body

            #message_properties = message.get("message_properties")

            # Accept message (removes it from the queue)
            session.message_accept(RangedSet(message.id))

            if (content != ""):
                try:
                    self.readGPB(content)

                except Exception, e: 
                    print( "Unexpected error: %s\n" % str(e) )
开发者ID:NexusIF,项目名称:ProjectsBackup,代码行数:57,代码来源:mc_consumer.py

示例2: start

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
    def start(self):
        """
        Enable AMQP queueing. This method puts up the event processor and
        sets it to "active".
        """
        self.log.debug("enabling AMQP queueing")

        # Evaluate username
        user = self.config.get("amqp.id", default=None)
        if not user:
            user = self.env.uuid
        password = self.config.get("amqp.key")

        # Create initial broker connection
        url = "%s:%s" % (self.url['host'], self.url['port'])
        self._conn = Connection.establish(url, reconnect=self.reconnect,
            username=user,
            password=password,
            transport=self.url['transport'],
            reconnect_interval=self.reconnect_interval,
            reconnect_limit=self.reconnect_limit)

        # Do automatic broker failover if requested
        if self.config.get('amqp.failover', False):
            auto_fetch_reconnect_urls(self._conn)

        # Create event exchange
        socket = connect(self.url['host'], self.url['port'])
        if self.url['scheme'][-1] == 's':
            socket = ssl(socket)
        user = self.config.get("amqp.id", default=None)
        if not user:
            user = self.env.uuid
        connection = DirectConnection(sock=socket,
                username=user,
                password=self.config.get("amqp.key"))
        connection.start()
        session = connection.session(str(uuid4()))
        # pylint: disable=E1103
        session.exchange_declare(exchange=self.env.domain, type="xml")
        connection.close()

        # Create event provider
        self._eventProvider = EventProvider(self.env, self.getConnection())
开发者ID:lhm-limux,项目名称:gosa,代码行数:46,代码来源:amqp.py

示例3: __init__

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
class IngestViaQPID:
    def __init__(self, host='localhost', port=5672):
        '''
        Connect to QPID and make bindings to route message to external.dropbox queue
        @param host: string hostname of computer running EDEX and QPID (default localhost)
        @param port: integer port used to connect to QPID (default 5672)
        '''

        try:
            #
            self.socket = connect(host, port)
            self.connection = Connection (sock=self.socket, username=QPID_USERNAME, password=QPID_PASSWORD)
            self.connection.start()
            self.session = self.connection.session(str(uuid4()))
            self.session.exchange_bind(exchange='amq.direct', queue='external.dropbox', binding_key='external.dropbox')
            print('Connected to Qpid')
        except:
            print('Unable to connect to Qpid')

    def sendmessage(self, filepath, header):
        '''
        This function sends a message to the external.dropbox queue providing the path
        to the file to be ingested and a header to determine the plugin to be used to
        decode the file.
        @param filepath: string full path to file to be ingested
        @param header: string header used to determine plugin decoder to use
        '''
        props = self.session.delivery_properties(routing_key='external.dropbox')
        head = self.session.message_properties(application_headers={'header':header},
                                               user_id=QPID_USERNAME) # For issue QPID-5569.  Fixed in Qpid 0.27
        self.session.message_transfer(destination='amq.direct', message=Message(props, head, filepath))

    def close(self):
        '''
        After all messages are sent call this function to close connection and make sure
        there are no threads left open
        '''
        self.session.close(timeout=10)
        print('Connection to Qpid closed')
开发者ID:freemansw1,项目名称:python-awips,代码行数:41,代码来源:qpidingest.py

示例4: BasilConnection

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
class BasilConnection(object):
    def __init__(self, model, host, port):
        self.model = model
        self.host = host
        self.port = port
        self.broker_id = "%s:%i" % (self.host, self.port)

        self.conn = Connection(connect(host, port), self.model.spec)

        self.mclient = managementClient(
            self.model.spec, None, self.model.on_props, self.model.on_stats, self.model.on_callback
        )
        self.mclient.schemaListener(self.model.on_schema)

        self.chan = None

        self.model.connections[self.broker_id] = self

    def open(self):
        self.conn.start()
        self.chan = self.mclient.addChannel(self.conn.session(str(uuid4())), self)

    def close(self):
        self.mclient.removeChannel(self.chan)
开发者ID:ssorj,项目名称:boneyard,代码行数:26,代码来源:__init__.py

示例5: dump_queue

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
def dump_queue(queue, ses, con, num_msgs, to, dest, broker):

   # Read responses as they come in and print to the screen.
   message = 0
   count = 0
   expected = 2*int(num_msgs)

   while True:
      try:
         message = queue.get(timeout=to)
         content = message.body
         count = count + 1
         job_data = message.get('message_properties').application_headers
         print 'Reply Message ID: ' + str(message.get('message_properties').message_id)
         print 'Correlation ID: ' + str(message.get('message_properties').correlation_id)
         print 'Headers:'
         for header in job_data.keys():
            print header + ': ' + str(job_data[header])
#         print ''
#         print 'Body: '
#         print content
         print ''
      except Empty:
         if count < expected:
            print 'Only received %d messages but expected %d.  TEST FAILED!' % (count, expected)
         else:
            print 'Received %d messages.  TEST PASSED.' % count
         break
      except qpid.session.Closed:
         try:
            con.close()
         except:
            pass

         # Give broker time to stablize and accept connections
         time.sleep(2)
         con = Connection(sock=connect(str(broker['ip']), int(broker['port'])))
         con.start()

         ses = con.session(str(uuid4()))

         ses.queue_declare(queue=dest, exclusive=True)
         ses.queue_declare(queue=broker['queue'], exclusive=False, durable=True)
         ses.exchange_bind(exchange='amq.direct', queue=broker['queue'], binding_key='grid')
         ses.exchange_bind(exchange='amq.direct', queue=dest, binding_key=dest)

         # Create the local queue. Use the queue name as destination name
         queue = ses.incoming(dest)

         # Subscribe the local queue to the queue on the server
         ses.message_subscribe(queue=dest, destination=dest, accept_mode=ses.accept_mode.explicit)
         ses.message_flow(dest, ses.credit_unit.message, 0xFFFFFFFFL)
         ses.message_flow(dest, ses.credit_unit.byte, 0xFFFFFFFFL)
      except:
         print 'Unexpected exception!'
         break

      if message != 0:
        ses.message_accept(RangedSet(message.id))

   return (0)
开发者ID:htcondor,项目名称:carod,代码行数:63,代码来源:no_universe.py

示例6: read_config_file

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
      print '%s' % error.msg
      print 'Attempting to retrieve config from %s' % conf_file
      try:
         broker_info = read_config_file(conf_file, 'Broker')
      except ConfigError, error:
         print '%s' % error.msg
         print 'Exiting'
         return(FAILURE)

   replyTo = str(uuid4())

   # Create a client and log in to it.
   connection = Connection(sock=connect(str(broker_info['ip']), int(broker_info['port'])))
   connection.start()

   session = connection.session(str(uuid4()))

   session.queue_declare(queue=replyTo, exclusive=True, auto_delete=True)
   session.queue_declare(queue=broker_info['queue'], exclusive=False, durable="true")
   session.exchange_bind(exchange='amq.direct', queue=broker_info['queue'], binding_key='grid')
   session.exchange_bind(exchange='amq.direct', queue=replyTo, binding_key=replyTo)

   # Create the local queue. Use the queue name as destination name
   dest = replyTo 
   recv_queue = session.incoming(dest)
   print 'Messages queue: ' + dest 

   # Subscribe the local queue to the queue on the server
   session.message_subscribe(queue=replyTo, destination=dest, accept_mode=session.accept_mode.explicit)
   session.message_flow(dest, session.credit_unit.message, 0xFFFFFFFFL)
   session.message_flow(dest, session.credit_unit.byte, 0xFFFFFFFFL)
开发者ID:htcondor,项目名称:carod,代码行数:33,代码来源:no_universe.py

示例7: get_session

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
 def get_session(self, user, passwd):
     socket = connect(self.broker.host, self.broker.port)
     connection = Connection (sock=socket, username=user, password=passwd,
                              mechanism="PLAIN")
     connection.start()
     return connection.session(str(uuid4()))
开发者ID:ChugR,项目名称:qpid-cpp,代码行数:8,代码来源:queue_redirect.py

示例8: dump_queue

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
def dump_queue(binfo, queue_name, to):
   # Create a client and log in to it.
   child_connection = Connection(sock=connect(str(binfo['ip']), int(binfo['port'])))
   child_connection.start()
   child_session = child_connection.session(str(uuid4()))
   child_session.queue_declare(queue=queue_name, exclusive=True)
   child_session.exchange_bind(exchange='amq.direct', queue=queue_name, binding_key=queue_name)

   print 'Messages queue: ' + queue_name 

   # Create the local queue. Use the queue name as destination name
   dest = queue_name 
   queue = child_session.incoming(dest)

   # Subscribe the local queue to the queue on the server
   child_session.message_subscribe(queue=queue_name, destination=dest, accept_mode=child_session.accept_mode.explicit)
   child_session.message_flow(dest, child_session.credit_unit.message, 0xFFFFFFFFL)
   child_session.message_flow(dest, child_session.credit_unit.byte, 0xFFFFFFFFL)

   # Read responses as they come in and print to the screen.
   message = 0
   count = 0

   while True:
      try:
         message = queue.get(timeout=to)
         count = count + 1
         if count == 1:
            print 'Received first reponse: %s ' % str(time.time())
      except Empty:
         print 'Received %s messages: %s' % (str(count), str(time.time() - to))
         break
#      except qpid.session.Closed:
#         print "Re-establishing"
#         try:
#            child_connection.close()
#         except:
#            pass
#
#         # Give broker time to stablize and accept connections
#         time.sleep(2)
#         child_connection = Connection(sock=connect(str(binfo['ip']), int(binfo['port'])))
#         child_connection.start()
#         child_session = child_connection.session(str(uuid4()))
#         child_session.queue_declare(queue=queue_name, exclusive=True)
#         child_session.exchange_bind(exchange='amq.direct', queue=queue_name, binding_key=queue_name)
#
#         # Create the local queue. Use the queue name as destination name
#         queue = child_session.incoming(dest)
#
#         # Subscribe the local queue to the queue on the server
#         child_session.message_subscribe(queue=queue_name, destination=dest, accept_mode=child_session.accept_mode.explicit)
#         child_session.message_flow(dest, child_session.credit_unit.message, 0xFFFFFFFFL)
#         child_session.message_flow(dest, child_session.credit_unit.byte, 0xFFFFFFFFL)
      except:
         print 'Unexpected exception!'
         break

      if message != 0:
        child_session.message_accept(RangedSet(message.id))

   child_session.close(timeout=10)
   child_connection.close()
   return (0)
开发者ID:htcondor,项目名称:carod,代码行数:66,代码来源:performance.py

示例9: QpidAMQPHubExtension

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]
class QpidAMQPHubExtension(BaseAMQPHubExtension):
    """
     Initialize the Moksha Hub.

    `broker`
        [amqps://][<user>[/<password>]@]<host>[:<port>]

    """

    def __init__(self, hub, config):
        self.config = config
        self.set_broker(self.config.get('amqp_broker'))
        self.socket = connect(self.host, self.port)
        if self.url.scheme == URL.AMQPS:
            self.socket = ssl(self.socket)
        self.connection = Connection(sock=self.socket,
                                     username=self.user,
                                     password=self.password)
        self.connection.start()
        log.info("Connected to AMQP Broker %s" % self.host)
        self.session = self.connection.session(str(uuid4()))
        self.local_queues = []
        super(QpidAMQPHubExtension, self).__init__()

    def set_broker(self, broker):
        self.url = URL(broker)
        self.user = self.url.password or 'guest'
        self.password = self.url.password or 'guest'
        self.host = self.url.host
        if self.url.scheme == URL.AMQPS:
            self.ssl = True
            default_port = 5671
        else:
            self.ssl = False
            default_port = 5672
        self.port = self.url.port or default_port

    def send_message(self, topic, message, **headers):
        headers['routing_key'] = headers.get('routing_key', topic)
        props = self.session.delivery_properties(**headers)
        msg = Message(props, message)
        self.session.message_transfer(
            destination=headers.get('exchange', 'amq.topic'),
            message=msg)
        super(QpidAMQPHubExtension, self).send_message(
            topic, message, **headers)

    def subscribe_queue(self, server_queue_name, local_queue_name):
        queue = self.session.incoming(local_queue_name)
        self.session.message_subscribe(queue=server_queue_name,
                                       destination=local_queue_name)
        queue.start()
        return queue

    def queue_declare(self, queue, durable=True, exclusive=False,
                      auto_delete=False, **kw):
        self.session.queue_declare(queue=queue, exclusive=exclusive,
                                   auto_delete=auto_delete,
                                   arguments={'qpid.max_count': 0,
                                              'qpid.max_size': 0}, **kw)

    def exchange_bind(self, queue, exchange='amq.topic', binding_key=None):
        self.session.exchange_bind(exchange=exchange, queue=queue,
                                   binding_key=binding_key)

    def message_subscribe(self, queue, destination):
        return self.session.message_subscribe(queue=queue,
                                              destination=destination)

    def message_accept(self, message):
        try:
            self.session.message_accept(RangedSet(message.id))
        except SessionClosed:
            log.debug("Accepted message on closed session: %s" % message.id)
            pass

    def subscribe(self, topic, callback):
        queue_name = '_'.join([
            "moksha_consumer", self.session.name, str(uuid4()),
        ])
        server_queue_name = local_queue_name = queue_name

        self.queue_declare(queue=server_queue_name, exclusive=True,
                           auto_delete=True)
        self.exchange_bind(server_queue_name, binding_key=topic)

        self.local_queues.append(self.session.incoming(local_queue_name))

        self.message_subscribe(queue=server_queue_name,
                               destination=local_queue_name)

        self.local_queues[-1].start()
        self.local_queues[-1].listen(callback)

        super(QpidAMQPHubExtension, self).subscribe(topic, callback)

    def close(self):
        self.session.close(timeout=2)
        self.connection.close(timeout=2)
        self.socket.close()
开发者ID:ShadowSam,项目名称:moksha,代码行数:102,代码来源:qpid010.py

示例10: registerObjId

# 需要导入模块: from qpid.connection import Connection [as 别名]
# 或者: from qpid.connection.Connection import session [as 别名]

#.........这里部分代码省略.........
    finally:
      self.lock.release ()

  def closeHandler (self, context, reason):
    if self.operational:
      print "Connection to broker lost:", reason
    self.operational = False
    if self.cli != None:
      self.cli.setPromptMessage ("Broker Disconnected")

  def schemaHandler (self, context, classKey, configs, insts, methods, events):
    """ Callback for schema updates """
    if classKey not in self.schema:
      schemaRev = 0
      for key in self.schema:
        if classKey[0] == key[0] and classKey[1] == key[1]:
          schemaRev += 1
      self.schema[classKey] = (configs, insts, methods, events, schemaRev)

  def setCli (self, cliobj):
    self.cli = cliobj

  def __init__ (self, disp, host, username="guest", password="guest"):
    self.lock           = Lock ()
    self.tables         = {}
    self.schema         = {}
    self.bootSequence   = 0
    self.operational    = False
    self.disp           = disp
    self.cli            = None
    self.lastUnit       = None
    self.methodSeq      = 1
    self.methodsPending = {}
    self.sessionId      = "%s.%d" % (platform.uname()[1], os.getpid())

    self.broker = Broker (host)
    sock = connect (self.broker.host, self.broker.port)
    oldTimeout = sock.gettimeout()
    sock.settimeout(10)
    self.conn   = Connection (sock,
                              username=self.broker.username, password=self.broker.password)
    def aborted():
      raise Timeout("Waiting for connection to be established with broker")
    oldAborted = self.conn.aborted
    self.conn.aborted = aborted

    self.conn.start ()

    sock.settimeout(oldTimeout)
    self.conn.aborted = oldAborted

    self.mclient = managementClient ("unused", self.ctrlHandler, self.configHandler,
                                     self.instHandler, self.methodReply, self.closeHandler)
    self.mclient.schemaListener (self.schemaHandler)
    self.mch = self.mclient.addChannel (self.conn.session(self.sessionId))
    self.operational = True
    self.idMap = {}
    self.idBackMap = {}
    self.nextId = 101

  def close (self):
    pass

  def refName (self, oid):
    if oid == None:
      return "NULL"
开发者ID:ChugR,项目名称:qpid-python,代码行数:70,代码来源:managementdata.py


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