Enterprise JavaBeans EJB Container Services
EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container manages the execution of enterprise beans for Java EE applications EJB benefit - container is charged with the task of making system services available to EJB components
J2EE Components and Container
EJB container services Container-provided services: Persistence Transactions Security JNDI Distribution Concurrency Multi-threading Component pooling Component life cycle Timer service Interceptors
Persistence Java Persistence public class Account private int id;... public class Customer private int id; private String private List accounts;... }
Transactions A transaction is a group of activities performed as a single unit Transaction demarcation types: Container-managed Declarative, default Bean-managed User transaction API Is applied to bean class Values: CONTAINER or BEAN
public class ShopBean implements Shop { public void setPrice(int prodId, int price){... public int getPrice(int prodId){... }
Transaction Attribute Definitions
public class ShopBean implements Shop UserTransaction EntityManager productMgr; public void setPrice(int prodId, int price){ tx.begin(); productMgr.find(Product.class, prodId).setPrice(price); tx.commit(); }
Security Security is very important in the enterprise environment Authentication Authorization @RunAs
public class HelloEJB implements Hello public String hello1(String msg) { return "1: Hello, " + msg; public String hello2(String msg) { return "2: Hello, " + msg; public String hello3(String msg) { return "3: Hello, " + msg; }
"B"}) public class HelloEJB implements Hello private SessionContext sc; public String hello(String msg) { if (sc.isCallerInRole("A") && !sc.isCallerInRole("B")){... } else {... }
JNDI JNDI = Java Naming and Directory Interface API that provides naming and directory functionality to applications store and retrieve named Java objects of any type associate attributes with objects and search for objects using their attributes A naming service helps organize an enterprise application by acting as a central registry for components
Naming and Directory services JNDI is independent of any specific directory service implementation LDAP, DNS, NIS, RMI, CORBA /jw /jw-02-howto.html /jw /jw-01-howto.html
JNDI functions void bind(String stringName, Object object) Binds a name to an object Object lookup(String stringName) Returns the specified object import javax.naming.Context; import javax.naming.InitialContext; Context ctx = new InitialContext(); Calculator calculatorRemote = (Calculator).lookup("CalculatorBean/remote");
JNDI architecture
Bean pooling To reduce memory consumption and processing, containers pool resources and manage the lifecycles of all the beans very carefully When a bean is not being used, a container will place it in a pool to be reused by another client The container copies data into or out of these pooled instances as necessary The best thing - client application is completely unaware of the resource management activities
Bean pooling benefits Reduces the resource requirements for a single server pooled beans are context switched as required Dynamic growth pool can be expanded/contracted as demand requires Pooled objects are instantiated on startup instead of every time may be expensive to instantiate Fine-grained control of resources able to set max/min beans
Bean lifecycle management Two strategies to perform lifecycle management: instance pooling passivation/activation Passivation is a technique to temporarily serialize a bean and store it to some persistent store Containers free up resources by passivating a bean and then re-activating it when resources are available
Stateless Session bean lifecycle Source: Sun J2EE tutorial
Stateful Session bean lifecycle Source: Sun J2EE tutorial
References Transactions Security J2EE/security_annotation/ JNDI