How to reduce time in netwrok traffic between EJB and servlet?

Discussions

EJB design: How to reduce time in netwrok traffic between EJB and servlet?

  1. Hi,
    Does anybody suggest a solution in reducing the time of network traffic in between EJB and Servlet? Can I zip the object passed by value from EJB to Servlet? Or is there any other way round to reduce the time taken?
    Thanks,
    Prateek
  2. There are several strategies, some structural and some technical. I find the following to be the most effective:
    1. Use a coarse-grained communication model. Don't make a lot of small remote calls - use few calls, each responsible for many things.
    2. If (1) is cumbersome or impractical, use the Command pattern. Instead of bringing the data to your servlets, send the functionality to the App server. You can find plenty of descriptions for this pattern on the Web (or in the excellent "Design Patterns: Element of Reusable Software Components" by GoF).
    3. If you really do pass a lot of large data that is generally compressible (e.g, text yes, images no) you can try to compress it. I would only consider this technique in rare occasions. You can find some design alternatives and sample code in http://www.theserverside.com/discussion/thread.jsp?thread_id=11763 (once again, I am not responsible for the code I posted there).

    Hope that helps.
    Gal
  3. Gal,

    please explain me how using commands can improve performance, why it may be faster than sending data to the ejb?

    Is it because you could send many commands at once (= one remote call only)?

  4. Well, lets assume you have very extensive operations that need to access many business objects. One option is to make all each of those a method of a session bean and have them run on the server side. That was my first suggestion. However, sometimes you may have many of these operations, you may need to choose which operation to run on-the-fly, you may want to be able to refine the implementation of an operation into a new one, etc. All these objectives can be met using the Command pattern. However, using it in the "normal" way would hurt the efficiency of your code: extensive operations will have to pull a lot of data through the network, and push a lot of data back on the network. You can achieve better efficiency by sending the command object to some session bean which will invoke it on the App server itself. This can be described as "sending the logic to the data", as opposed to "bringing the data to the logic" which is the traditional approach.
    Note that EJB doesn't inherit Java's built-in capabillity to perform dynamic loading of classes from remote locations, so you still have to put your command classes in the App server codebase.

    Gal
  5. You can consider running the EJB server and servlet engine on the same machine, or even in the same JVM.

    Tom