ActiveMQ How to handle closed session

I am using ActiveMQ to enqueue email messages, the consumer reads the queue and sends out emails.

At startup, I register a producer and cache it forever.

PooledConnectionFactory factory = new PooledConnectionFactory(new ActiveMQConnectionFactory(jmsBrokerUserName, jmsBrokerPassword, activeMQBrokerURL)); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageProducer producer = session.createProducer(destination);

From time to time, the producer fails to enqueue the message as the connection gets closed.

Caused by: javax.jms.IllegalStateException: The Session is closed at org.apache.activemq.ActiveMQSession.checkClosed(ActiveMQSession.java:767) ~[activemq-client-5.10.0.jar:5.10.0] at org.apache.activemq.ActiveMQSession.configureMessage(ActiveMQSession.java:755) ~[activemq-client-5.10.0.jar:5.10.0] at org.apache.activemq.ActiveMQSession.createTextMessage(ActiveMQSession.java:438) ~[activemq-client-5.10.0.jar:5.10.0] at org.apache.activemq.jms.pool.PooledSession.createTextMessage(PooledSession.java:242) ~[activemq-jms-pool-5.10.0.jar:5.10.0]

Can somebody please let me know what is the best way to handle closed sessions? Should I re-register my producer? Or is there a way to reopen session?


When using JMS you shouldn't really cache the JMS Session (and anything hanging of that such as a Producer). The reason being is that the JMS Session is the unit of work within JMS and so should be a short lived object. In the Java EE world that JMS Session might also be enlisted with a global transaction for example and so needs to be scoped correctly.

The JMS Session instance can not be used concurrently by multiple threads, so the best advise is to create the JMS Session on each thread you are using and close the jms session when you have finished that unit of work (sending a message or a collection of messages in a transaction)


  • JMS and IBM WebSphere not generating COD
  • How to implement Logical ordering in IBM MQ in Java?
  • spring mqtt: catch ConnectException
  • how to send msg to Websphere MQ with specific reply to MQ MGR using JMS
  • Visual Studio Unit Test Security Exception
  • Unable to “Peek” messages from an Azure Service Bus Queue using AMQP and Node [duplicate]
  • Oozie shell script action
  • Possible Parser for Unknown String Format(soup?) from SUDS.client
  • how to get the raw version of a template string in iojs
  • How do I browse a Websphere MQ Queue through all messages?
  • OpenERP : Multiple module overriding onchange function
  • ValueError: 0 is not in list when running this code
  • Gembox - get address of ExcelCell
  • SVN Commit failed with syntax error, unexpected '[' [duplicate]
  • Bash: How to do a variable expansion within an arithmetic expression?
  • Vim key mapping works in command editor, not in .vimrc - why?
  • Color Space Mapping YCbCr to RGB
  • openerp Message_post error..NameError: global name '_' is not defined
  • Google Contacts API asp.net settings and authorization token
  • Retrieve google contact based on contact Id
  • How to decode route points from the JSON Output Data?
  • VBA - open excel, find and replace, delete row, save as csv
  • How do I chomp a string if I have Perl 4?
  • Yahoo finance historical stock price power query returns 301 response
  • Casting double to integer when is it undefined behaviour
  • Vim folding : how to hide all the single lines not containing a search pattern (or fold zero line)?
  • Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?
  • jquery code not working without breakpoint
  • Google Maps api v3 get start and end coordinates of a street
  • NHibernate manually control fetching
  • Unable to send e-mail through Java
  • Salesforce Different WSDL files and when to use
  • SQLite connection strategies
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • How can I extract results of aggregate queries in slick?
  • Swift: Switch statement fallthrough behavior
  • How to test if a URL from an Eclipse bundle is a directory?
  • Debugging ASP.NET on a built-in web server suddenly stops
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • How to get NHibernate ISession to cache entity not retrieved by primary key