本文整理汇总了Python中kombu.BrokerConnection类的典型用法代码示例。如果您正苦于以下问题:Python BrokerConnection类的具体用法?Python BrokerConnection怎么用?Python BrokerConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BrokerConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RabbitMQHandler
class RabbitMQHandler(object):
def __init__(self, connection_string, exchange):
self._connection = BrokerConnection(connection_string)
self._connections = set([self._connection]) # set of connection for the heartbeat
self._exchange = Exchange(exchange, durable=True, delivry_mode=2, type='topic')
self._connection.connect()
monitor_heartbeats(self._connections)
def _get_producer(self):
producer = producers[self._connection].acquire(block=True, timeout=2)
self._connections.add(producer.connection)
return producer
def publish(self, item, contributor):
with self._get_producer() as producer:
producer.publish(item, exchange=self._exchange, routing_key=contributor, declare=[self._exchange])
def info(self):
if not self._is_active:
return {}
with self._get_producer() as producer:
res = producer.connection.info()
if 'password' in res:
del res['password']
return res
示例2: consume_messages
def consume_messages(amqp_string):
conn = BrokerConnection(amqp_string)
queue = conn.SimpleQueue("eventstream")
while True:
try:
message = queue.get(block=False, timeout=1)
except Empty:
break
else:
message.ack()
pl = message.payload
log_message = {
'state': pl['state'],
'state_msg': pl['state_msg'],
'host': pl['host'],
'body': pl['body'],
'timestamp': pl['timestamp'],
'html': loader.load("message.html").generate(message=pl)
}
MessageHandler.update_cache(log_message)
MessageHandler.send_updates(log_message)
queue.close()
conn.close()
示例3: FanoutPublisher
class FanoutPublisher(PluginBase):
def __init__(self):
if app.debug:
setup_logging(loglevel='DEBUG', loggers=[''])
self.connection = BrokerConnection(app.config['AMQP_URL'])
try:
self.connection.connect()
except Exception as e:
LOG.error('Failed to connect to AMQP transport %s: %s', app.config['AMQP_URL'], e)
raise RuntimeError
self.channel = self.connection.channel()
self.exchange_name = app.config['AMQP_TOPIC']
self.exchange = Exchange(name=self.exchange_name, type='fanout', channel=self.channel)
self.producer = Producer(exchange=self.exchange, channel=self.channel)
LOG.info('Configured fanout publisher on topic "%s"', app.config['AMQP_TOPIC'])
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
LOG.info('Sending message %s to AMQP topic "%s"', alert.get_id(), app.config['AMQP_TOPIC'])
LOG.debug('Message: %s', alert.get_body())
self.producer.publish(alert.get_body(), declare=[self.exchange], retry=True)
示例4: MqServer
class MqServer(object):
"""
exchange='E_X7_W2S', queue='Q_X7_W2S',routing_key = 'RK_X7_W2S'
"""
def __init__(self, callback, kwargs):
self.callback = callback
if kwargs:
self.kwargs = kwargs
else:
self.kwargs = MqDict
def connect(self, hostname="localhost", userid="guest", password="guest", virtual_host="/"):
self.conn = BrokerConnection(hostname, userid, password, virtual_host)
# define Web2Server exchange
exchange = Exchange(self.kwargs["X7_E"], type="direct")
self.queue = Queue(self.kwargs["X7_Q"], exchange, routing_key=self.kwargs["X7_RK"])
channel = self.conn.channel()
consumer = Consumer(channel, self.queue, callbacks=[self.callback])
consumer.consume()
def run(self, once=False):
if once:
self.conn.drain_events()
else:
while True:
self.conn.drain_events()
def get(self):
message = self.queue.get(block=True)
message.ack()
return message
示例5: wait_many
def wait_many(timeout=1):
#: Create connection
#: If hostname, userid, password and virtual_host is not specified
#: the values below are the default, but listed here so it can
#: be easily changed.
connection = BrokerConnection("amqp://guest:[email protected]:5672//")
#: SimpleQueue mimics the interface of the Python Queue module.
#: First argument can either be a queue name or a kombu.Queue object.
#: If a name, then the queue will be declared with the name as the queue
#: name, exchange name and routing key.
queue = connection.SimpleQueue("kombu_demo")
while True:
try:
message = queue.get(block=False, timeout=timeout)
except Empty:
break
else:
spawn(message.ack)
print(message.payload)
queue.close()
connection.close()
示例6: Worker
class Worker(ConsumerMixin):
def __init__(self, **kwargs):
self.__callbacks = kwargs.get('callbacks',self.on_message)
self.__amqp_url = kwargs.get('amqp_url',AMQP_URL)
self.__queue = kwargs.get('queue')
self.__conn_retries = kwargs.get('max_retries',2)
if self.__queue is None:
raise TypeError('invalid worker queue parameter')
self.connection = BrokerConnection(self.__amqp_url)
self.connection.ensure_connection(max_retries=self.__conn_retries, callback=self.on_conn_retry)
def get_consumers(self, consumer, channel):
if not isinstance(self.__callbacks,list):
self.__callbacks = [ self.__callbacks ]
return [consumer(self.__queue, callbacks=self.__callbacks)]
def on_message(self, body, message):
out = 'Received message: %r' % dumps(body)
out += ' properties: %s' % dumps(message.properties)
out += ' delivery_info: %s' % dumps(message.delivery_info)
LOG.info(out,json=True)
message.ack()
def on_conn_retry(self):
LOG.error('Retrying connection for {0}'.format(self.__amqp_url))
def start(self):
LOG.info('Starting AMQP worker {0}'.format(self.__queue))
self.run()
def stop(self):
LOG.info('Stopping AMQP worker {0}'.format(self.__queue))
self.should_stop = True
示例7: run
def run(self):
# Setup connection
mainLogger.debug('Connecting to Redis on %s %s %s' % (
agentConfig['redis_host'], agentConfig['redis_port'], agentConfig['redis_db'])
)
connection = BrokerConnection(
hostname=agentConfig['redis_host'],
transport="redis",
virtual_host=agentConfig['redis_db'],
port=int(agentConfig['redis_port'])
)
connection.connect()
consumer = Consumer(connection)
while True:
try:
consumer.consume()
except Empty:
mainLogger.debug('No tasks, going to sleep')
# sleep is patched and triggers context switching
# for eventlet
time.sleep(1)
mainLogger.debug('Waiting')
mainLogger.debug('Done & exit')
示例8: main
def main():
filename = "meta"
fptr = open(filename, "r")
amqpurl = fptr.readline().strip()
exchange_name = fptr.readline().strip()
exchange = Exchange(exchange_name, type="direct")
D_queue = Queue(exchange_name, exchange, routing_key=exchange_name, auto_delete=False, exclusive=False)
connection = BrokerConnection(amqpurl)
print amqpurl
channel = connection.channel()
queue = D_queue(channel)
queue.declare()
producer = Producer(channel, exchange, routing_key=exchange_name)
message_count = int(sys.argv[1])
imgsize = int(sys.argv[2])
name = sys.argv[3]
s3url = ""
if 'S3_URL' in os.environ:
s3url = os.environ['S3_URL']
s3id = os.environ['EC2_ACCESS_KEY']
s3pw = os.environ['EC2_SECRET_KEY']
n = datetime.now()
print "XXX starting %s" % (str(n))
msg_list = []
dashi_name = str(uuid.uuid4()).split('-')[0]
for i in range(0, message_count):
msg = {'program': 'python node2.py %d %d %d' % (i, message_count, imgsize),
'rank': i,
's3url': s3url,
's3id': s3id,
's3pw': s3pw,
'testname': name,
'dashiname': dashi_name}
msg_list.append(msg)
random.shuffle(msg_list)
print "Add the messages to the queue..."
for msg in msg_list:
print "%s %d of %d" % (msg['testname'], msg['rank'], message_count)
sys.stdout.flush()
producer.publish(msg,
exchange=exchange,
routing_key=exchange_name,
serializer="json")
dashi = get_dashi_connection(amqpurl, dashi_name)
p_con = get_phantom_con(s3id, s3pw)
wait_till_done(dashi, message_count, p_con, name)
n = datetime.now()
print "XXX done %s" % (str(n))
示例9: connect
def connect(self, hostname="localhost", userid="guest", password="guest", virtual_host="/"):
conn = BrokerConnection(hostname, userid, password, virtual_host)
# define Web2Server exchange
exchange = Exchange(self.kwargs["X7_E"], type="direct")
# queue = Queue(self.kwargs["X7_Q"], exchange, routing_key=self.kwargs["X7_RK"])
channel = conn.channel()
self.producer = Producer(channel, exchange, routing_key=self.kwargs["X7_RK"])
示例10: exchange_send
def exchange_send(data,exchange):
try:
connection = BrokerConnection()
channel = connection.channel()
producer = Producer(channel, Exchange(exchange, type="fanout"))
producer.publish(data)
channel.close()
connection.close()
except Exception, error:
print(error)
示例11: __init__
def __init__(self,
server_id,
amqp_host='localhost',
amqp_user ='guest',
amqp_password='guest',
amqp_vhost='/',
amqp_port=5672,
ssl=False,
threaded=False):
self.logger = logging.getLogger('callme.server')
self.logger.debug('Server ID: %s' % server_id)
self.server_id = server_id
self.threaded = threaded
self.do_run = True
self.is_stopped = True
self.func_dict={}
self.result_queue = queue.Queue()
target_exchange = Exchange("server_"+server_id+"_ex", "direct", durable=False,
auto_delete=True)
self.target_queue = Queue("server_"+server_id+"_queue", exchange=target_exchange,
auto_delete=True, durable=False)
self.connection = BrokerConnection(hostname=amqp_host,
userid=amqp_user,
password=amqp_password,
virtual_host=amqp_vhost,
port=amqp_port,
ssl=ssl)
try:
self.connection.connect()
except IOError:
self.logger.critical("Connection Error: Probably AMQP User has not enough permissions")
raise ConnectionError("Connection Error: Probably AMQP User has not enough permissions")
self.channel = self.connection.channel()
self.publish_connection = BrokerConnection(hostname=amqp_host,
userid=amqp_user,
password=amqp_password,
virtual_host=amqp_vhost,
port=amqp_port,
ssl=ssl)
self.publish_channel = self.publish_connection.channel()
# consume
self.consumer = Consumer(self.channel, self.target_queue)
if self.threaded == True:
self.consumer.register_callback(self._on_request_threaded)
else:
self.consumer.register_callback(self._on_request)
self.consumer.consume()
self.logger.debug('Init done')
示例12: Messaging
class Messaging(object):
amqp_opts = {
'amqp_queue': '', # do not send to queue by default
'amqp_topic': 'notify',
'amqp_url': 'amqp://guest:[email protected]:5672//', # RabbitMQ
# 'amqp_url': 'mongodb://localhost:27017/kombu', # MongoDB
# 'amqp_url': 'redis://localhost:6379/', # Redis
# 'amqp_url': 'sqs://ACCESS_KEY:[email protected]' # AWS SQS (must define amqp_queue)
# 'amqp_sqs_region': 'eu-west-1' # required if SQS is used
}
def __init__(self):
config.register_opts(Messaging.amqp_opts)
if CONF.debug:
setup_logging(loglevel='DEBUG', loggers=[''])
self.connection = None
self.connect()
def connect(self):
if not CONF.amqp_url:
return
if CONF.amqp_url.startswith('sqs://'):
CONF.amqp_url = 'sqs://' + CONF.amqp_url[6:].replace('/', '%2F')
if CONF.amqp_sqs_region:
transport_options = {'region': CONF.amqp_sqs_region}
else:
transport_options = {}
self.connection = BrokerConnection(
CONF.amqp_url,
transport_options=transport_options
)
try:
self.connection.connect()
except Exception as e:
LOG.error('Failed to connect to AMQP transport %s: %s', CONF.amqp_url, e)
sys.exit(1)
LOG.info('Connected to broker %s', CONF.amqp_url)
def disconnect(self):
return self.connection.release()
def is_connected(self):
return self.connection.connected
示例13: __init__
class Audit:
def __init__(self, hostname='localhost', port='5672',
userid='', password='', virtual_host='graylog',
exchange=None):
self.hostname = hostname
self.port = port
self.userid = userid
self.password = password
self.virtual_host = virtual_host
self.connection = BrokerConnection(virtual_host=virtual_host)
self.exchange_setup = exchange or ExchangeSetup()
def custom_exchange(self, exchange, exchange_type, routing_key, queue):
"""Broker exchange can be set after the object has been instantiated.
Args:
exchange (str): Exchange name
exchange_type (str): AMQP exchange type, see your broker manual
routing_key (str)
queue (str)
"""
self.exchange_setup.exchange = exchange
self.exchange_setup.exchange_type = exchange_type
self.exchange_setup.routing_key = routing_key
self.exchange_setup.queue = queue
def log(self, message):
"""Pushes argument object to message broker.
Args:
message (json/gelp): Message can depend on third-party log software
Graylog uses gelp format: https://www.graylog.org/resources/gelf/
"""
if (type(message) is not str) or (message == ''):
print 'Unable to log empty message'
return False
if len(message) > 8192:
print 'Message size too large'
return False
self.connection.connect()
channel = self.connection.channel()
exchange = Exchange(self.exchange_setup.exchange,
type=self.exchange_setup.exchange_type)
bound_exchange = exchange(channel)
bound_exchange.declare()
# example_message = '{"short_message":"Kombu", "host":"example.org"}'
message = bound_exchange.Message(message)
bound_exchange.publish(message, routing_key=self.exchange_setup.routing_key)
self.connection.release()
示例14: _connect
def _connect(glance_api_cfg):
# We use BrokerConnection rather than Connection as RHEL 6 has an ancient
# version of kombu library.
conn = BrokerConnection(hostname=glance_api_cfg['host'],
port=glance_api_cfg['port'],
userid=glance_api_cfg['userid'],
password=glance_api_cfg['password'],
virtual_host=glance_api_cfg['virtual_host'])
exchange = Exchange(glance_api_cfg['exchange'],
type='topic',
durable=False,
channel=conn.channel())
return conn, exchange
示例15: add_query
def add_query(query):
query = query.lower()
queries = fetch_queries()
result = True
slots = getattr(settings, 'KRAL_SLOTS', 1)
if query in queries:
queries.remove(query)
else:
try:
connection = BrokerConnection();
channel = connection.channel();
Exchange(query, type="fanout")(channel).declare()
print('Exchange declared for: %s' % query)
except Exception,error:
print(error)