本文整理汇总了Python中qpid.messaging.Connection.open方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.open方法的具体用法?Python Connection.open怎么用?Python Connection.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qpid.messaging.Connection
的用法示例。
在下文中一共展示了Connection.open方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_qpid_connection
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
def get_qpid_connection(self):
"""Connect to a broker, set and return the connection object
Authenticates (if necessary), and sets bkr.common._connection and
returns it. This method is thread safe.
"""
self.connection_lock.acquire()
try:
global can_use_qpid, _connection
if not can_use_qpid:
global qpid_import_error
raise ImportError(str(qpid_import_error))
if _connection is None or _connection.get_error():
connection_params = [[self._broker],
{'reconnect': self._reconnect,
'heartbeat': self._heartbeat_timeout}]
if self.krb_auth:
connection_params[1].update({'sasl_mechanisms' : 'GSSAPI'})
# As connections can recover from krb errors, we don't need
# to worry about doing this manually.
self.do_krb_auth()
_connection = Connection(*connection_params[0], **connection_params[1])
_connection.open()
return _connection
finally:
self.connection_lock.release()
示例2: FreshDocWriter
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
class FreshDocWriter(PageWriterBase):
def _initialize(self):
self.set_name('FreshDocWriter')
if get_project_settings()['DEBUG_MODE']:
self._push_message = self._push_message_debug
else:
self._create_client()
def _create_client(self):
try:
self.connection_ = Connection(url='10.100.151.13:5672', heartbeat=4, reconnect=True,
reconnect_limit=10, reconnect_interval=4)
self.connection_.open()
self.sender_ = self.connection_.session().sender('leso.exchange.fresh_video')
except:
self.connection_ = None
self.logger_.exception('failed to connect to message queue server.')
def finalize(self):
if self.connection_:
self.connection_.close()
PageWriterBase.finalize(self)
def _push_message_debug(self, doc):
pass
def _push_message(self, doc):
doc.video.doc_id = doc.id
doc.video.id = str(doc.id)
doc.video.crawl_time = doc.video.create_time = doc.crawl_time
doc.video.discover_time = doc.discover_time
doc.video.url = doc.url
doc.video.domain = doc.domain
doc.video.domain_id = doc.domain_id
doc.video.in_links = doc.in_links
msg_body = thrift_to_str(doc.video)
if not msg_body:
return
self.sender_.send(Message(content=doc.url + '\t' + base64.b64encode(msg_body), durable=True))
self.logger_.info('send message successfully, %s', doc.url)
def process_item(self, item):
if not item:
return
doc = item.to_crawldoc()
if doc.doc_type != CrawlDocType.PAGE_TIME:
return
try:
self._push_message(doc)
except:
self.logger_.exception('failed to send message, %s', doc.url)
示例3: testConnectError
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
def testConnectError(self):
try:
self.conn = Connection.open("localhost", 0)
assert False, "connect succeeded"
except ConnectError, e:
# XXX: should verify that e includes appropriate diagnostic info
pass
示例4: Qpid
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
class Qpid(MQ):
_timeout = 1
def _conectar(self):
try:
logger.debug("Qpid: %s" % self._url.netloc)
self._conn = Connection(self._url.netloc)
if not self._conn:
raise MQError(None, 2)
self._conn.open()
except ConnectError:
raise MQError(cod=2)
try:
self._session = self._conn.session()
self._sender = self._session.sender(self._url.path[1:])
self._receiver = self._session.receiver(self._url.path[1:])
logger.info("Connected on queue %s on %s" % (self._url.path[1:], self._url.netloc))
except ConnectError:
raise MQError(cod=3)
def _enviar(self, mensagem):
logger.debug("Sending a message")
m = Message(mensagem)
self._sender.send(m, True, self._timeout)
def _receber(self, timeout=None):
logger.debug("Retrieving a message")
self._mensagem = self._receiver.fetch(timeout)
return self._mensagem.content
def _tamanho(self):
self._receiver.available()
def _excluir(self):
logger.debug("Ack last message")
self._session.acknowledge()
def _terminar(self):
self._conn.close(self._timeout)
示例5: QpidConnection
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
class QpidConnection():
port = None
ip = None
data_queue = None
cmd_queue = None
conn = None
session = None
sender = None
receiver = None
def __init__(self, ip, port, data_queue, cmd_queue):
self.ip = ip;
self.port = port
self.data_queue = data_queue
self.cmd_queue = cmd_queue
def start(self):
try:
url = str(self.ip)
url += str(':')
url += str(self.port)
self.conn = Connection(url)
self.conn.open()
self.session = self.conn.session()
self.sender = self.session.sender(self.cmd_queue)
self.receiver = self.session.receiver(self.data_queue)
return 1
except MessagingError as m:
print(m)
return 0
def stop(self):
try:
self.conn.close()
except MessagingError as m:
print(m)
return 0
示例6: open
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
def open(self):
"""
Open a connection to the broker.
"""
if self.is_open():
# already open
return
connector = Connector.find(self.url)
Connection.add_transports()
domain = self.ssl_domain(connector)
log.info('open: %s', connector)
impl = RealConnection(
host=connector.host,
port=connector.port,
tcp_nodelay=True,
transport=connector.url.scheme,
username=connector.userid,
password=connector.password,
heartbeat=10,
**domain)
impl.open()
self._impl = impl
log.info('opened: %s', self.url)
示例7: len
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
from qpid.messaging import Connection, Message
from time import sleep
import sys
host = "localhost:10009"
if len(sys.argv) > 1:
host = sys.argv[1]
conn = Connection(host)
try:
conn.open()
sess = conn.session()
tx = sess.sender("spfdemo.com/mobile.45")
count = 0
while(True):
tx.send("Seq: %d" % count)
print "Origin: %s Seq: %d" % (host, count)
count += 1
sleep(1)
except Exception, e:
print "Exception: %r" % e
except KeyboardInterrupt:
print
示例8: RpcConnectionQpidMQ
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
class RpcConnectionQpidMQ(RpcConnection):
def __init__(self,ep=None):
RpcConnection.__init__(self,ep=ep)
self.conn = None
self.exitflag = False
ep.impl = self
self.mq_recv =''
RpcConnectionMQ_Collection.instance().add(self)
@staticmethod
def create(name,host,port,address,af=AF_WRITE):
"""
创建MQ的连接对象
:param name: 必须是真实的 mq 名称
:param host:
:param port:
:param address:
:param af:
:return:
"""
ep = RpcEndPoint(name=name,host=host,port=port,addr=address,type_='qpid')
if ep.open(af):
return ep.impl
return None
# conn = RpcConnectionQpidMQ(ep)
# conn.open(af)
# return conn
# @staticmethod
# def createRpcInvocationPair(ep_read,ep_write):
# conn_read = RpcConnectionQpidMQ(ep_read)
# conn_read.open(AF_READ)
# conn_write = RpcConnectionQpidMQ(ep_write)
# conn_write.open(AF_WRITE)
# conn_write.setLoopbackMQ(conn_read)
# return conn_read,conn_write
def open(self,af):
'''
<ep name="mq_gwa_2" address="mq_gwa_2;{create:always,node:{type:queue,durable:true}}" type="mq" host="127.0.0.1" port="5672"/>
<ep name="mq_gwa_broadcast" address="mq_gwa_broadcast;{create:always,node:{type:topic,durable:true}}" type="mq" host="127.0.0.1" port="5672"/>
'''
from qpid.messaging import Connection
from qpid.util import URL
ep = self.ep
self.af = af
self.exitflag = False
broker = "%s:%s"%(ep.host,ep.port)
# log_debug('prepare mq : <%s %s>!'% (ep.name,broker))
try:
self.conn = Connection( broker,reconnect= True,tcp_nodelay=True)
self.conn.open()
self.ssn = self.conn.session()
if af & AF_READ:
self.rcv = self.ssn.receiver(self.ep.addr)
self.rcv.capacity = 4000
# self.thread = threading.Thread(target =self.thread_recv)
# self.thread.start()
# import gevent
gevent.spawn(self.thread_recv)
if af & AF_WRITE:
self.snd = self.ssn.sender(self.ep.addr)
except:
log_error(traceback.format_exc())
return False
# log_debug('prepare mq : <%s %s> okay!'% (ep.name,broker))
return True
def close(self):
if self.conn:
self.conn.close()
self.conn = None
self.exitflag = True
def setLoopbackMQ(self,conn_recv):
'''
设置rpc调用的回路连接, mq_recv为回路mq的名称, mq在EasyMQ_Collection中被缓存
目前的回路mq名称取 队列名称,如果携带主机信息的话,回路可以从另外一台mq-server返回
'''
self.mq_recv = conn_recv.ep.getUnique()
return self
def sendMessage(self,m):
if m.traits and m.traits.max_linger_time:
value = m.extra.props.get(RpcMessageTraits.MAX_MSG_LINGER_TIME,'0')
value = int(value)
if not value:
value += int(time.time())
m.extra.setPropertyValue(RpcMessageTraits.MAX_MSG_LINGER_TIME, value )
#app制定了超时接收时间,这里调整为绝对时间,以便接收端进行判别,选择接受还是丢弃
#.........这里部分代码省略.........
示例9: __init__
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
def __init__(self, serviceURL, serviceAddress=None, serviceName=None,
conn=None, workers=3):
self.__URL = url = parseURL(serviceURL)
self.__serviceURL = serviceURL
self.__serviceName = serviceName
self.__serviceAddress = serviceAddress
self.__workers = workers
domain = url['path']
# Prepare AMQP connection if not already there
if not conn:
conn = Connection(url['url'], transport=url['transport'], reconnect=True)
conn.open()
AMQPServiceProxy.domain= domain
# Prefill __serviceAddress correctly if domain is given
if AMQPServiceProxy.domain:
self.__serviceAddress = '%s.command.core' % AMQPServiceProxy.domain
if not self.__serviceAddress:
raise AMQPException("no serviceAddress or domain specified")
try:
AMQPServiceProxy.worker[self.__serviceAddress]
except KeyError:
AMQPServiceProxy.worker[self.__serviceAddress] = {}
# Pre instanciate core sessions
for i in range(0, workers):
ssn = conn.session(str(uuid4()))
AMQPServiceProxy.worker[self.__serviceAddress][i] = {
'ssn': ssn,
'sender': ssn.sender(self.__serviceAddress),
'receiver': ssn.receiver('reply-%s; {create:always, delete:always, node: { type: queue, durable: False, x-declare: { exclusive: False, auto-delete: True } }}' % ssn.name),
'locked': False}
# Store connection
self.__conn = conn
self.__ssn = None
self.__sender = None
self.__receiver = None
self.__worker = None
# Retrieve methods
try:
AMQPServiceProxy.methods
except AttributeError:
AMQPServiceProxy.methods = None
AMQPServiceProxy.methods = {}
# Retrieve methods
try:
AMQPServiceProxy.methods[self.__serviceAddress]
except KeyError:
AMQPServiceProxy.methods[self.__serviceAddress] = None
AMQPServiceProxy.methods[self.__serviceAddress] = self.getMethods()
# If we've no direct queue, we need to push to different queues
if AMQPServiceProxy.domain:
queues = set([
x['target'] for x in AMQPServiceProxy.methods[self.__serviceAddress].itervalues()
if x['target'] != 'core'
])
# Pre instanciate queue sessions
for queue in queues:
for i in range(0, workers):
ssn = conn.session(str(uuid4()))
AMQPServiceProxy.worker[queue] = {}
AMQPServiceProxy.worker[queue][i] = {
'ssn': ssn,
'sender': ssn.sender("%s.command.%s" %
(AMQPServiceProxy.domain, queue)),
'receiver': ssn.receiver('reply-%s; {create:always, delete:always, node: { type: queue, durable: False, x-declare: { exclusive: False, auto-delete: True } }}' % ssn.name),
'locked': False}
示例10: AMQPEventConsumer
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
class AMQPEventConsumer(object):
"""
The AMQPEventConsumer can be used to subscribe for events
and process them thru a callback. The subscription is done
thru *XQuery*, the callback can be a python method.
Example listening for an event called *AsteriskNotification*::
>>> from gosa.common.components import AMQPEventConsumer
>>> from lxml import etree
>>>
>>> # Event callback
>>> def process(data):
... print(etree.tostring(data, pretty_print=True))
>>>
>>> # Create event consumer
>>> consumer = AMQPEventConsumer("amqps://admin:[email protected]/org.gosa",
... xquery=\"\"\"
... declare namespace f='http://www.gonicus.de/Events';
... let $e := ./f:Event
... return $e/f:AsteriskNotification
... \"\"\",
... callback=process)
The consumer will start right away, listening for your events.
=============== ============
Parameter Description
=============== ============
url URL used to connect to the AMQP service broker
domain If the domain is not already encoded in the URL, it can be specified here.
xquery `XQuery <http://en.wikipedia.org/wiki/XQuery>`_ string to query for events.
callback Python method to be called if the event happened.
=============== ============
.. note::
The AMQP URL consists of these parts::
(amqp|amqps)://user:[email protected]:port/domain
"""
def __init__(self, url, domain="org.gosa", xquery=".", callback=None):
# Build connection
url = parseURL(url)
self.__conn = Connection(url['url'], transport=url['transport'], reconnect=True)
self.__conn.open()
# Assemble subscription query
queue = 'event-listener-%s' % uuid4()
address = """%s; {
create: always,
delete:always,
node: {
durable: False,
x-declare: {
exclusive: True,
auto-delete: True }
},
link: {
x-bindings: [
{
exchange: '%s',
queue: %s,
key: event,
arguments: { xquery: %r}
}
]
}
}""" % (queue, domain, queue, xquery)
# Add processor for core.event queue
self.__callback = callback
self.__eventWorker = AMQPStandaloneWorker(
self.__conn,
r_address=address,
workers=1,
callback=self.__eventProcessor)
def __del__(self):
self.__eventWorker.join()
self.__conn.close()
#pylint: disable=W0613
def __eventProcessor(self, ssn, data):
# Call callback, let exceptions pass to the caller
xml = objectify.fromstring(data.content)
self.__callback(xml)
def join(self):
self.__eventWorker.join()
示例11: setup_connection
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
def setup_connection(self):
return Connection.open(self.broker.host, self.broker.port,
reconnect=self.reconnect())
示例12: testOpen
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
def testOpen(self):
# XXX: need to flesh out URL support/syntax
self.conn = Connection.open(self.broker.host, self.broker.port,
reconnect=self.reconnect())
self.ping(self.conn.session())
示例13: QpidConnection
# 需要导入模块: from qpid.messaging import Connection [as 别名]
# 或者: from qpid.messaging.Connection import open [as 别名]
class QpidConnection(object):
"""
This class represents a connection to a Qpid broker. A QpidConnection must
be created in order to send or receive AMQP messages using a Qpid broker.
"""
def __init__(self, url, username, password, transport='tcp',
reconnection_interval=60, reconnect_handler=None,
context=None, log=None):
"""
Create a new connection to a Qpid message broker in order to send or
receive AMQP messages.
:param: url URL for the Qpid connection, e.g. 9.10.49.164:5672
:param: username Qpid username
:param: password Qpid password
:param: transport Transport mechanism, one of tcp, tcp+tls,
or ssl (alias for tcp+tls).
:param: reconnection_interval Interval in seconds between reconnect
attempts.
:param: reconnect_handler The function to call upon reconnecting to
the Qpid broker after connection was lost and
then reestablished. This function will be called after the
connections is reestablished but before the listeners are
started up again. It is not passed any parameters.
:param: context The security context
:param: log The logging module used for logging messages. If not
provided then no logging will be done.
"""
self.url = url
self.username = username
self.password = password
self.context = context
self.log = log.getLogger(__name__) if log else None
self.transport = transport
self.reconnection_interval = reconnection_interval
self.reconnect_handler = reconnect_handler
self._listeners = []
self._is_connected = False
def create_listener(self, exchange, topic):
"""
Create a new listener on the given exchange for the given topic.
:param: exchange The name of the Qpid exchange, e.g. 'nova'
:param: topic The topic to listen for, e.g. 'notifications.info'
:returns: A new QpidListener that will listen for messages on the
given exchange and topic.
"""
listener = QpidListener(self, exchange, topic)
self._listeners.append(listener)
return listener
def start(self, is_reconnect=False):
"""
Initiate the Qpid connection and start up any listeners.
:param: is_reconnect True if this method is called as part of a
reconnect attempt, False otherwise
:raise: ConnectionError if a connection cannot be established
"""
# If the Qpid broker URL is not specified (or just the hostname is not
# specified) then we can't make a connection.
if not self.url or self.url.startswith(':'):
log(self.log, 'warn', _('Qpid broker not specified, cannot start '
'connection.'))
return
if not self._is_connected:
self.conn = Connection(self.url, username=self.username,
password=self.password,
transport=self.transport)
try:
self.conn.open()
except ConnectionError as e:
log(self.log, 'critical', _('Cannot connect to Qpid message '
'broker: %s') % (e.message))
# close this connection when encounter connection error
# otherwise, it will leave an ESTABLISHED connection
# to qpid server forever.
if self.conn is not None:
self.conn.close()
raise e
self._is_connected = True
if is_reconnect and self.reconnect_handler:
self.reconnect_handler()
for listener in self._listeners:
listener._start(self.conn)
log(self.log, 'info', _('Connected to Qpid message broker: '
'%[email protected]%s') % (self.username, self.url))
def _reconnect(self):
"""
Attempt to reconnect to the Qpid message broker in intervals until the
connection comes back.
"""
#.........这里部分代码省略.........