0

I am using a DLQ listener to be invoked every minute via Spring scheduling as follows.

@Scheduled(fixedDelay=60000)
public void retryQueue() {
 ConnectionFactory cf =
          new ActiveMQConnectionFactory(uname, pwd, brokerName);
      Connection cn = cf.createConnection();
      cn.start();
      Session sess = cn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Destination queue = sess.createQueue(retryQueue);
      MessageConsumer messageConsumer = session.createConsumer(queue);
 //further message processing here
}

Now my question is that every minute a new connection gets made and destroyed. How can I let Spring handle that connection management so that it does not create a connection every time.

station
  • 6,715
  • 14
  • 55
  • 89

1 Answers1

0

Use a CachingConnectionFactory to wrap the ActiveMQConnectionFactory it will also cache the consumer, by default. If you just want the connection, disable consumer caching, or use the super class SingleConnectionFactory.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • This only works if the `SingleExceptionStrategy` is created outside of `retryQueue()` (i.e. as a bean). – Petter Nordlander Jun 18 '18 at 05:18
  • @PetterNordlander Can you explain that part more please – station Jun 19 '18 at 05:50
  • Also I deployed SingleConnectionFactory it resulted in just tipping my NAT ActiveConnectionCount in EC2 – station Jun 19 '18 at 08:03
  • @station I accidentally wrote `SingleExceptionFactory` instead of SingleConnectionFactory, but I guess you figured. Non the less, the `SingleConnectionFactory` must be instanciated once in the code, not every time the `retryQueue` method executes. Otherwise it will function just like an ordinary ConnectionFactory. – Petter Nordlander Jun 20 '18 at 06:34