File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes handling synchronous/asynchnronous modes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "handling synchronous/asynchnronous modes" Watch "handling synchronous/asynchnronous modes" New topic

handling synchronous/asynchnronous modes

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi there,

I would want to know your viewpoint on this design. There are two modes of triggering an application.
In asynch we are planning to have an MDB which consumes message from the queue and invokes a sessionBean which in turn calls the core java biz application.
The second (synch) mode (web app - jsp/servlet) talks directly to the aforementioned sessionBean.
We need to respond back to the calling app in both the cases. So with the asynch mode we are thinking on the lines of having another (outbound) queue which will have the resultant message pushed by the sessionBean.
Is it a good idea to have the sessionBean discern its caller (MDB or servlet) and handle the response mechanism accordingly.
Or is there a better way of handling the same inside the SessionBean.

Do let me know your thoughts on the same.

Mudi Appu
Ranch Hand

Joined: Apr 06, 2005
Posts: 44
If I understood correctly, onMessge() method of your MDB invokes the sessioBean method and it returns the response back. Now what you have to do is push the response into the outbound queue. In order to do this you don�t need a session bean because of you are not going to expose a method (entry point) to outside the container here. Push the message to outbound queue directly by your onMessage().

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
No, your session bean should definitely *not* know about its caller, as that would be tight coupling, leading to rigidity of the system - everytime a caller changes, or a new caller gets added, there is a likelihood that you also need to change the session bean. It's also hard to reuse the session bean in a different context. Not good.

What you could do - if this needs to be done by the session bean at all - , is using the Strategy design pattern: create an interface for handling the message, for example called MessageReceiver, with a single method that simply gets a message. Have to implementations, one for synchronous, one for the asynchronous case. Pass in the right implementation when you call the session bean. The session bean then just needs to delegate to the receiver, without knowing at all what happens to the message after that.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The business logic of the application should be coded in a Session EJB or POJO class. Coding business logic in a Message-Driven Bean is a bad practice. The only code that should be in the MDB is code that handles processing of a message, and in this case, handling the response.

MDB receives message and invokes business logic in Session EJB.

Servlet receives request from user and invokes business logic in Session EJB.

Code in both servlet and MDB should handle response from Session EJB accordingly.
[ October 22, 2008: Message edited by: James Clark ]
I agree. Here's the link:
subject: handling synchronous/asynchnronous modes
It's not a secret anymore!