def __init__(self, config, callback): self.reconnection_delay = 1.0 self.caller_callback = callback self.config = ConnectionConfig() self.config.read(config) self.callbacks = self.set_callbacks() credentials = PlainCredentials(**self.config.credentials) broker_config = self.config.broker_config broker_config['credentials'] = credentials parameters = ConnectionParameters(**broker_config) try: parameters.host = self.config.host except NoOptionError: pass try: parameters.heartbeat = int(self.config.heartbeat) except NoOptionError: pass try: parameters.heartbeat = int(self.config.heartbeat) except NoOptionError: pass self.connection = SelectConnection(parameters, self.on_connected)
def __init__(self, host=None, port=None, vhost=None, username=None, password=None, exchange='oplog', queue=None, dump=DUMP_JSON): super(QueueHandler, self).__init__() parameters = ConnectionParameters() if host is not None: parameters.host = host if port is not None: parameters.port = port if vhost is not None: parameters.virtual_host = vhost if username is not None and password is not None: parameters.credentials = PlainCredentials(username, password) logger.info('Connect to queue.') channel = BlockingConnection(parameters).channel() if queue is None: channel.exchange_declare(exchange, 'fanout', passive=True) else: channel.exchange_delete(exchange) channel.exchange_declare(exchange, 'direct') channel.queue_declare(queue, durable=True) channel.queue_bind(queue, exchange) self.exchange = exchange self.channel = channel self.queue = queue if dump == DUMP_JSON: self.dump = lambda op: json.dumps(op, default=json_util.default) elif dump == DUMP_BSON: self.dump = lambda op: BSON.encode(op) else: raise ValueError('Invalid `dump` parameter for QueueHandler.')