• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic JMS question  RSS feed

 
Ranch Hand
Posts: 59
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a basic question on JMS architecture.

On the receiving end, we look up for the connection factory by name, get a connection, procure a Queue session object, then look for the Queue by name, create a receiver providing this queue name to the session object and register a listener with the session.

So this listener implements the message listener interface so that the onMessage() event is triggered automatically when a JMS message arrives at the specified queue.

But, in the receiver class (which does the above sequence of procuring a session object and registering a listener to the specified queue), I see this while loop coded:

while (true)
{

    Thread.sleep(1000);
}

My question is, what does this while(true) condition stand for? Next, what way the onMessage() of the listener is related to this Thread.sleep process? I'm not getting the actual sequence. Can someone please explain.

Thanks.
 
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression "while(true)" means simply "do forever", since true is always true and the while condition is repeated until false - which will never be the case here. Of course, in real life, external forces will eventually break the loop.

I actually did work with a programming language long ago that literally had a "DO FOREVER" statement, but most languages just fake it with something like "while(true)".

So what you have here is a basic polling loop. Once entered, the thread sleeps for a set period of time, checks for something to do and does it, then cycles back around for another nap/text/execute cycle ad inifinitum.
 
Ranch Hand
Posts: 973
9
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if using an endless loop in the presence of a JMS listener is or not a good practice, since the listener is awakened by the JMS machinery every time a message is send to a queue or to a topic.
I'd bet "no".
 
Tim Holloway
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:I wonder if using an endless loop in the presence of a JMS listener is or not a good practice, since the listener is awakened by the JMS machinery every time a message is send to a queue or to a topic.
I'd bet "no".



I had misgivings about that too. These days, polling is usually not the preferred option. It's extra overhead and it can result in delays in processing while the thread waits to wake up.

But that's a valid poll loop, anyway. The ones you DON'T want use a counting loop instead of a sleep delay.
 
Ravi Desigan
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Claude Moore wrote:I wonder if using an endless loop in the presence of a JMS listener is or not a good practice, since the listener is awakened by the JMS machinery every time a message is send to a queue or to a topic.
I'd bet "no".



I had misgivings about that too. These days, polling is usually not the preferred option. It's extra overhead and it can result in delays in processing while the thread waits to wake up.

But that's a valid poll loop, anyway. The ones you DON'T want use a counting loop instead of a sleep delay.



Hello Tim,

You have mentioned about a process that I precisely have question about. Will you please help me understand the following?

1. What happens to the polling thread when a message arrives (and onMessage() of the listener gets triggered)? Does it get interrupted and throw an interrupted exception?

2. Does the polling thread actively look for message or the arrival of message triggers the thread to wake up and do some processing?

I thank you for your earlier reply regarding the while (true) condition.

 
Tim Holloway
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually you either poll or are interrupt-driven, but not both.  Polling is extra overhead, so if you are wired for interrupts (which are usually done via callbacks in Java), then polling has no advantage - the interrupt will almost invariably detect the event first.

Interrupts also have overhead, and that overhead becomes significant when interrupts occur frequently, so in cases like that, polling may actually be less overhead.

The other time that polling is likely to be used is when the event-generator cannot post unsolicited events to the consumer. For example, if you connect a client computer to a server, but the client computer cannot initiate connection so the server connects, checks for client needs, then initiates a conversation to handle those needs. Back in school, I worked with a remote workstation that had that limitation.
 
Sheriff
Posts: 21305
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is the entire loop body, then it does nothing but sleep, wake up a second later, sleep again, etc. It doesn't actually do anything, except prevent the thread that's running in from exiting. It's definitely not actually polling. The onMessage method is most likely called from another thread, which means that this thread is not interrupted but continues to go on.
 
Ravi Desigan
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim, nice to know the difference between polling and interrupts. Although I do not understand the example you have presented. I appreciate your help.

@Rob, the onMessage is implemented by a separate class (that implements messageListener interface) and it is registered with the while (true) thread as its listener for a specific queue in case a message arrives. So that is there connection.

My question is, in case this while (true) and sleep thread does not poll, in that case, is it getting interrupted when onMessage() callback gets triggered?

Someone asked me in a recent interview about the JMS architecture for asynchronous message process. My memory was vague at that time and I said that the receiver polls. He immediately said "No".

Now I am referring to articles all over the net, none of it explains exactly what happens when the message arrives - they only explain how to set up the code.

I am giving the code just in case:

The following is the class implementing messageListener interface:



This class is the thread (the receiver) of the JMS queue messages:



Of course in the above receiver class, the while (true) is not coded (the lines of code following the //Consumer comment) but the thread goes to sleep. Now when onMessage() of this thread registered listener class gets triggered, how does this thread respond? Does it get interrupted? And what does it do, if another message arrives while this thread is processing the current message in its onMessage()?

Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!