Sunday, January 12, 2014

Boost performance JAVA EJB

Using EJB (entity java beans) for enterprise client server application may be a good idea.
but it is highly important to divide your code into many EJB's as you can , for each EJB class being responsible for its logical functionality.  

But doing the above can result in a performance issue , because jumping and calling many time to the EJB server from the client code takes time.

for example having this EJB:

@remote
MyEjb
..
..
public List<Object> getObject1()
{
return new ArrayList<Object>();
}

public List<Object> getObject1()
{
return new ArrayList<Object>();
}


Client code:

public static void main (String []args)
{
List<Object> list1 = MyEjb.getObject1();
List<Object> list2 = MyEjb.getObject1();
}
we have here 2 calling and to the EJB server. which takes time....
and time is something that we want to save.

A Much better option would be:

@remote
MyEjb
..
..
public Pair<List<Object>,List<Object>> getObjects()
{
return new Pair<List<Object>,List<Object>>( new ArrayList<Object>,  new ArrayList<Object>);
}

Client code:

public static void main (String []args)
{
Pair pair = MyEjb.getObjects();
//and then retrieve the data from the pair.
}

this is a better option!!

*please note: this is a sample code , calling the EJB server must be done via the interface.

No comments:

Post a Comment