title: Why is Spring JMS creating a JMS connection every second when connecting to an ActiveMQ Broker?
body: <p>I've created a Spring JMS application using version 4.1.2.RELEASE, which is connected to a broker that is running ActiveMQ 5.11.0. The problem that I'm seeing is as follows. In the logs, I notice that every second, I'm seeing a connection being created as such. </p>
  
  <p>2017-06-21 13:10:21,046 | level=INFO | thread=ActiveMQ Task-1 | class=org.apache.activemq.transport.failover.FailoverTransport | Successfully connected to tcp://localhost:61616</p>
  
  <p>I know that it is creating a new ActiveMQ connection each time, because it says successfully "connected" and not "reconnected" as shown in the code located here: <a href="http://grepcode.com/file/repo1.maven.org/maven2/com.ning/metrics.collector/1.3.3/org/apache/activemq/transport/failover/FailoverTransport.java#891" rel="nofollow noreferrer">http://grepcode.com/file/repo1.maven.org/maven2/com.ning/metrics.collector/1.3.3/org/apache/activemq/transport/failover/FailoverTransport.java#891</a></p>
  
  <p>I don't have a caching connection factory set for my consumer, but I'm wondering if the following is the culprit when it comes to why I'm seeing constant connections being created. </p>
  
  <pre><code>factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_NONE);
  </code></pre>
  
  <p>The following post states that consumers should not be cached, but I wonder if that applies to caching the connection + session. If the connection is cached, but the session is not, then I wonder if that creates a problem.</p>
  
  <p><a href="https://stackoverflow.com/questions/21984319/why-defaultmessagelistenercontainer-should-not-use-cachingconnectionfactory/21989895#21989895">Why DefaultMessageListenerContainer should not use CachingConnectionFactory?</a></p>
  
  <p>The following are the configurations that I'm using in my application. I am hoping that it is something that I've misconfigured, and would appreciate any insights that anyone has to offer.</p>
  
  <p><strong>Spring Configurations</strong></p>
  
  <pre><code>@Bean
      public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() throws Throwable {
  DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
          factory.setConnectionFactory(connectionFactory());
  
          factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_NONE);
          factory.setMaxMessagesPerTask(-1);
          factory.setConcurrency(1);
          factory.setSessionTransacted(true);
          return factory;
  }
  
  @Bean
  public CachingConnectionFactory cachingConnectionFactory(){
  CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory());
  cachingConnectionFactory.setCacheConsumers(false);
  cachingConnectionFactory.setSessionCacheSize(1);
  return cachingConnectionFactory;
  }
  
  @Bean
  public ActiveMQConnectionFactory connectionFactory(){
  RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
  redeliveryPolicy.setInitialRedeliveryDelay(1000L);
  redeliveryPolicy.setRedeliveryDelay(1000L);
  redeliveryPolicy.setMaximumRedeliveries(6);
  redeliveryPolicy.setUseExponentialBackOff(true);
  redeliveryPolicy.setBackOffMultiplier(5);
      ActiveMQConnectionFactory activeMQ = new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");
  activeMQ.setRedeliveryPolicy(redeliveryPolicy);
  activeMQ.setPrefetchPolicy(prefetchPolicy());
  return activeMQ;
  }
  
  @Bean
  public JmsMessagingTemplate jmsMessagingTemplate(){
  ActiveMQTopic activeMQ = new ActiveMQTopic("topic.out");
  JmsMessagingTemplate jmsMessagingTemplate = new JmsMessagingTemplate(cachingConnectionFactory());
  jmsMessagingTemplate.setDefaultDestination(activeMQ);
  return jmsMessagingTemplate;
  }
  
  protected ActiveMQPrefetchPolicy prefetchPolicy(){
  ActiveMQPrefetchPolicy prefetchPolicy = new ActiveMQPrefetchPolicy();
  int prefetchValue = 1000; 
  prefetchPolicy.setQueuePrefetch(prefetchValue);
  return prefetchPolicy;
  }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Juan</p>