EJB Basics

 

VERSION 3.*

1 Interface (publishes methods to client ---how client accesses) + 1 Class (implmeents actual business logic...container invokes)

stateless session transaction

EJB - stateless session bean life

lifecyle continued

 

 

EJB Stateless Session bean transaction diagram

stateless session ejb transactions

How client access EJB..... the process:

  1. Client locates the EJB container through the JNDI service. (or you can know where bean is or it can be local)
  2. Client finds a reference to the EJB's interface accesses through its methods which the container in turn delegates to the bean itself.

FIRST --- VISIT SESSION BEAN for an example

Uses special keywords to denote method invokation as related to lifecyle

Construction/Destroy methods

@PostConstruct

PostConstruct callbacks occur after any dependency injection has been performed by the container and before the first business method invocation on the bean.

PostConstruct methods are invoked in an unspecified transaction context and security context.

@PreDestroy

PreDestroy callbacks occur at the time the bean instance is destroyed.

PreDestroy methods execute in an unspecified transaction and security context.

Stateless/Not Stateless declaration

do annotation prior to Bean class

@Stateless
public class BeanName implements InterfaceName

  

@Statefull
public class BeanName implements InterfaceName

 

Remote/Local access

Remote Specification

OPTION 1: preceed interface with keyword

@Remote
public interface InterfaceName { ... }

 

OPTION 2: preceed the bean class with @Remote, specifying the business interface or interfaces:

@Remote(InterfaceName.class)
public class BeanName implements InterfaceName { ... }

 

Local Declaration (DEFAULT ---do not have to declare) --- use this only when the EJB will ONLY be accessed by local webapps or other EJB.

OPTION 1: preceed interface with keyword

@Local public interface InterfaceName { ... }

OPTION 2: preceed the bean class with @Local, specifying the business interface or interfaces:

@Local(InterfaceName.class)  public class BeanName implements InterfaceName { ... }

EJB Passivation

container can put to sleep, "passivate", an EJB not in use.

Passivate ----Method called before passivation happens --tag with this

@PrePassivate

Activate ----Method called afteractivation happens --tag with this

@PostActivate

 

Many more annotation tags

  • for concurrency (@ConcurrencyManagement(CONTAINER) , @Lock(READ)....)

  • for accesstimeout ( @AccessTimeout(value=360000) )

  • to explicitly remove bean (@Remove)

  • indicate webservice endpoint (@WebService)

MORE......

See Java website or http://puzzle.mcs.csueastbay.edu/docs/javaee-tutorial/doc/gijrb.html for some examples

 

 

 

 

 

 

 

 

VERSION 2.* (< 3.*) DO NOT use this old version unless need to

2 Interfaces + 1Class

(Note: Entity beans also have a primary key class.)

(Note: Message-driven beans MDBs only have a class, no interfaces)

 

package: javax.ejb

Home Interface

Lists methods available for creating, removing and finding EJBs in the container.

  • container creates a home object from this interface at deployment time.
  • at runtime home object used by client in conjunction with a naming service to find the component and establish a connection to its component interface

public interface EnrollmentCartHome extends EJBHome {

EnrollmentCart create () throws CreateException,RemoteException;

}

 
Component Interface

defines the methods offered by the bean class.

  • container provides implementation of this interface by creating an object
  • client through the home object (see above) is given this componenet object and uses it to invoke methods on the bean.
  • component interface can be remote OR local.... remote when EJB client on different machine (typical) otherwise local (sometimes).

public interface EnrollmentCart extends EJBObject {

public void addCourses(String[] courseIDs) throws RemoteException;

public Collection getCourses() throws RemoteException;

public void empty() throws RemoteException;

}

 
Bean Class

has methods that implements business logic,etc.

  • accessed by client through component object (see above) not directly.

public class EnrollmentCartEJB implements javax.ejb.SessionBean {

private SessionContext ctx;
private HashSet cart;

/* callback methods */
public void setSessionContext(SessionContext ctx) {
      this.ctx = ctx; }

pubic void ejbCreate() throws CreateException {
      cart = new HasSet();    }

public void ejbActivate() {}

public void ejbPassivate() {}

public void ejbRemove() {}

/* implement all business methods as defined in the component interface*/
public void addCourses(String[] courseIds) {
   if(couseIds == null)
      return;
   for(int i=0; i<courseIDs.length; i++)
      cart.add(courseIds[i]);
}

public Collection getCourses() {
   return cart; }

public void empty() {
   cart.clear(); }

}

Note: You can create "local interfaces" for your EJB when you can garauntee your client is running in the same JVM as your EJB. It will improve performance as you do not need to use RMI. This first introduced in EJB 2.0 specification.

 

Remote versus Local Interfaces