sabato 22 dicembre 2012

Interfaces: Remote vs Local

Many of you may be confused as i was upon the terminology and the usage of interfaces.
Should they be remote? or should they be local?


Nice question, weird anser. Depends is always a good answer but I will explain here my point of view, after having read here and there some hints.

When you design your appliaction you first decide what it has to do, and doing so, you will decide the logic that your EJB will hold.
The separation between the "Logic"(hold by the EJB tier) and the "Presentation"(hold by the web application, or generally your client) following the EJB architecture is foundamental, and here it comes the time to decide what you want your client to be.

i.e. Suppose you want your logic to implement a calculator, you will provide sum, subtraction, multiplication and division through EJB.

The interfaces are just the view exposed to the client of your Logic, without knowing of course how it is implemented.

i.e. your client will provide a sort of gui or some input methods for the numbers and the operation you want to do, hence calling the EJB it will perform the computation.

At this point you may wonder where you should have your client.

At the very beginning EJB was meant to provide remote access throught RMI standard, so the client was meant to be on a different machine from the one who acted as container for EJBs (remote).
Moving to version 2.0 the Java EE team decided to provide also a different approch to EJB. You wonder: why should i run through RMI, sockets and network connection when i have the client on the same JVM as the EJB container?
So they introduced the local interface that allows the client to call directly methods on bean, bypassing the RMI sematics(local), you will find that you write local interfaces in some special cases.

How much good is this idea to have it local?? I can't say. Still you should have a stub and a skeleton as the RMI paradigm impose, but you don't have the network in the middle because the client and the server(ejb container) run on the same java virtual machine and hence on the same heap.
Instead with the remote interface you have two different JVM and two different heaps.

Anyhow, you have to choose based on your client-program. For sure you will not have both the implementation, it will be useless. Think about it, should you have a remote interface and a local interface that expose the same things? or partial things on the remote and partial on local?

Furthermore in my opinion i think that if you are going to use EJB to provide the logics, your client will be running on a different machine.

other sources:

book: Head first EJB