dcsimg
Behaviour of Jms without Listener ,is it acceptable?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   R_Kannan
Posted On:   Thursday, April 18, 2002 10:01 PM

Hai, Usually for the Receiver application inorder to get reference of Message Interface, he has to register the listener and implement onMessage(Message msg) ,but I saw this example which is working fine without the help of Listener . ========================= public class QReceiver { public static void main(String[] args) {    try{    InitialContext initContext = new InitialContext(); QueueConnectionFactory factory =(QueueConnectionFactory) initContext.lookup("factoryName"); Queue queue = (Queue) initContext.lookup("queueName");    More>>

Hai,

Usually for the Receiver application inorder to get reference of Message
Interface, he has to register the listener and implement onMessage(Message
msg)
,but I saw this example which is working fine without the help of
Listener
.

=========================

public class QReceiver

{

public static void main(String[] args)

{   

try{   

InitialContext
initContext = new InitialContext();

QueueConnectionFactory factory =(QueueConnectionFactory)
initContext.lookup("factoryName");

Queue queue = (Queue) initContext.lookup("queueName");

QueueConnection connection = factory.createQueueConnection();

QueueSession session =connection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

QueueReceiver receiver = session.createReceiver(queue);

connection.start();

Message msg = null;

String msgText = "";

do {  

msg = receiver.receive();

if (msg instanceof TextMessage)

{   

msgText = ((TextMessage)msg).getText();

System.out.println("Message Received = "+ msgText );

}

}while(msgText.equals("quit"));

} catch
(Exception e) {}

}

}

=========================

My Question is :


  1. To receive the message without the help of MessageListsener is it
    acceptable using the above code?If acceptable ,when to go for this knid of coding?
  2. This program is it asynchronous behaviour or synchronous behaviour?



Thanks,

Ranga.    <<Less

Re: Behaviour of Jms without Listener ,is it acceptable?

Posted By:   Martin_Kernland  
Posted On:   Friday, April 19, 2002 12:42 AM

Still not giving up on JMS, are you ;-))


  1. Does it work? From just looking at it I would say it is "acceptable"
    (meaning "if it works, it is ok"). Just one thing: you are missing a "!" in the
    while-clause, if not the loop quits when a message comes in without a "quit" as content.

    The difference to the "onMessage" solution is that the receiver.receive() will block until a message arrives.
    So your application is just waiting when there is no traffic. Sometimes this is just what you want, sometimes not.
  2. This is asynchronous behavior. The reason why JMS is asynchronous is because the data (usually) flows
    through a server (hub-and-spoke-architecture). The sender just "fires-and-forgets" his messages to a queue. He
    doesn't care when the message arrives (because it's JMS he knows it will arrive sometime). Just because
    your client is blocking doesn't mean that it is synchronous.

Hope this helps!

-kerny
About | Sitemap | Contact