Sunday, December 2, 2007

EJB Configuration

My EJB Tutorial
For Session Bean



First Create a file which implements SessionBean and extends the file which having the Actual Business Methods.

com.joshi.system.service
SystemBs (An interface for Business Methods)
SystemBsBean (implementation of interface for Business Methods)

com.joshi.system.service.ejb
SystemBsEjbLocal (used to define where a business methods)
SystemBsEjbLocalHome (used to create object of above)
SystemBsEjbBean (The Main file which implements SessionBean
Also define the implementation class
i.e SystemBsBean)

The above is Package Hirarchy for an EJB.



SystemBs.java

package com.joshi.system.service;

public interface SystemBs {

public String deleteData(String[] p_recordlist);
public String createUser(UserDetailVo p_uservo);

}

This file having all the method signature for insert,delete and update. Not for select.

SystemBsBean.java

package com.joshi.system.service;

public class SystemBsBean implementation SystemBs {

/** implementation from SystemBs interface */
public String deleteData(String[] p_recordlist) {

/** Call to related ServiceBean */

return DataServiceBean.getInstance().
deleteData(p_recordlist);

}

/** implementation from SystemBs interface */
public String createUser(UserDetailVo p_uservo) {

/** Call to related ServiceBean */

}

/** A non interface method Usually for Select which not required a
Transaction support */
public ArrayList selectUserByPk(int p_userId) {

/** Call to related ServiceBean */

}

}

This Java file implements all the Business Methods. Commonly the method which required Transaction support (means a EJB) (like insert, delete, update) are implemented here from SystemBs interface. And non transactional method like select also defined here. The selectuserByPk is one of that example.

This Java file is on replace of SystemBsEjbLocalBusiness Java file created in NetBeans IDE. So there is no need of SystemBsEjbLocalBusiness.java file which is auto generated in NetBeans IDE.

SystemBsEjbBean.java

package com.joshi.system.service.ejb;

import com.joshi.system.service.SystemBsBean;
import javax.ejb.*;

public class SystemBsEjbBean extends SystemBsBean implementation SessionBean {

private SessionContext context;

/**
* @see SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
public void setSessionContext(SessionContext aContext) {
context = aContext;
}

/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() {

}

/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() {

}

/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() {

}
/**
* See section 7.10.3 of the EJB 2.0 specification
* See section 7.11.3 of the EJB 2.1 specification
*/
public void ejbCreate() {
// TODO implement ejbCreate if necessary, acquire resources
// This method has access to the JNDI context so resource aquisition
// spanning all methods can be performed here such as home interfaces
// and data sources.
}

// Add business logic below. (Right-click in editor and choose
// "EJB Methods > Add Business Method" or "Web Service > Add Operation")

}


This is the Main Java File. Which is responsible for creating, a SessionBean for us. Here in general coding convention when we follow we built a separate file for Business Method implementation. i.e. SystemBsBean.java. That file should be extends which having a business methods. As we do here. And remember to implement the SessionBean interface.

SystemBsEjbLocal.java

package com.joshi.system.service.ejb;

import com.joshi.system.service.SystemBs;
import javax.ejb.EJBLocalObject;


/**
* This is the local interface for SystemBsEjb enterprise bean.
*/
public interface SystemBsEjbLocal extends EJBLocalObject, SystemBs {


}

In this file we actually write the implementation of Business Method. But while we are using another interface SystemBs.java for that so, just extends that interface.


SystemBsEjbLocalHome.java

package com.joshi.system.service.ejb;

import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;


/**
* This is the local-home interface for SystemBsEjb enterprise bean.
*/
public interface SystemBsEjbLocalHome extends EJBLocalHome {

SystemBsEjbLocal create() throws CreateException;


}

Responsible to create the object for SystemBsEjbLocal.


Change in ejb-jar.xml



System-ejb


SystemBsEjbSB
SystemBsEjbBean

com.joshi.system.service.ejb.SystemBsEjbLocalHome


com.joshi.system.service.ejb.SystemBsEjbLocal


com.joshi.system.service.ejb.SystemBsEjbBean

Stateless
Container





SystemBsEjbBean
*

Required





And web.xml


ejb/SystemLocal
Session

com.joshi.system.service.ejb.SystemBsEjbLocalHome


com.joshi.system.service.ejb.SystemBsEjbLocal

SystemEjb.jar#SystemBsEjbBean



And in SystemBd.java

Add following code:

SystemBd bd;
private SystemBs m_service = null;
String serviceName = null;

try {
SystemBsEjbLocalHome home =
(SystemBsEjbLocalHome)
ServiceLocator.getInstance().getEjbLocalHome(
JNDIUtils.System_EJB_LOCAL_HOME);
bd = new SystemBd();
bd.m_service= home.create();
} catch (Throwable e) {

}
return bd;
}

JNDIUtil.java


package com.joshi.bus.util;

/**
* @author admin
*
*/

public class JNDIUtils {

/** Creates a new instance of JNDIUtils */
public JNDIUtils() {
}

//
// JNDI names of EJB home objects
//
public static final String SYSTEM_EJB_LOCAL_HOME =
"java:comp/env/ejb/SystemLocal";
}

=============================================

FOR ECLIPSE

For creating Stateless SessionBean go through following step:

Download Xdoclet-bin-1.2.3.zip
Extract that any where suppose c:\.
So c:\xdoclet-1.2.3 having 3 folder from that 1 is lib.
Now from Eclipse window -> Preferences -> XDoclet (which is last option ) Click on that
Than Browse select XDoclet home: as C:\xdoclet-1.2.3\lib
Change Version to 1.2.3 Click on Apply..
Then extends + XDoclet
ejbDoclet
Now Deselect CheckBoxes of
RemoteInterface ( which is 2nd Option )
HomeInterface ( which is 3rd Option )
UtilObject ( which is 6th Option )
Session ( which is 13th Option )
Apply and Built a Session Application as Above..

Contributors