3 Simple Enterprise JavaBeans 3.0 Examples

The following sections describe simple Java examples of EJBs that use the new metadata annotation programming model:

Later procedural sections of this guide that describe how to program an EJB make reference to these examples.

Example of a Simple Stateless EJB

The following code shows a simple business interface for the ServiceBean stateless session EJB:

package examples;
/**
* Business interface of the Service stateless session EJB
*/
public interface Service {
  public void sayHelloFromServiceBean();
}

The code shows that the Service business interface has one method, sayHelloFromServiceBean(), that takes no parameters and returns void.

The following code shows the bean file that implements the preceding Service interface; the code in bold is described after the example:

package examples;
import static javax.ejb.TransactionAttributeType.*;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.interceptor.ExcludeDefaultInterceptors;
/**
 * Bean file that implements the Service business interface.
 * Class uses following EJB 3.0 annotations:
 *   - @Stateless - specifies that the EJB is of type stateless session
 *   - @TransactionAttribute - specifies that the EJB never runs in a
 *       transaction
 *   - @ExcludeDefaultInterceptors - specifies any configured default
 *      interceptors should not be invoked for this class
 */
@Stateless
@TransactionAttribute(NEVER)
@ExcludeDefaultInterceptors
public class ServiceBean
  implements Service
{
  public void sayHelloFromServiceBean() {
    System.out.println("Hello From Service Bean!");
  }
}

The main points to note about the preceding code are:

  • Use standard import statements to import the metadata annotations you use in the bean file:

    import static javax.ejb.TransactionAttributeType.*;
    import javax.ejb.Stateless;
    import javax.ejb.TransactionAttribute;
    import javax.interceptor.ExcludeDefaultInterceptors
    

    The annotations that apply only to EJB 3.0 are in the javax.ejb package. Annotations that can be used by other Java Platform, Enterprise Edition (Java EE) Version 5 components are in more generic packages, such javax.interceptor or javax.annotation.

  • The ServiceBean bean file is a plain Java file that implements the Service business interface; it is not required to implement any EJB-specific interface. This means that the bean file does not need to implement the lifecycle methods, such as ejbCreate and ejbPassivate, that were required in the 2.X programming model.

  • The class-level @Stateless metadata annotation specifies that the EJB is of type stateless session.

  • The class-level @TransactionAttribute(NEVER) annotation specifies that the EJB never runs inside of a transaction.

  • The class-level @ExcludeDefaultInterceptors annotation specifies that default interceptors, if any are defined in the ejb-jar.xml deployment descriptor file, should never be invoked for any method invocation of this particular EJB.

Example of a Simple Stateful EJB

The following code shows a simple business interface for the AccountBean stateful session EJB:

package examples;
/**
 * Business interface for the Account stateful session EJB.
 */
public interface Account {
  public void deposit(int amount);
  public void withdraw(int amount);
  public void sayHelloFromAccountBean();
}

The code shows that the Account business interface has three methods, deposit, withdraw, and sayHelloFromAccountBean.

The following code shows the bean file that implements the preceding Account interface; the code in bold is described after the example:

package examples;
import static javax.ejb.TransactionAttributeType.*;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.Remote;
import javax.ejb.EJB;
import javax.annotation.PreDestroy;
import javax.interceptor.Interceptors;
import javax.interceptor.ExcludeClassInterceptors;
import javax.interceptor.InvocationContext;
/**
 * Bean file that implements the Account business interface.
 * Uses the following EJB annotations:
 *    -  @Stateful: specifies that this is a stateful session EJB
 *    -  @TransactionAttribute - specifies that this EJB never runs
 *         in a transaction
 *    -  @Remote - specifies the Remote interface for this EJB
 *    -  @EJB - specifies a dependency on the ServiceBean stateless
 *         session ejb
 *    -  @Interceptors - Specifies that the bean file is associated with an
 *         Interceptor class; by default all business methods invoke the
 *         method in the interceptor class annotated with @AroundInvoke.
 *    -  @ExcludeClassInterceptors - Specifies that the interceptor methods
 *         defined for the bean class should NOT fire for the annotated
 *         method.
 *    -  @PreDestroy - Specifies lifecycle method that is invoked when the
 *         bean is about to be destoryed by EJB container.
 *
 */
@Stateful
@TransactionAttribute(NEVER)
@Remote({examples.Account.class})
@Interceptors({examples.AuditInterceptor.class})
public class AccountBean
 implements Account
{
  private int balance = 0;
  @EJB(beanName="ServiceBean")
  private Service service;
  public void deposit(int amount) {
    balance += amount;
    System.out.println("deposited: "+amount+" balance: "+balance);
  }
  public void withdraw(int amount) {
    balance -= amount;
    System.out.println("withdrew: "+amount+" balance: "+balance);
  }
  @ExcludeClassInterceptors
  public void sayHelloFromAccountBean() {
    service.sayHelloFromServiceBean();
  }
  @PreDestroy
  public void preDestroy() {
   System.out.println("Invoking method: preDestroy()");  
  }
}

