Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3)
B14428-02
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
 
Next
Next
 

5 Using EJB 3.0 Session Bean API

This chapter describes the various options that you must configure in order to use an EJB 3.0 session bean.


Note:

In this release, OC4J supports a subset of the functionality specified in the EJB 3.0 public review draft. You may need to make code changes to your EJB 3.0 OC4J application after the EJB 3.0 specification is finalized and OC4J is updated to full EJB 3.0 compliance. For more information, see "Understanding EJB Support in OC4J".

There are no OC4J-proprietary EJB 3.0 annotations. For all OC4J-specific configuration, you must still use the EJB 2.1 orion-ejb-jar.xml file.


Table 5-1 lists these options and indicates which are basic (applicable to most applications) and which are advanced (applicable to more specialized applications).

For more information, see:

Table 5-1 Configurable Options for an EJB 3.0 Session Bean

Options Type

"Configuring Passivation"


Advanced

"Configuring Passivation Criteria"


Advanced

"Configuring Passivation Location"


Advanced

"Configuring Bean Instance Pool Size"


Basic

"Configuring Bean Instance Pool Timeouts for Session Beans"


Advanced

"Configuring a Transaction Timeout for a Session Bean"


Advanced

"Configuring a Lifecycle Callback Method for an EJB 3.0 Session Bean"


Basic

"Configuring an Interceptor on an EJB 3.0 Session Bean"


Advanced


Configuring Passivation

Passivation is an Oracle-specific option that you configure using the EJB 2.1 orion-ejb-jar.xml file.

For more information, see "Configuring Passivation".

Configuring Passivation Criteria

Passivation criteria is an Oracle-specific option that you configure using the EJB 2.1 orion-ejb-jar.xml file.

For more information, see "Configuring Passivation Criteria".

Configuring Passivation Location

Passivation location is an Oracle-specific option that you configure using the EJB 2.1 orion-ejb-jar.xml file.

For more information, see "Configuring Passivation Location".

Configuring a Lifecycle Callback Method for an EJB 3.0 Session Bean

You can specify an EJB 3.0 session bean class method as a callback method for any of the following lifecycle events (see "Using Annotations"):


Note:

Do not specify pre-passivate or post-activate lifecycle callback methods on a stateless session bean.

The session bean class lifecycle callback method must have the following signature:

public void <MethodName>()

For more information, see "Callback Methods".

Using Annotations

You can specify an EJB 3.0 session bean class method as a lifecycle callback method using any of the following annotations:

  • @PostConstruct

  • @PreDestroy

  • @PrePassivate (stateful session beans only)

  • @PostActivate (stateful session beans only)

Example 5-1 shows how to use the @PostConstruct annotation to specify EJB 3.0 stateful session bean class method initialize as a lifecycle callback method.

Example 5-1 @PostConstruct

@Stateful
public class CartBean implements Cart
{
    private ArrayList items;
 
    @PostConstruct
    public void initialize()
    {
        items = new ArrayList();
    }
...
}

Configuring an Interceptor on an EJB 3.0 Session Bean

You can designate one non-business method as the interceptor method for a stateless or stateful session bean (see "Using Annotations"). The method must have a signature of:

public Object <MethodName>(InvocationContext) throws Exception

For more information, see "Understanding EJB 3.0 Interceptors".

Using Annotations

Example 5-2 shows how to designate a method of a session bean class as an interceptor method using the @AroundInvoke annotation. Each time a client invokes a business method of this stateless session bean, OC4J intercepts the invocation and invokes the interceptor method myInterceptor. The client invocation proceeds only if the interceptor method returns InvocationContext.proceed().

Example 5-2 @AroundInvoke in an EJB 3.0 Session Bean

@Stateless
public class HelloWorldBean implements HelloWorld 
{
    public void sayHello() 
    {
        System.out.println("Hello!");
    }

    @AroundInvoke
    public Object myInterceptor(InvocationContext ctx) throws Exception 
    {
        Principal p = ctx.getEJBContext().getCallerPrincipal;
        if (!userIsValid(p))
        {
            throw new SecurityException(
                "Caller: '" + p.getName() +
                "' does not have permissions for method " + ctx.getMethod()
            );
        }
        return ctx.proceed();
    }
}