Search code examples
spring-amqpspring-rabbit

Which is better CachingConnectionFactory.CacheMode Connection or Channel in Spring AMQP


Hi I have been using Spring AMQP with connection obtained from CachingConnectionFactory with the properties being shown below

<bean id="connectionFactory"
    class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="rabbit-server-fqdn" />
    <property name="virtualHost" value="some-vhost" />
    <property name="username" value="username" />
    <property name="password" value="password " />
   <property name="channelCacheSize" value="25" />
</bean>

Now i need to change my mode to Connection as i need to check open connection

   <bean id="connectionFactory"
    class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="rabbit-server-fqdn" />
    <property name="virtualHost" value="some-vhost" />
    <property name="username" value="username" />
    <property name="password" value="password " />
    <property name="cacheMode" value="CONNECTION" />
   <property name="channelCacheSize" value="25" />
</bean>

So Q1. Will channelCacheSize will work ? Q2. what will be default connection pool size in CachingConnectionFactory ? Q3. Do i need to set additional property ?


Solution

  • The CHANNEL is better, because you don't need to create a new connection for each call, but reuse a shared one.

    The Reference Manual has plenty of facts on the matter. One of them is when do you really need CONNECTION mode:

    The use of separate connections might be useful in some environments, such as consuming from an HA cluster, in conjunction with a load balancer, to connect to different cluster members.

    The shared connection is still can be traced for open/close state via ConnectionListener injected into that CachingConnectionFactory.