Top Banner
Enterprise JavaBeans EJB Container Services
22

Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Dec 28, 2015

Download

Documents

Myra Clark
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Enterprise JavaBeans

EJB Container Services

Page 2: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 3: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

J2EE Components and Container

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications/platform_technologies/component/index.html

Page 4: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

EJB container servicesContainer-provided services:

• Persistence• Transactions• Security• JNDI• Distribution

• Concurrency• Multi-threading• Component pooling• Component life cycle• Timer service• Interceptors

Page 5: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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;

. . .

}

Page 6: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 7: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Example: Container-managed@TransactionAttribute(MANDATORY)@Statelesspublic class ShopBean implements Shop {

public void setPrice(int prodId, int price){...}

@TransactionAttribute(REQUIRED)public int getPrice(int prodId){...}

}

Page 8: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Transaction Attribute Definitions

http://java.sys-con.com/read/325149.htm

Page 9: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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();

}}

Page 10: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Security• Security is very important in the enterprise

environment• Authentication• Authorization

• Security annotations• @DeclareRoles • @DenyAll • @PermitAll • @RolesAllowed • @RunAs

Page 11: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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;}

}

Page 12: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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 {

...

}

}

}

Page 13: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 14: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 15: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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");

Page 16: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

JNDI architecture

http://java.sun.com/products/jndi/tutorial/getStarted/overview/index.html

Page 17: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 18: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 19: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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

Page 20: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Stateless Session bean lifecycle

Source: Sun J2EE tutorial

Page 21: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

Stateful Session bean lifecycle

Source: Sun J2EE tutorial

Page 22: Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.

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