GlassFish Project - Enterprise JavaBeansTM (EJB) 3.0 Adapted Stateful Session Bean Example 

New to GlassFish | Community Guidelines | Downloads | FAQ | How-Tos
 

Overview

This is an example of a bean class implemented using the simplified EJB 3.0 API that exposes an Adapted EJB 2.x Home interface.

Click here to get the ZIP file with the complete example.

Contact us at ejb@glassfish.dev.java.net with any comments or questions.

EJB 2.x Home/Remote interfaces

In this case we want to expose the bean to clients written to the EJB 2.x remote programming model.  For that, we need a Home and Remote interface.  

public interface AdaptedHome extends EJBHome {

    public AdaptedRemote create(String id)
        throws CreateException, RemoteException;

}

public interface AdaptedRemote extends EJBObject {

    public String getId() throws RemoteException;

}

Stateful Session Bean Class

Here we have a bean class that uses the simplified EJB 3.0 API and annotations but exposes an EJB 2.x Remote view.

@Stateful
@RemoteHome(AdaptedHome.class)
public class AdaptedBean {

    private String myId = "unknown";

    @Init
    public void create(String id) {
        myId = id;
    }

    public String getId() {
        return myId;
    }

}

An EJB 2.x style Remote view is added via the @RemoteHome annotation. ( @LocalHome would be used for an EJB 2.x Local view. )   In addition, each create method in the 2.x Home interface must have a corresponding implementation in the bean class, although the method-name does not have to match.  Each of these methods is tagged with the @Init annotation.  

Note that the EJB 2.x Remote component interface counterpart to AdaptedHome (AdaptedRemote) does not appear in the implements clause of the AdaptedBean class.   In fact, it does not have to be declared explicitly in the bean class at all because it is derived from the signature of the create method declared in AdaptedHome.  

Deployment Descriptor

No deployment descriptor is needed. 

Application Client

Here's a Java EE Application Client that accesses the Adapted Stateful Session Bean using the @EJB annotation and dependency injection.   @EJB can handle the 2.x Home/LocalHome view in addition to EJB 3.0 business interfaces.

public class AdaptedAppClient {

    @EJB
    private static AdaptedHome adaptedHome;

    public static void main(String args[]) {

        try {

            AdaptedRemote adapted = adaptedHome.create("duke");
            System.out.println("Adapted id = " + adapted.getId());

        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}

Standalone Java Client

Here's an example of a plain Java client that runs outside of a Java EE container.

public class AdaptedJavaClient {

    public static void main(String args[]) {

        try {

            InitialContext ic = new InitialContext();

            Object o = ic.lookup(AdaptedHome.class.getName());
            AdaptedHome adaptedHome = (AdaptedHome)
                PortableRemoteObject.narrow(o, AdaptedHome.class);

            AdaptedRemote adapted = adaptedHome.create("duke");

            System.out.println("Adapted id = " + adapted.getId());

           
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

}

SUN App Server Specific Deployment Configuration

There is no need to define any SUN App Server-specific deployment descrpitors (e.g. sun-ejb-jar.xml, sun-application-client.xml) for this example.   The JNDI name for the Adapted Stateful Session bean will default to the fully-qualified class name of its Remote Home interface  : ejb30.AdaptedHome.    The Application Client's Remote Home @EJB dependency will be resolved to the same value since its type is also ejb30.AdaptedHome.  

Running the example


Use the following steps to build and execute the test.

  1. Set the glassfish.home property within build.xml.
  2. Set JAVA_HOME to point to a JDK 1.5 (or later) installation.
  3. ant build
  4. ant deploy
  5. ant run  

This will execute the Application Client and produce the following output :

Adapted id = duke

Other Targets