本文整理汇总了Python中qpid.connection.Connection类的典型用法代码示例。如果您正苦于以下问题:Python Connection类的具体用法?Python Connection怎么用?Python Connection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_qpid_connection
def get_qpid_connection(broker_addr):
try:
socket = connect(broker_addr, 5672)
connection = Connection (sock=socket)
connection.start()
return connection
except:
sys.stderr.write("mhsAckNotify: connect to %s: %s\n" % (broker_addr, sys.exc_info()[1],))
return None
示例2: run
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) )
示例3: __init__
def __init__(self, broker, **kw):
self.set_broker(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()))
示例4: __init__
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')
示例5: __init__
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__()
示例6: BasilConnection
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)
示例7: __init__
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
示例8: __init__
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
示例9: start
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())
示例10: read_config_file
broker_info['port'] = broker_info['broker_port']
broker_info['queue'] = broker_info['broker_queue']
except ConfigError, error:
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
示例11: dump_queue
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)
示例12: get_session
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()))
示例13: dump_queue
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)
示例14: connect
parser.add_option("-t", "--targets", dest="targets", default="localhost",
help="comma-separated list of target hostnames running qpid")
parser.add_option("-d", "--debug", dest="debug", action="store_true",
help="debug what messages are being sent")
options, args = parser.parse_args()
options.targets = [t.strip() for t in options.targets.split(',')]
# Create connection and session
session_dicts = []
for target in options.targets:
print "Attempting to setup connection with", target
try:
socket = connect(target, 5672)
connection = Connection(
socket, username='guest', password='guest',
)
connection.start(timeout=10000)
session = connection.session(str(uuid4()))
# Setup routing properties
properties = session.delivery_properties(routing_key='httpdlight_http_rawlogs')
session_dicts.append({
'target' : target,
'socket' : socket,
'connection' : connection,
'session' : session,
'properties' : properties,
})
print " Created target", target
except Exception as e:
示例15: QpidAMQPHubExtension
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()