Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Communication
Software Acknowledgements: Bill Burke, JBoss Group, Tal Cohen, IBM Haifa
2010-2011
© The Authors
Contents
1. Introduction
– EJBs and the EJB container
– modelling enterprise applications
– types of EJB
– resource management with EJBs
2. EJB3 Session Beans
– stateful and stateless session bean examples
– call-back methods and the session bean life-cycle
– ENC and dependency injection
3. Transactions in EJBs
– CMT, BMT and the Java transaction APIs
– CMT transactional attributes
– session synchronisation, extended persistence context,
Communication transactional isolation
Software
2010-2011
4. Comparison EJB2 and EJB3
© The Authors
1
Introduction
• Advantages of EJBs:
– developer concentrates on the business logic and uses the
services provided by the container
• transactions, security, life-cycle management, multi-threading,
connection pooling, etc.
– components: reuse y abstraction
Communication
Software
2010-2011
– compatible with other Java APIs
© The Authors 3
© The Authors 4
2
EJB Container Services (2/5)
© The Authors 5
• Security:
– authentication
• validation of user’s identity
– authorisation (of access to components)
• security policy specifying what user can and cannot do
• declarative concept based on user “roles”:
• roles and their access rights to business methods defined with
annotations or in deployment descriptor XML file
• deployer assigns roles to users
– EJB server manages users and roles
– container deals with access control
– secure communications
• Concurrency (“multi-threading”)
– bean developer doesn’t explicitly code multithreading
– e.g. two ways of handling concurrent requests
Communication
Software • maintain instance pool & direct requests to bean instances from pool
2010-2011
• serialize requests to a single bean instance
© The Authors 6
3
EJB Container Services (4/5)
• Messaging:
– container provides access to messaging service
• JMS (Java Messaging Service) API
– asynchronous communication between 2 or more participants
• through a message-queueing system
– receivers of messages must be message-driven beans
• any enterprise bean can be an emitter of messages
Communication
Software
2010-2011
© The Authors 7
• Timer / scheduling
– schedule notifications to be sent to EJBs at specific times
– c.f. Unix cron
• Persistence (EJB2):
– EJB 2.1 entity beans can be used as alternative to JPA entities
• EJB3 container must support EJB2.1 entity beans (legacy beans)
– instances of entity beans in memory linked to business data
• container guarantees data consistency (periodic loading and storing)
– bean-managed persistence (BMP)
• bean developer codes DB access explicitly using JDBC but container
decides when to call this code
• bean instance uses JDBC connections provided by the container
– container-managed persistence (CMP)
• bean developer doesn’t code DB access explicitly
Communication • generally supports connection to relational databases
Software
2010-2011 • exact means of persistence depends on the container and is
© The Authors
independent of the bean 8
4
Modelling Enterprise Applications
Communication
Software
2010-2011
© The Authors 9
Business Entities
• Examples:
– client, order, account, employee,...
Communication
Software
2010-2011
© The Authors 10
5
Business Processes
© The Authors 11
Business Rules
• Examples:
– entity:
the balance of an account cannot be negative
(independent of the process that causes it to occur)
– process:
the max amount that can be withdrawn from a cashpoint is 500€
(independent of the state of the account entity)
Communication
Software
2010-2011
© The Authors 12
6
Enterprise JavaBeans
• Session Beans:
– processes executed in response to a client request (e.g. bank
transactions, calculations, implementation of orders,…)
– collaborative process: make use of JPA entities / entity beans
– receive synchronous calls to methods defined on business interface
• Message-Driven Beans:
Communication
– processes executed as a response to the reception of a message
Software
2010-2011 – receive asynchronous calls via a channel
© The Authors 13
© The Authors 14
7
Message-driven Beans
© The Authors 15
Session Beans
8
Resource Management in Stateless Session
Beans: Instance Pooling / Swapping
Communication
Software
2010-2011
© The Authors 18
9
Resource Management in “Stateful”
Session Beans: Activation & Passivation
• “Passivation”:
– dissociation of bean class proxy from bean instance
– serialization of instance state to secondary storage
• Activation:
Communication – deserialization of instance state from secondary storage
Software
2010-2011 – restoring of association to bean class proxy
© The Authors 19
Contents
1. Introduction
– EJBs and the EJB container
– modelling enterprise applications
– types of EJB
– resource management with EJBs
2. EJB3 Session Beans
– stateful and stateless session bean examples
– call-back methods and the session bean life-cycle
– ENC and dependency injection
3. Transactions in EJBs
– CMT, BMT and the Java transaction APIs
– CMT transactional attributes
– session synchronisation, extended persistence context,
Communication transactional isolation
Software
2010-2011
4. Comparison EJB2 and EJB3
© The Authors
10
Session beans, client view
© The Authors 21
package examples.session.stateless;
package examples.session.stateless;
import javax.ejb.Remote;
import javax.ejb.Stateless;
11
Example 1: EJB3 Stateless Session Bean v2
package examples.session.stateless;
package examples.session.stateless;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Stateless;
12
Example 1: EJB3 Session Bean Client
import javax.naming.context;
import javax.naming.InitialContext;
System.out.println(hello.hello());
}
}
Communication
Software
2010-2011 Source of example 1: Mastering Enterprise JavaBeans 3.0
© The Authors 25
@Remove
public void checkout() {
… }
}
Communication
Software
2010-2011
© The Authors 26
13
Session Bean Life-Cycle Call-Back Methods
Communication
Software
2010-2011
© The Authors 28
14
Session Bean Life-Cycle
© The Authors
© The Authors
15
Stateful Session Bean Life-Cycle
© The Authors 32
16
The JNDI Enterprise Naming Context (1/2)
© The Authors 33
Communication
Software
2010-2011
© The Authors 34
17
Example 3: Dependency Injection
@EJB(name="CreditProcessorEJB")
private CreditCardProcessor processor;
"
private DataSource jdbc;
@Resource(name="java:/DefaultDS")
public void setDataSource(DataSource db) {
this.jdbc = db;
Communication }
Software
2010-2011 }
© The Authors 35
Contents
1. Introduction
– EJBs and the EJB container
– modelling enterprise applications
– types of EJB
– resource management with EJBs
2. EJB3 Session Beans
– stateful and stateless session bean examples
– call-back methods and the session bean life-cycle
– ENC and dependency injection
3. Transactions in EJBs
– CMT, BMT and the Java transaction APIs
– CMT transactional attributes
– session synchronisation, extended persistence context,
Communication transactional isolation
Software
2010-2011
4. Comparison EJB2 and EJB3
© The Authors
18
Transaction Management in EJBs
• Transaction:
– set of tasks to be executed atomically
• if one or more task fails: rollback
• if all tasks are successful: commit
– ACID properties
• Atomicity, Consistency, Isolation, Durability
• Easy to use
• Transactional behaviour
– independent of business logic
• Uses transactional attributes to control propagation
– declared as annotations or in deployment descriptor
– associated with each EJB method
• Container uses JTS API to automatically manage
– transaction start and finish
– interaction with DB
– creation & propagation of context during transaction
Communication
Software
2010-2011
© The Authors 38
19
Bean-Managed Transactions (BMT)
• Difficult to use
– but finer control
• Transactional behaviour
– may depend on business logic
• Explicit use of
– Java Transaction API (JTA)
– JDBC, if needed
• e.g. session bean wrapping legacy code
© The Authors 39
© The Authors
20
CMT: interface and methods
Communication
Software
2010-2011
© The Authors
21
Example 4: BMT
[...]
InitialContext cxt = new InitialContext();
userTx = (javax.transaction.UserTransaction)
cxt.lookup("java:comp/UserTransaction");
try{
userTx.begin();
beanA.setX(1);
beanA.setName("one");
beanB.setY(2);
beanB.setName("two");
userTx.commit();
} catch(Exception e){
try{
System.out.println(e.getMessage());
userTx.rollback();
} catch(Exception ex){}
}
Communication [...]
Software
2010-2011
© The Authors
Definitions
• Transactional context
– defines transactional scope and participant objects & operations
– by default, propagates between transactional objects such as EJBs
• Transactional objects
– objects whose methods are invoked in transaction
– may only be associated with one transaction at a time
• Transactional attributes
– per-method spec of transaction management (by container) on
invocation
• Transactional client
– agent that invokes methods on transactional objects
22
CMT: Transactional Attributes (1/4)
© The Authors
23
CMT: Transactional Attributes (3/4)
• Supports:
– if invoker has transaction context, it is propagated to bean
– otherwise, no transaction context used
– use for "don't care" situations (use “don’t care” carefully!
varying transactional behaviour can be tricky)
• e.g. method carrying out a single update operation
• NotSupported:
– if invoker has transaction context, it is suspended for the
duration of this method's execution
– otherwise, no transaction context used
– in all cases, method executes without transaction context
– example use: resource managers that do not propagate the
Communication transaction
Software
2010-2011
© The Authors
• Mandatory:
– if invoker has a transaction context, it is propagated to bean
– otherwise, exception is thrown
• TransactionRequiredException
• TransactionRequiredLocalException
– use when invoker is to provide the transaction
• does not necessarily imply BMT or client-managed
transactions: invoker can be different method in same EJB
• Never:
– if invoker has transaction context, an exception is thrown
• RemoteException or EJBException
– otherwise, method proceeds normally, without a context
– used for non-transactional resources
Communication
Software
2010-2011
© The Authors
24
CMT: Transactional Attributes, Summary
Supports T T
None None
NotSupported T None
None None
Mandatory T T
None Exception
Communication
Never T Exception
Software
2010-2011
None None
© The Authors
25
Transaction Attributes, Entities and MDB
• Message-driven beans
Communication – CMT: only use NotSupported or Required
Software
2010-2011 • since other types apply to client-initiated transactions (no client!)
© The Authors
© The Authors
26
Stateful Session Beans
and Extended Persistence Context
Transactional Isolation
© The Authors
27
Contents
1. Introduction
– EJBs and the EJB container
– modelling enterprise applications
– types of EJB
– resource management with EJBs
2. EJB3 Session Beans
– stateful and stateless session bean examples
– call-back methods and the session bean life-cycle
– ENC and dependency injection
3. Transactions in EJBs
– CMT, BMT and the Java transaction APIs
– CMT transactional attributes
– session synchronisation, extended persistence context,
Communication transactional isolation
Software
2010-2011
4. Comparison EJB2 and EJB3
© The Authors
28
Structure of EJB 2 (2/2)
Communication
Software
2010-2011
© The Authors 57
• Remote interfaces
– home interface (container generates one object per class)
• create and delete session and entity beans, find entity beans
• extends javax.ejb.EJBHome
– remote business interface (container generates proxy object)
• export business methods (remotely)
• extends javax.ejb.EJBObject
• Local interfaces
– local home Interface (container generates 1 object per class)
• create and delete session and entity beans, find entity beans
• extends javax.ejb.EJBLocalHome
– local business interface (container generates proxy object)
• export business methods (locally)
Communication
Software
• extends javax.ejb.EJBLocalObject
2010-2011
© The Authors 58
29
Example 5: EJB 2 Stateless Session Bean
extends
implements
Deployment Descriptor
name=HelloWorldEJB
class=HelloBean
Communication home=HelloHome
Software Type=Session
2010-2011
Compare with example 1 Transaction=Container
© The Authors
… 59
import java.rmi.RemoteException;
Communication
Software
}
2010-2011
© The Authors 60
30
Example 5: EJB 2 Local Interfaces
import javax.ejb.CreateException;
Communication }
Software
2010-2011
© The Authors 61
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
31
Example 5: EJB 2 Deployment Descriptor
<ejb-jar
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
<enterprise-beans>
<session>
<ejb-name>HelloWorldEJB</ejb-name>
<home>examples.ejb21.HelloHome</home>
<remote>examples.ejb21.Hello</remote>
<local-home>examples.ejb21.HelloLocalHome</local-home>
<local>examples.ejb21.HelloLocal</local>
<ejb-class>examples.ejb21.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-bean>
Communication </ejb-jar>
Software
2010-2011
© The Authors 63
import javax.naming.context;
import javax.naming.InitialContext;
32
EJB2 & EJB3, summary of differences (1/3)
EJB2 EJB3
Business interface Business interface
• must extend special interface • just a normal Java interface (POJI)
• connection with bean class made in • connection with bean class made with
deployment descriptor Java implements keyword (POJI)
• local and/or remote interfaces • one single interface with @Remote and/or
@Local annotation
EJB2 EJB3
XML deployment descriptor XML deployment descriptor
• mandatory for metadata description • Java5 annotations can be used instead
• cons: complex and verbose • pros: annotations much simpler
• pros: all appl. metadata in one file • pros: can also use deployment descriptor
Testability Testability
• EJBs (including entity beans) not • EJBs & JPA entities both testable outside
testable outside the container the container (beans are POJOs)
Communication
Software
2010-2011
© The Authors
33
EJB2 & EJB3, summary of differences (3/3)
EJB2 EJB3
Persistence Persistence
• BMP or CMP entity bean • JPA entities (POJOs) + EntityManager
Communication
Software
2010-2011
Source: Mastering Enterprise JavaBeans 3.0, Patel et al. 68
© The Authors
34
EJB 3 Programming Model
Communication
Software
2010-2011
© The Authors
Source: Mastering Enterprise JavaBeans 3.0, Patel et al. 69
35
Bibliography for Sections 1- 3
• EJB 3 in Action. Debu Panda, Reza Rahman, Derek Lane. Manning 2007
• Enterprise JavaBeans Technology. Sun Microsystems.
http://java.sun.com/products/ejb/
• JSR 220: Enterprise JavaBeans 3.0. The Java Community Process 2006.
http://jcp.org/en/jsr/detail?id=220
© The Authors
Communication
Software
2010-2011
© The Authors
36