0

It seems the JMSProducer is not getting garbage collected and keeps alive after delivering messages to queue, I'm using Spring 3.2.2 and CachingConnectionFactory with Keep-alive setting for sending message.

Producers count keeps increasing every time I send message.

Is it related to spring version I am using?
or am I doing something wrong in my configuration?

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Tarish Saini
  • 340
  • 1
  • 5
  • 14

1 Answers1

1

You need to call close() method on your MessageProducer. As per the Java docs:-

void close()  
           throws JMSException

Closes the message producer.

Since a provider may allocate some resources on behalf of a MessageProducer outside the Java virtual machine, clients should close them when they are not needed. Relying on garbage collection to eventually reclaim these resources may not be timely enough.

As per the spring CachingConnectionFactory docs :-

NOTE: This ConnectionFactory requires explicit closing of all Sessions obtained from its shared Connection. This is the usual recommendation for native JMS access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Session reuse.

So you need to call getCachedSessionProxy instead of getSession and once done with sending message call the close() (in finally block) . As per the source code, the close call to this Session proxy is handled such that the session and messageproducer is reused. Gary's comments states the same.

Manjunath
  • 1,685
  • 9
  • 9
  • I doubt again and again closing producers will effect performance and while using Cachingconnectionfactory it should handle internally. – Tarish Saini Aug 14 '14 at 16:17
  • That's the whole point; the `close()` is intercepted and the producer is put into the cache instead of being actually closed. The factory can't do it "automatically"; the application has to inform it when it is done (an application might want to produce more than one message at a time). – Gary Russell Aug 14 '14 at 19:23
  • @GaryRussell Thanks Gary for your suggestion – Manjunath Aug 15 '14 at 03:46
  • @TarishSaini Please find the updated answer. Kindly let us know if it fixed your issue – Manjunath Aug 15 '14 at 03:47
  • I'm using JMSTemplate, hope it takes care of it explicitly, then also do I need to call close() method??? – Tarish Saini Aug 18 '14 at 05:13