spring-cloud / spring-cloud-connectors

Library to let cloud applications connect to services
Apache License 2.0
185 stars 161 forks source link

Should be able to set rabbitmq heartbeat in spring cloud xml declaration #101

Closed poblin-orange closed 9 years ago

poblin-orange commented 9 years ago

While using spring cloud on cloudfoundry with rabbitmq cf-service-contrib, we have some connection lost, probably due to lack of heartbeat on the AMQP connections.

The default requestedHeartbeat in spring amqp is 0 (means disabled) https://github.com/rabbitmq/rabbitmq-java-client/blob/master/src/com/rabbitmq/client/ConnectionFactory.java

We can not set the heartbeat in the spring cloud xml construction

scottfrederick commented 9 years ago

@poblin-orange We can add requestedHeartbeat (and maybe a few other ConnectionFactory options) to RabbitConnectionFactoryConfig to allow users to pass that as an option.

scottfrederick commented 9 years ago

Fixed via https://github.com/spring-cloud/spring-cloud-connectors/commit/e2cd8edda510562fca8093c8c3b49d69b6b487d9 and https://github.com/spring-cloud/spring-cloud-connectors/commit/30c40dd0d8ed6b884cc516a98ce068f8bc1b0d56

This change allows a map of connection properties to be provided to customize the created ConnectionFactory bean.

With XML configuration, you can now do something like this:

<cloud:rabbit-connection-factory id="rabbitmq" service-name="my-rabbitmq">
    <cloud:connection-properties>
        <entry key="requestedHeartbeat" value="5"/>
        <entry key="connectionTimeout" value="10"/>
    </cloud:connection-properties>
</cloud:rabbit-connection-factory>

With Java config, you can now do something like this:

@Configuration
class RabbitConfig extends AbstractCloudConfig {
    @Bean
    public ConnectionFactory connectionFactory() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("requestedHeartbeat", 5);
        properties.put("connectionTimeout", 10);
        RabbitConnectionFactoryConfig serviceConfig = new RabbitConnectionFactoryConfig(properties);
        return connectionFactory().rabbitConnectionFactory("my-rabbitmq", serviceConfig);
    }
}