Download presentation
Presentation is loading. Please wait.
Published byMyra Clark Modified over 9 years ago
1
Enterprise JavaBeans EJB Container Services
2
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
3
J2EE Components and Container http://java.sun.com/blueprints/guidelines/designing_enterprise_applications/platform_technologies/component/index.html
4
EJB container services Container-provided services: Persistence Transactions Security JNDI Distribution Concurrency Multi-threading Component pooling Component life cycle Timer service Interceptors
5
Persistence Java Persistence API @Entity public class Account { @Id private int id;... } @Entity public class Customer { @Id private int id; private String name; @OneToMany private List accounts;... }
6
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
7
Example: Container-managed @TransactionAttribute(MANDATORY) @Stateless public class ShopBean implements Shop { public void setPrice(int prodId, int price){... } @TransactionAttribute(REQUIRED) public int getPrice(int prodId){... }
8
Transaction Attribute Definitions http://java.sys-con.com/read/325149.htm
9
Example: Bean-managed @TransactionManagement(BEAN) @Stateless public 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(); }
10
Security Security is very important in the enterprise environment Authentication Authorization Security annotations @DeclareRoles @DenyAll @PermitAll @RolesAllowed @RunAs
11
Example: Security @Stateless @DeclareRoles({“javaee"}) public class HelloEJB implements Hello { @PermitAll public String hello1(String msg) { return "1: Hello, " + msg; } @RolesAllowed("javaee") public String hello2(String msg) { return "2: Hello, " + msg; } @DenyAll public String hello3(String msg) { return "3: Hello, " + msg; }
12
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 {... }
13
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
14
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
15
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");
16
JNDI architecture http://java.sun.com/products/jndi/tutorial/getStarted/overview/index.html
17
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
18
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
19
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
20
Stateless Session bean lifecycle Source: Sun J2EE tutorial
21
Stateful Session bean lifecycle Source: Sun J2EE tutorial
22
References Transactions http://java.sys-con.com/read/325149.htm http://java.sys-con.com/read/325149.htm Security http://java.sun.com/developer/technicalArticles/ J2EE/security_annotation/ JNDI http://www.java2s.com/Article/Java/J2EE/JNDI.htm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.