本文整理匯總了Python中qpid.messaging.Connection.close方法的典型用法代碼示例。如果您正苦於以下問題:Python Connection.close方法的具體用法?Python Connection.close怎麽用?Python Connection.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qpid.messaging.Connection
的用法示例。
在下文中一共展示了Connection.close方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: FreshDocWriter
# 需要導入模塊: from qpid.messaging import Connection [as 別名]
# 或者: from qpid.messaging.Connection import close [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)
示例2: Qpid
# 需要導入模塊: from qpid.messaging import Connection [as 別名]
# 或者: from qpid.messaging.Connection import close [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)
示例3: QpidConnection
# 需要導入模塊: from qpid.messaging import Connection [as 別名]
# 或者: from qpid.messaging.Connection import close [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
示例4: RpcConnectionQpidMQ
# 需要導入模塊: from qpid.messaging import Connection [as 別名]
# 或者: from qpid.messaging.Connection import close [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製定了超時接收時間,這裏調整為絕對時間,以便接收端進行判別,選擇接受還是丟棄
#.........這裏部分代碼省略.........
示例5: AMQPEventConsumer
# 需要導入模塊: from qpid.messaging import Connection [as 別名]
# 或者: from qpid.messaging.Connection import close [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()
示例6: QpidConnection
# 需要導入模塊: from qpid.messaging import Connection [as 別名]
# 或者: from qpid.messaging.Connection import close [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.
"""
#.........這裏部分代碼省略.........