Skip Headers
Oracle® Fusion Middleware Programming Enterprise JavaBeans, Version 3.0, for Oracle WebLogic Server
11g Release 1 (10.3.6)

Part Number E13720-08
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

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:

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:

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:

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.