The main points to note about the preceding code are:

  • Use standard import statements to import the metadata annotations you use in the bean file:

    import static javax.ejb.TransactionAttributeType.*;
    import javax.ejb.Stateful;
    import javax.ejb.TransactionAttribute;
    import javax.ejb.Remote;
    import javax.ejb.EJB;
    import javax.annotation.PreDestroy;
    import javax.interceptor.Interceptors;
    import javax.interceptor.ExcludeClassInterceptors;
    

    The annotations that apply only to EJB 3.0 are in the javax.ejb package. Annotations that can be used by other Java Platform, Enterprise Edition (Java EE) Version 5 components are in more generic packages, such javax.interceptor or javax.annotation.

  • Import the InvocationContext class, used to maintain state between interceptors:

    import javax.interceptor.InvocationContext;
    
  • The AccountBean bean file is a plain Java file that implements the Account business interface; it is not required to implement any EJB-specific interface. This means that the bean file does not need to implement the lifecycle methods, such as ejbCreate and ejbPassivate, that were required in the 2.X programming model.

  • The class-level @Stateful metadata annotation specifies that the EJB is of type stateful session.

  • The class-level @TransactionAttribute(NEVER) annotation specifies that the EJB never runs inside of a transaction.

  • The class-level @Remote annotation specifies the name of the remote interface of the EJB; in this case it is the same as the business interface, Account.

  • The class-level @Interceptors({examples.AuditInterceptor.class}) annotation specifies the interceptor class that is associated with the bean file. This class typically includes a business method interceptor method, as well as lifecycle callback interceptor methods. See Example of an Interceptor Class for details about this class.

  • The field-level @EJB annotation specifies that the annotated variable, service, is injected with the dependent ServiceBean stateless session bean context. The data type of the injected field, Service, is the business interface of the ServiceBean EJB. The following code in the sayHelloFromAccountBean method shows how to invoke the sayHelloFromServiceBean method of the dependent ServiceBean:

    service.sayHelloFromServiceBean();
    
  • The method-level @ExcludeClassInterceptors annotation specifies that the @AroundInvoke method specified in the associated interceptor class (AuditInterceptor) should not be invoked for the sayHelloFromAccountBean method.

  • The method-level @PreDestroy annotation specifies that the EJB container should invoke the preDestroy method before the container destroys an instance of the AccountBean. This shows how you can specify interceptor methods (for both business methods and lifecycle callbacks) in the bean file itself, in addition to using an associated interceptor class.

Example of an Interceptor Class

The following code shows an example of an interceptor class, specifically the AuditInterceptor class that is referenced by the preceding AccountBean stateful session bean with the @Interceptors({examples.AuditInterceptor.class}) annotation; the code in bold is described after the example:

package examples;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
/**
 * Interceptor class.  The interceptor method is annotated with the
 *  @AroundInvoke annotation.
 */
public class AuditInterceptor {
  public AuditInterceptor() {}
  @AroundInvoke
  public Object audit(InvocationContext ic) throws Exception {
    System.out.println("Invoking method: "+ic.getMethod());
    return ic.proceed();
  }
  @PostActivate
  public void postActivate(InvocationContext ic) {
    System.out.println("Invoking method: "+ic.getMethod());
  }
  @PrePassivate
  public void prePassivate(InvocationContext ic) {
    System.out.println("Invoking method: "+ic.getMethod());
  }
}

The main points to notice about the preceding example are:

  • As usual, import the metadata annotations used in the file:

    import javax.interceptor.AroundInvoke;
    import javax.interceptor.InvocationContext;
    import javax.ejb.PostActivate;
    import javax.ejb.PrePassivate;
    
  • The interceptor class is plain Java class.

  • The class has an empty constructor:

    public AuditInterceptor() {}
    
  • The method-level @AroundInvoke specifies the business method interceptor method. You can use this annotation only once in an interceptor class.

  • The method-level @PostActivate and @PrePassivate annotations specify the methods that the EJB container should call after reactivating and before passivating the bean, respectively.

    Note:

    These lifecycle callback interceptor methods apply only to stateful session beans.

Example of Invoking a 3.0 Entity From A Session Bean

For an example of invoking an entity from a session bean, see the EJB 3.0 example in the distribution kit. After you have installed WebLogic Server, the example is in the following directory:

WL_HOME/samples/server/examples/src/examples/ejb/ejb30

WL_HOME refers to the directory in which you installed WebLogic Server, such as /Oracle/Middleware/wlserver_10.3.