Wednesday, July 04, 2012

EJBHome and EJBObject in EJB

Client can not directly create object of session, entity or message driven beans. Ok
let’s try to get in to depth of how session and entity beans can be accessed by end clients.
There are two important interfaces which needs to be used in order that we can create
and use session, entity and message driven bean objects:-

Bean Interface
Client uses the Bean interface to communicate with the session bean which resides on the
EJB application server. The Bean interface extends the EJBObject interface of the
javax.ejb package. This interface has all the methods, functions which the final session
bean has.

Home Interface
In order the client gets a reference to the Bean interface it must call the Beans home
interface. The Home interface extends EJBHome interface of the javax.ejb package.
Home Interface of a bean is responsible for creating the object and giving the reference
of the Bean interface.


Above figure depicts the complete flow of how the Java application or the Java client
gets a reference of the customer bean interface. 
Step1 Java application calls the home interface to get a reference of customer session object. 
Step2 Home interface then creates an object of customer session bean which can be referenced by using customer bean interface.
Step3 java clients gets reference of the customer session bean object through
customer bean interface.

Local interfaces :
On of the biggest issues of creating objects using home interface is performance. Below
are the steps which follow when you call the EJB object:-
  • JAVA client calls the local stub.
  • Stub marshal the values in to some other form which the network understands and sends it to the skeleton.
  • Skeleton then de-marshals it back to a form which is suitable for JAVA.Skeleton then calls the EJB object and methods.
  • EJB object then does object creation, connection pooling, transaction etc.
  • Once EJB object calls the bean and the bean completes its functionalities. All the above steps must again be repeated to reach back to the JAVA client.

So you can easily guess from the above step that its lot of work. But this has been improved in EJB 2.0 using Local objects. Local objects implement local interface rather than using
remote interface. Just to have a comparison below are the steps how the local object
works.
  • JAVA client calls the local object.
  • Local object does connection pooling, transactions and security.
  • It then passes calls the bean and when bean completes its work it returns the data to the Local object who then passes the same to the end client.
You can understand from the above steps we have by passed completely marshalling and
de-marshalling.
Limitations of using Local object?
Local object only work if you are calling beans in the same process. Second they marshal
data by ref rather than by Val. This may speed up your performance but you need to
change semantics for the same. So finally it’s a design and the requirement decision. If
you are expecting to call beans remotely then using local object will not work.

No comments: