示例#1
0
    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)
示例#2
0
 def __init__(self, connection_id, timeout=60):
     # Look up the given connection
     connection = getUtility(IBrokerConnection, name=connection_id)
     # Prepare a new one-shot blocking connection
     credentials = PlainCredentials(
         connection.username, connection.password, erase_on_connect=False)
     parameters = ConnectionParameters(
         connection.hostname, connection.port, connection.virtual_host,
         credentials=credentials, heartbeat=True)
     # AMQP-heartbeat timeout must be set manually due to bug in pika 0.9.5:
     if parameters.heartbeat:
         parameters.heartbeat = timeout
     # Create the connection and reset channel
     self.connection = BlockingConnection(parameters=parameters)
     self.channel = None
示例#3
0
def get_rabbitmq_connection():
    connection = getUtility(IBrokerConnection, name='ws.response')
    credentials = PlainCredentials(
        connection.username,
        connection.password,
        erase_on_connect=False,
    )
    parameters = ConnectionParameters(
        connection.hostname,
        connection.port,
        connection.virtual_host,
        credentials=credentials,
        heartbeat=True)
    parameters.heartbeat = connection.heartbeat
    return parameters
示例#4
0
 def __init__(self, connection_id, timeout=60):
     # Look up the given connection
     connection = getUtility(IBrokerConnection, name=connection_id)
     # Prepare a new one-shot blocking connection
     credentials = PlainCredentials(
         connection.username, connection.password, erase_on_connect=False)
     parameters = ConnectionParameters(
         connection.hostname, connection.port, connection.virtual_host,
         credentials=credentials, heartbeat=True)
     # AMQP-heartbeat timeout must be set manually due to bug in pika 0.9.5:
     if parameters.heartbeat:
         parameters.heartbeat = timeout
     # Create the connection and reset channel
     self.connection = BlockingConnection(parameters=parameters)
     self.channel = None
示例#5
0
 def connect(self):
     credentials = PlainCredentials(
         self.username, self.password, erase_on_connect=False)
     parameters = ConnectionParameters(
         self.hostname, self.port, self.virtual_host,
         credentials=credentials,
         heartbeat=self.heartbeat and True or False)
     # AMQP-heartbeat timeout must be set manually due to bug in pika 0.9.5:
     if parameters.heartbeat:
         parameters.heartbeat = int(self.heartbeat)
     self._connection = AsyncoreConnection(
         parameters=parameters,
         on_open_callback=self.on_connect)
     self._reconnection_timeout = None
     self._connection.add_on_close_callback(self.reconnect)