Enterprise JavaBeans EJB Container Services
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
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications/platform_technologies/component/index.html
EJB container servicesContainer-provided services:
• Persistence• Transactions• Security• JNDI• Distribution
• Concurrency• Multi-threading• Component pooling• Component life cycle• Timer service• Interceptors
Persistence
Java Persistence APIJava Persistence API
@Entity
public class Account {
@Id
private int id;
. . .
}
@Entity
public class Customer {
@Id private int id;
private String name;
@OneToMany
private List<Account>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
• Annotation @TransactionManagement• Is applied to bean class• Values: CONTAINER or BEAN
Example: Container-managed@TransactionAttribute(MANDATORY)@Statelesspublic class ShopBean implements Shop {
public void setPrice(int prodId, int price){...}
@TransactionAttribute(REQUIRED)public int getPrice(int prodId){...}
}
Transaction Attribute Definitions
http://java.sys-con.com/read/325149.htm
Example: Bean-managed@TransactionManagement(BEAN)@Statelesspublic class ShopBean implements Shop {
@Resource UserTransaction tx;@PersistenceContext 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
• Security annotations• @DeclareRoles • @DenyAll • @PermitAll • @RolesAllowed • @RunAs
Example: Security@Stateless@DeclareRoles({“javaee"}) public class HelloEJB implements Hello {
@PermitAllpublic String hello1(String msg) {
return "1: Hello, " + msg;}
@RolesAllowed("javaee")public String hello2(String msg) {
return "2: Hello, " + msg;}
@DenyAllpublic String hello3(String msg) {
return "3: Hello, " + msg;}
}
Example: Security@Stateless
@DeclareRoles({"A", "B"})
public class HelloEJB implements Hello {
@Resource 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
http://www.javaworld.com/javaworld/jw-02-2000/jw-02-howto.html
http://www.javaworld.com/javaworld/jw-01-2000/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
http://java.sun.com/products/jndi/tutorial/getStarted/overview/index.html
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
http://java.sys-con.com/read/325149.htm
• Securityhttp://java.sun.com/developer/technicalArticles/J2EE/security_annotation/
• JNDIhttp://www.java2s.com/Article/Java/J2EE/JNDI.htm