How to Use Your Session Beans from GWT Client

Recently we have finished a project in which we were using EJB3.0 in the business tier and a GWT client for the user interface. Here i will share you how we connected them in a very convenient way.

Suppose you have an Entity class, Department and a stateless Session Bean named DepartmentBean which implements a remote interface DepartmentBeanRemote. To save a department the bean class
has a create method

public BigInteger create(Department department)
{
try
{
em.persist(department);
em.flush();
return department.getIdNr();
} catch (InvalidStateException in)
{
in.printStackTrace();
} catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}

Now how to call this create method from your GWT client?

First create a GWT RPC Service named DepartmentService and DepartmentServiceImpl is the implementation class of this service interface. The service implemenltation class will serve as a gateway between client and bean class.

Create an instance of DepartmentBeanRemote by dependency injection in DepartmentServiceImpl.
@EJB
private DepartmentBeanRemote departmentBeanRemote;
Now use this instance to call the methods of the DepartmentBean in the following way

public Long create()
{
try
{
Department department = new Department();
department.setIdNr(null);
department.setDepartmentNameSt("Computer Science");
department.setDepartmentDescriptionSt("Most popular department of the university");
BigInteger returnValue = departmentBeanRemote.create(department);
return new Long(returnValue.toString());
}
catch (Exception e)
{
e.printStackTrace();
return null; 
}
}
Call the service method from the client. You have just made a complete path between your GWT Client and the Session Beans.

2 comments:

Anonymous July 14, 2010 at 11:07 PM  

Hi, how you share POJO "Department" between two projects - ejb project and GWT project?

Unknown July 15, 2010 at 7:06 PM  

Department entity class can not be shared between two project. You need another class in GWT Project DepartmentModel which will serve as a DTO. Read this tutorial to get a clear idea <a href = "http://zawoad.blogspot.com/2010/06/google-app-engine-jdo-and-gxtext-gwt.html">Google App Engine, JDO and GXT(Ext GWT) Grid - Make All These Working Together</a>

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers