Thursday, October 10, 2013

Enterprise JavaBeans

1. What is EJB?
Enterprise JavaBeans is a managed, server-side component architecture for modular construction of enterprise application.

2. What is feature of EJB?
EJB provies stateless session beans which is efficient avenue for distributed transactions. It also provides remote and transaction support where simple POJOs does not.

3. Where is EJB best used at?
You should use ejb if it solves a problem for you that one of the light weight frameworks does not. For examples, clustering, fail-over, distributed caching and administration tools.

4. should we use ejb 2.0 or ejb 3.0 if im starting it out to learn ejb?
excerpt from [4]
The goal of EJB 3.0 is target ease of development, the main theme of the JAVA EE 5 platform release. EJB 3.0 is a major simplification over the APIs defined by the EJB 2.1 and earlier specifications. The simplified EJB 3.0 API allows developers to program EJB components as ordinary Java objects with ordinary Java business interfaces rather than as heavy weight components. Both component and client code are simplified, and the same tasks can be accomplished in a simpler way, with fewer lines of code. Because it is much simpler, EJB 3.0 is also much faster to learn to use than EJB 2.1

5. Any book describing EJB which you recommend?
Enterprise JavaBeans 3.1 [5]

6. Show me example of codes that EJB are used at?
in the web environment.


<remote>what should go here??</remote>

class Foo

public UserManager getUserManager() throws HUDException
String ROLE_JNDI_NAME = 'ejb/userManagerBean';

Properties props = System.getProperties();
Context ctx = new InitialContext(props);
UserManagerHome userHome = (UserManagerHome) ctx.lookup(ROLE_JNDI_NAME);
UserManager userManager = userHome.create();
WASSSecurity user = userManager.getUserProfile('user101', null);
return userManager;
catch (NamingException e)
log.error('Error occured while getting EJB UserManager ' + e);
return null;
catch (RemoteException ex)
log.error('Error occured while getting EJB UserManager' + ex);
return null;
catch (CreateException ex)
log.error('Error occured while getting EJB UserManager' + ex);
return null;



// create a home interface
// a remote EJB object - extends javax.ejb.EJBHome
// a local EJB object - extends javax.ejb.EJBLocalHome
public interface MyBeanRemoteHome extends javax.ejb.EJBHOME
MyBeanRemote create() throws javax.ejb.CreateException, java.rmi.RemoteException;

// create an business interface in order to define business logic in our
// ejb object

// a remote EJB object - extends javax.ejb.EJBObject
// a local EJB object - extends javax.ejb.EJBLocalObject
public interface MyBeanRemote extends javax.ejb.EJBObject
void doSomething() throws java.rmi.RemoteException;

// our ejb
public class MyBean implements javax.ejb.SessionBean
// why create method ? Take a special look at EJB Home details (above)
public void create()

public void doSomething() throws java.rmi.RemoteException
// some code


<?xml version='1.0' encoding ='UTF-8'?>
<ejb-jar xmlns='' xmlns:xsi='' xsi:schemaLocation=' version='2.1'>

and put in META-INF directory.


now our ejb 3.0

// or @Local
// You can not put @Remote and @Local at the same time
public interface MyBean
void doSomething();

public class MyBeanStateless implements MyBean

public void doSomething()




No comments:

Post a Comment