Enterprise Java Beans EJB Container Services EJB container
Enterprise Java. Beans EJB Container Services
EJB container • Enterprise Java. Beans 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
J 2 EE Components and Container http: //java. sun. com/blueprints/guidelines/designing_enterprise_applications/platform_technologies/component/index. html
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 API @Entity public class Customer { @Id private int id; public class Account { private String name; @Id @One. To. Many private int id; private List<Account>accounts; . . . } @Entity }
Transactions 1. A transaction is a group of activities performed as a single unit 2. Transaction demarcation types: 1. Container-managed 1. Declarative, default 2. Bean-managed 1. User transaction API 3. Annotation @Transaction. Management 1. Is applied to bean class 2. Values: CONTAINER or BEAN
Example: Container-managed @Transaction. Attribute(MANDATORY) @Stateless public class Shop. Bean implements Shop { public void set. Price(int prod. Id, int price){. . . } @Transaction. Attribute(REQUIRED) public int get. Price(int prod. Id){. . . } }
Transaction Attribute Definitions http: //java. sys-con. com/read/325149. htm
Example: Bean-managed @Transaction. Management(BEAN) @Stateless public class Shop. Bean implements Shop { @Resource User. Transaction tx; @Persistence. Context Entity. Manager product. Mgr; public void set. Price(int prod. Id, int price){ tx. begin(); product. Mgr. find(Product. class, prod. Id). set. Price(price); tx. commit(); } }
Security • Security is very important in the enterprise environment • • Authentication Authorization • Security annotations • • • @Declare. Roles @Deny. All @Permit. All @Roles. Allowed @Run. As
Example: Security @Stateless @Declare. Roles({“javaee"}) public class Hello. EJB implements Hello { @Permit. All public String hello 1(String msg) { return "1: Hello, " + msg; } @Roles. Allowed("javaee") public String hello 2(String msg) { return "2: Hello, " + msg; } @Deny. All public String hello 3(String msg) { return "3: Hello, " + msg; } }
Example: Security @Stateless @Declare. Roles({"A", "B"}) public class Hello. EJB implements Hello { @Resource private Session. Context sc; public String hello(String msg) { if (sc. is. Caller. In. Role("A") && !sc. is. Caller. In. Role("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 http: //www. javaworld. com/javaworld /jw-01 -2000/jw-01 -howto. html http: //www. javaworld. com/javaworld /jw-02 -2000/jw-02 -howto. html • JNDI is independent of any specific directory service implementation • LDAP, DNS, NIS, RMI, CORBA
JNDI functions • void bind(String string. Name, Object object) • Binds a name to an object • Object lookup(String string. Name) • Returns the specified object import javax. naming. Context; import javax. naming. Initial. Context; Context ctx = new Initial. Context(); Calculator calculator. Remote = (Calculator). lookup("Calculator. Bean/remote");
JNDI architecture http: //java. sun. com/products/jndi/tutorial/get. Started/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 J 2 EE tutorial
Stateful Session bean lifecycle Source: Sun J 2 EE tutorial
References • Transactions http: //java. sys- con. com/read/325149. htm • Security http: //java. sun. com/developer/technical. Articles/ J 2 EE/security_annotation/ • JNDI http: //www. java 2 s. com/Article/Java/J 2 EE/JNDI. htm
- Slides: 22