def test_reconnect_order(self): brokers = ['host1', 'host2', 'host3', 'host4', 'host5'] brokers_count = len(brokers) info = {'attempt': 0} class MyConnection(kombu.connection.BrokerConnection): def __init__(myself, *args, **params): super(MyConnection, myself).__init__(*args, **params) self.assertEqual(params['hostname'], brokers[info['attempt'] % brokers_count]) try: if info['attempt']: raise Exception('connection timeout') finally: info['attempt'] += 1 self.stubs.Set(kombu.connection, 'BrokerConnection', MyConnection) self.config(rabbit_hosts=brokers, rabbit_max_retries=1) # starting from the first broker in the list connection = self.rpc.create_connection(self.FLAGS) # reconnect will advance to the next broker, one broker per attempt, # and finally wrap back to the start of the list once its end is # reached for i in range(1, brokers_count) + [0]: self.assertRaises(rpc_common.RPCException, connection.reconnect) connection.close()
def main(): if len(sys.argv) < 4: print_help() return address = sys.argv[1] exchange_name = sys.argv[2] message = sys.argv[3] connection = kombu.BrokerConnection(address) exchange = kombu.Exchange(exchange_name) channel = connection.channel() producer = kombu.Producer(channel, exchange=exchange, serializer="json") producer.publish(message) producer.close() connection.close()
def start_worker(callback, name, deployment_id, deployment_config, exchange, logger): host = deployment_config.get('rabbit_host', 'localhost') port = deployment_config.get('rabbit_port', 5672) user_id = deployment_config.get('rabbit_userid', 'guest') password = deployment_config.get('rabbit_password', 'guest') virtual_host = deployment_config.get('rabbit_virtual_host', '/') durable = deployment_config.get('durable_queue', True) queue_arguments = deployment_config.get('queue_arguments', {}) topics = deployment_config.get('topics', {}) logger.info("%s: %s %s %s %s %s" % (name, exchange, host, port, user_id, virtual_host)) connection = create_connection(hostname=host, port=port, userid=user_id, password=password, transport="librabbitmq", virtual_host=virtual_host) worker = Worker(callback, name, connection, deployment_id, durable, queue_arguments, exchange, topics[exchange], logger) worker.run() connection.close()