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 6750305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:47:31+00:00 2026-05-26T12:47:31+00:00

I have the following code. I m trying to access a queue and consume

  • 0

I have the following code. I m trying to access a queue and consume resources, well i dont know how many elements are in the queue and how many left, so i am doing while(true) but in this case, i cant close the connection which causes errors, later on, like the process doesnt die. even though i stopped it.

How can i find if there are more items in the queue then do the following ? i looked at ActiveMqQueueBrowser but it s internal so i cant really use it easily.

What would you do? What do u recommend?

    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
    Connection connection = connectionFactory.createConnection();
    connection.start();

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

    Destination destination = session.createQueue(subject);

    MessageConsumer consumer = session.createConsumer(destination);

    while(true){
            Message message = consumer.receive();

            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("message from queue : '" + textMessage.getText() + "'");
            }
    }

   // unreachable code. compiler complains.
   // connection.close();
  • 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-26T12:47:32+00:00Added an answer on May 26, 2026 at 12:47 pm

    Sounds like you need to have another thread. How do you know when to be done listening? If its where the consumerQueue no longer has a value, try this:

    while(true){
            Message message = consumer.receive(//some int timeout);
            if (message == null)
            {
                break;
            }
    
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("message from queue : '" + textMessage.getText() + "'");
            }
    }
    

    Otherwise you are right, the loop will never return and you won;t be able to close the connections correctly when the JVM comes down.

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

Sidebar

Related Questions

I am trying to create a table in Access. I have the following code:
I'm trying to use opengl in C#. I have following code which fails with
I have the following code that I am trying to extract the systems proxy
I have the following code that i am trying to change from a regular
I have the following code sample that im trying to wrap my head around
I have the following code, I'm trying to get a table with 4 columns
I have the following code for adding to/extracting from Zip. I'm trying to refactor
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a
I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code
Im trying to get debugging working without an app.config. I have the following code:

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.