Confused over "Asynchronous"

Discussions

EJB design: Confused over "Asynchronous"

  1. Confused over "Asynchronous" (2 messages)

    Hi guys.


    I'm confused (again!).JMS (Java Messaging Service) is a means of allowing an application communicate with other systems in an asynchronous non-blocking fashion - In other words, our application puts something on the queue and forgets about it.

    I'm trying to figure out how this works. - Is it by means of spawning new threads - let me explain. Consider the following 4 lines of Java code (say withina method):

    Line 1: Object1 a = new Object1("fjkdjfks");
    Line 2: a.executeLine2();
    Line 3: a.SendMessageToJMSQUEUE();
    Line 4: System.out.println("Finished")


    Java works in a SERIAL SYNCHRONOUS fashion - i.e. Line2 code will NOT begin to execute until LINE1 has successfully completed.
    Line3 won't complete until Line2 has completed etc.

    However, if the code in Line2 [a.executeLine2() ] happens to spawn a seperate thread, Line3 can continue even though this new thread continues to do some processing in parallel [i.e. it turns my synchronous mode of operation into asynchronous mode] -

    So my questions:

    1) Is my understanding correct?

    2) Does JMS work in this fashion? In other words, does JMS effectively spawn new threads?

    3) If my application sends something to JMS, what does JMS complete before giveing control back to the calling application - in other words, does the message have to get sent to the message queue and persisted (for message guarantee) before the calling application continues to execute the next line of code? - Is this JMS Service Provider dependent?

    Thanks for your help

    Paddy
  2. Confused over "Asynchronous"[ Go to top ]

    Line 1: Object1 a = new Object1("fjkdjfks");

    > Line 2: a.executeLine2();
    > Line 3: a.SendMessageToJMSQUEUE();
    > Line 4: System.out.println("Finished")

    You are correct that the JVM will execute these in order. The question that needs to be answered is "What does Line 3 do?" When the thread that calls these lines hits Line 3, it merely sends the message and returns (It may or may not wait for an acknowledgement that the message was received by the *server* [NOT the destination], depending on how you've set up your queue). The *processing* of the message occurs asynchronously. The JMS server will call the onMessage callback of the receiver whenever the message is received. This indeed occurs in a separate thread owned by the JMS server.

    Line 3 sends the message, but does not wait for the message to be processed. To answer your third question, if you set the message as persistent, the server guarantees that the message is persisted before Line 3 returns.

    --Steven
  3. Confused over "Asynchronous"[ Go to top ]

    the simples way to understand this is to percieve the queue as a Persistance(or may be not) Store... so a.SendMessageToJMSQueue(); is basically...

    1. Writing message to a store
    2. gaining ack that the message has been written to the store.

    note that this is merely an ack that the store (i.e. the JMS provider) has received the message. and then you'r program goes on it's own merry way...