Mike
Mike

Reputation: 7914

How JMS work in Java?

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!

Upvotes: 1

Views: 4321

Answers (2)

Stephen C
Stephen C

Reputation: 718718

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.

Upvotes: 5

Logan
Logan

Reputation: 2364

This link looks like a pretty good place with examples using Oracle AQ. There's an examples section that tells you how to setup the examples and run them. Hopefully this can help.

Link to Oracle Advanced Queueing

Upvotes: 0

Related Questions