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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:54:46+00:00 2026-05-28T00:54:46+00:00

How does async JMS work? I’ve below sample code: public class JmsAdapter implements MessageListener,

  • 0

How does async JMS work? I’ve below sample code:

public class JmsAdapter implements MessageListener, ExceptionListener
{
private ConnectionFactory connFactory = null;
private Connection conn = null;
private Session session = null;

public void receiveMessages() 
{
    try
    {
        this.session = this.conn.createSession(true, Session.SESSION_TRANSACTED);

        this.conn.setExceptionListener(this);

        Destination destination = this.session.createQueue("SOME_QUEUE_NAME");

        this.consumer = this.session.createConsumer(destination);

        this.consumer.setMessageListener(this);

        this.conn.start();
    } 
    catch (JMSException e) 
    {
        //Handle JMS Exceptions Here
    }
}

@Override
public void onMessage(Message message) 
{
    try
    {
        //Do Message Processing Here

        //Message sucessfully processed...  Go ahead and commit the transaction.
        this.session.commit();
    }
    catch(SomeApplicationException e)
    {
        //Message processing failed.
        //Do whatever you need to do here for the exception.

        //NOTE: You may need to check the redelivery count of this message first
        //and just commit it after it fails a predefined number of times (Make sure you
        //store it somewhere if you don't want to lose it).  This way you're process isn't
        //handling the same failed message over and over again.
        this.session.rollback()
    }
}

}

But I’m new to Java & JMS. I’ll probably consume messages in onMessage method. But I don’t know how does it work exactly.

Do I need to add main method in JmsAdapter class? After adding main method, do I need to create a jar & then run the jar as “java -jar abc.jar”?

Any help is much appreciated.

UPDATE: What I want to know is that if I add main method, should I simply call receiveMessages() in main? And then after running, will the listener keep on running? And if there are messages, will it retrieve automatically in onMessage method?

Also, if the listener is continuously listening, doesn’t it take CPU??? In case of threads, when we create a thread & put it in sleep, the CPU utilization is zero, how doe it work in case of listener?

Note: I’ve only Tomcat server & I’ll not be using any jms server. I’m not sure if listener needs any specific jms server such as JBoss? But in any case, please assume that I’ll not be having anything except tomcat.
Thanks!

  • 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-28T00:54:47+00:00Added an answer on May 28, 2026 at 12:54 am

    You need to learn to walk before you start trying to run.

    • Read / do a tutorial on Java programming. This should explain (among other things) how to compile and run a Java program from the command line.

    • Read / do a tutorial on JMS.

    • Read the Oracle material on how to create an executable JAR file.

    • Figure out what it is you are trying to do … and design your application.


    Looking at what you’ve shown and told us:

    • You could add a main method to that class, but to make an executable JAR file, you’ve got to create your JAR file with a manifest entry that specifies the name of the class with the main method.

    • There’s a lot more that you have to do before that code will work:

      • add code to (at least) log the exceptions that you are catching

      • add code to process the messages

      • add code to initialize the connection factory and connection objects

    • And like I said above, you probably need some kind of design … so that you don’t end up with everything in a “kitchen sink” class.


    if I add main method, should I simply call receiveMessages() in main?

    That is one approach. But like I said, you really need to design your application.

    And then after running, will the listener keep on running?

    It is not entirely clear. It should keep running as long as the main thread is alive, but it is not immediately obvious what happens when your main method returns. (It depends on whether the JMS threads are created as daemon threads, and that’s not specified.)

    And if there are messages, will it retrieve automatically in onMessage method?

    It would appear that each message is retrieved (read from the socket) before your onMessage method is called.


    Also, if the listener is continuously listening, doesn’t it take CPU???

    Not if it is implemented properly.

    In case of threads, when we create a thread & put it in sleep, the CPU utilization is zero, how doe it work in case of listener?

    At a certain level, a listener thread will make a system call that waits for data to arrive on a network socket. I don’t know how it is exactly implemented, but this could be as simple as an read() call on the network socket’s InoutStream. No CPU is used by a thread while it waits in a blocking system call.

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

Sidebar

Related Questions

Does anyone know why the following code does not work in FireFox but does
FB.api does not work when called right after FB.init. Here is the code snippet
Does anyone have a simple example of implementing an async validation rule in csla?
I found an example for async ftp upload on msdn which does the following
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does jQuery.ajax({async: false}) still use the XMLHttpRequest object? If so, how is the request
I am using the Async CTP library for Windows Phone. Does anyone know how
Possible Duplicate: Why does the async keyword exist In the C# 5 – await
Does Silverlight ever callback in the non-UI thread after an Async task (such as
Specifically, how does it differ from the default ( async: true ) ? In

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.