Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6245349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:29:19+00:00 2026-05-24T12:29:19+00:00

The code below does NOT work: Cause: I assume I tracked down the cause

  • 0

The code below does NOT work:

Cause:

I assume I tracked down the cause to:
http://community.jboss.org/thread/150988
=> This article says that HornetQ uses Weak References.

My Question:
Why does the code not run? (I have this code running with a slight different implementation, but the code blow fails repeatedly). My only guess is, that the
following references:

private Connection connection = null;
private Session session = null;
private MessageProducer producer = null;

are not regarded as strong references? (And this leads to the fact that the garbage collector removes the objects… But way arent they strong references?

Or is there another problem with the code (as said the code runs fine if I copy everything into one single method. But if I use the Singleton approach below the code does not work…) Another assumption was that it might have to do with ThreadLocal stuff, but I am using only a single thread…

The Code not working (stripped down):

public class JMSMessageSenderTest {
    private static final Logger logger = Logger.getLogger(JMSMessageSenderTest.class);

    private static JMSMessageSenderTest instance;

    private Connection connection = null;
    private Session session = null;
    private MessageProducer producer = null;

    private JMSMessageSenderTest() {
        super();
    }


    public static JMSMessageSenderTest getInstance() throws JMSException {
        if (instance==null) {
            synchronized(JMSMessageSenderTest.class) {
                if (instance==null) {
                    JMSMessageSenderTest instanceTmp = new JMSMessageSenderTest();
                    instanceTmp.initializeJMSConnectionFactory();
                    instance = instanceTmp;
                }
    } }
        return instance;
    }


    private void createConnectionSessionQueueProducer() throws Exception {

        try {


            Queue queue = HornetQJMSClient.createQueue("testQueue");

            connection = initializeJMSConnectionFactory();

             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

             producer = session.createProducer(queue);

             connection.start();

        } catch (Exception e) { 
            cleanupAfterError();
            throw e;
        }

    }


    private void cleanupAfterError() {

        if (connection != null){
             try{
                 connection.close();
             }catch(JMSException jmse) {
                 logger.error("Closing JMS Connection Failed",jmse);
             }
         }
    session = null;
    producer = null;

    }


    public synchronized void sendRequest(String url) throws Exception {

            if (connection==null) {
                createConnectionSessionQueueProducer();
            }

        try {

             //HERE THE EXCEPTION IS THROWN, at least when debugging
             TextMessage textMessage = session.createTextMessage(url);

             producer.send(textMessage); 

            } catch (Exception e) {
            cleanupAfterError();
            throw e;
        }

    }

        private Connection initializeJMSConnectionFactory() throws JMSException{

        Configuration configuration = ConfigurationFactory.getConfiguration(null, null);

        Map<String, Object> connectionParams = new HashMap<String, Object>();

        connectionParams.put(org.hornetq.core.remoting.impl.netty.TransportConstants.PORT_PROP_NAME, 5445);
        connectionParams.put(org.hornetq.core.remoting.impl.netty.TransportConstants.HOST_PROP_NAME, "localhost");

        TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams);

        ConnectionFactory connectionFactory = (ConnectionFactory) HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);

    //      return  connectionFactory.createConnection(login, password);    
        return connectionFactory.createConnection();

    }


    /**
     * Orderly shutdown of all resources.
     */
    public void shutdown() {
        cleanupAfterError();
    }


}

TestCode to run the code above

JMSMessageSenderTest jmsMessageSender = JMSMessageSenderTest.getInstance();
jmsMessageSender.sendRequest("www.example.com)");
jmsMessageSender.shutdown();

Gives the following error:

I'm closing a JMS connection you left open. Please make sure you close all JMS connections explicitly before letting them go out of scope!
The JMS connection you didn't close was created here:
java.lang.Exception
    at org.hornetq.jms.client.HornetQConnection.<init>(HornetQConnection.java:152)
    at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:662)
    at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:121)

Solution:

1.) You also have to Keep a reference to the ConnectionFactory (see the answer from Clebert below)

private ConnectionFactory factory = null;

2.) AND this code contains a severe hidden bug (that is not so easy to spot):
I initialized the Connection in the Constructor as well as in the createConnectionSessionQueueProducer() method. It will therefore override the old value and (as it is a Ressource that needs to be closed) will lead to a stale connection that HornetQ then will close and will then throw the error.

Thanks very very much! Markus

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:29:20+00:00Added an answer on May 24, 2026 at 12:29 pm

    HornetQ will close the connection factory when the connection factory is released.

    You need to hold a reference for the connection factory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
In the code below, why does the open function work but the close function
Why does the code below return true only for a = 1? main(){ int
In the below code sample, what does {0:X2} mean? This is from the reflection
Code below is not working as expected to detect if it is in design
The code below gives an error: Property 'Int32 Key' is not defined for type
Update: I checked the answer before I fully tested it still does not work.
Why does changing the below code from the Old to New entry fix the
The variable $a below does not seem to parse properly when if I attempt
The code below shows a sample that I've used recently to explain the different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.