Download Stateful Session Beans - BFH-TI / Organisation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Berner Fachhochschule
Technik und Informatik
Stateful
Session Beans
Course Multi Tier Business Applications with Java EE
Prof. Dr. Eric Dubuis
Berner Fachhochschule
Biel
Content
●
●
●
●
●
●
●
April 14, 2011
Characteristics of stateful session beans
Business interface, bean implementation
Bean's life cycle
Conversational state
Activation and passivation
Callback annotations
Client view
MTA with Java EE / Stateful Session Beans
2
Characteristics of a Stateful Session Beans
Executes on behalf of a single client.
●
A component that preserves state information between method calls
(conversational state).
●
Can be transaction-aware.
●
Does not represent directly shared data in the database, although it may
access and update such data.
●
Is relatively short-lived.
●
Can be viewed as an extension of application clients that hold (client)
state information.
A typical EJB container provides a scalable runtime environment to execute
a large number of (stateful and/or stateless) session objects
concurrently.
But:
●
Pooling is not as simple as with stateless session beans (container
issue)
●
activation/passivation (containers decide on strategy; LRU used most
often)
●
April 14, 2011
MTA with Java EE / Stateful Session Beans
3
What are Stateful Session Beans Used For?
●
●
To encapsulate some of the business logic and conversional state, and
to combine these in a workflow.
To present a simplified interface to clients.
For example, a shopping cart
April 14, 2011
MTA with Java EE / Stateful Session Beans
4
A Stateful Session Bean Example: The Business Interface
●
To illustrate the mechanism of a stateful session bean, we define an
interface for a remote calculator engine. Notice that the engine can
memorize a value:
/**
* Defines a calculator component with a memory register.
*/
// @Remote (omitted here to be technology-independent)
public interface Calculator {
public double add(double x, double y);
public double subtract(double x, double y);
public double multiply(double x, double y);
public double divide(double x, double y);
public void memoryAdd(double x);
public void memorySubtract(double x);
public double memoryRead();
public void memoryClear();
public void close();
}
Remarks:
●
Methods of the form memoryXx() manipulate a memory register located
in the stateful session bean.
●
Method close() tells the bean when the client is done with it.
April 14, 2011
MTA with Java EE / Stateful Session Beans
5
The Stateful Calculator Bean Class I
●
The implementation of the preceding interface is straightforward. EJB3
technology comes in with the help of EJB annotations (in bold).
import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateful;
Implementation of
interface Calculator:
not mandatory, but
recommended
@Stateful
@Remote(value = { Calculator.class })
public class CalculatorBean implements Calculator {
private double memoryCell = 0.0;
public double add(double x, double y) { return x + y; }
// Similar for subtract(), multiply(), and divide().
// ... to be continued
April 14, 2011
MTA with Java EE / Stateful Session Beans
6
The Stateful Calculator Bean Class
●
You add the business methods. Some of them manipulate the bean's
memory cell:
// Methods that manipulate the memory cell:
public void memoryAdd(double x) {
this.memoryCell += x;
}
public void memorySubtract(double x) {
this.memoryCell -= x;
}
public double memoryRead() { return this.memoryCell; }
public void memoryClear() { this.memoryCell = 0.0; }
// Method for releasing the bean:
@Remove
public void close() {
// Nothing here.
}
} // end of CalculatorBean
April 14, 2011
MTA with Java EE / Stateful Session Beans
7
Remarks
●
●
The @Stateful annotation tells the EJB container that CalculatorBean is
a stateful session bean.
The @Remote annotation designates the remote interface of the bean.
–
–
–
●
●
Must be added if corresponding interface lacks @Remote annotation.
The value element must be specified if the annotation is applied to the
bean class.
Must be used if bean class implements more than on remote interface.
The instance field memoryCell denotes the conversational state of the
bean.
Since EJB 3.1, JNDI name space syntax is standardized [EJB3.1, 4.4]:
–
–
–
April 14, 2011
portable session bean global JNDI name:
java:global[/<app-name>]/<module-name>/<bean-name>
[!<fully-qualified-interface-name>]
application-specific name space within a Java EE application:
java:app/<module-name>/<bean-name>
[!<fully-qualified-interface-name>]
module-specific name space within a Java EE application:
java:module/<bean-name>[!<fully-qualified-interface-name>
MTA with Java EE / Stateful Session Beans
8
Stateful Session Bean Life Cycle [EJB3.1, 4.6]
April 14, 2011
MTA with Java EE / Stateful Session Beans
9
Conversational State
Definition
"The conversational state of a stateful session object is defined as the
session bean instance’s field values, its associated interceptors and their
instance field values, plus the transitive closure of the objects from these
instances’ fields reached by following Java object references."
[EJB3.1, 4.2]
April 14, 2011
MTA with Java EE / Stateful Session Beans
10
Passivation/Activation
Definition
"To efficiently manage the size of its working set, a session bean container
may need to temporarily transfer the state of an idle stateful session
bean instance to some form of secondary storage. The transfer from the
working set to secondary storage is called instance passivation. The
transfer back is called activation." [EJB3.1, 4.2]
April 14, 2011
MTA with Java EE / Stateful Session Beans
11
Bean Provider Responsibilities Regarding Conversational
State
Ensure that the @PrePassivate method leaves the instance fields ready to
be serialized by the container. The objects that are assigned to the
instance’s non-transient fields after the @PrePassivate method
completes must be one of the following:
●
A serializable object.
●
A null.
●
A reference to an enterprise bean’s business interface.
●
A reference to an enterprise bean’s remote interface, even if the stub
class is not serializable.
●
A reference to an enterprise bean’s remote home interface, even if the
stub class is not serializable.
●
A reference to an entity bean’s local interface, even if it is not
serializable.
●
A reference to an entity bean’s local home interface, even if it is not
serializable.
April 14, 2011
MTA with Java EE / Stateful Session Beans
12
Non-Transient Fields Continued
●
●
●
●
●
●
●
●
A reference to the @SessionContext object, even if it is not serializable.
A reference to the environment naming context (that is, the
java:comp/env JNDI context) or any of its sub contexts.
A reference to the UserTransaction interface.
A reference to a resource manager connection factory.
A reference to an EntityManager object, even if it is not serializable.
A reference to an EntityManagerFactory object, even if it is not
serializable.
A reference to a javax.ejb.Timer object.
An object that is not directly serializable, but becomes serializable by
replacing the references to an enterprise bean’s business interface, an
enterprise bean’s home and component interfaces, the references to the
SessionContext object, the references to the java:comp/env JNDI
context and its sub contexts, the references to the UserTransaction
interface, and the references to the EntityManager and/or
EntityManagerFactory by serializable objects during the object’s
serialization.
April 14, 2011
MTA with Java EE / Stateful Session Beans
13
Non-Transient Fields Continued II
This means, for example, that the Bean Provider must close all JDBC
connections in the @PrePassivate method and assign the instance’s
fields storing the connections to null.
Note:
●
Instance fields can be private.
April 14, 2011
MTA with Java EE / Stateful Session Beans
14
Callback for Stateful Session Beans
Supported life cycle event callback methods for stateful session beans:
●
@PostConstruct: A method being executed ...
– ... after any dependency injection (not discussed in this session) was
made
– ... before the first business method is executed
●
@PreDestroy: A method being executed ...
– ... after any method annotated with @Remove has completed.
●
@PrePassivate: A method being executed ...
– ... before the container is about to passivate the instance
●
@PostActivate: A method being executed ...
– ... after the instance it has just been reactivated.
All callback methods occur in a unspecified transaction and security
context.
April 14, 2011
MTA with Java EE / Stateful Session Beans
15
Client View
In the context of
EJB, this part is
more complicated
than shown
Communication in a distributed environment
«interface»
Calculator
Client
Calculator
Stub
●
network
Calculator
«bean»
CalculatorBean
Skeleton
"The communication stubs used on the client side are artifacts
generated at the enterprise bean’s deployment time by the Container
Provider’s tools. The stubs used on the client are specific to the wire
protocol used for the remote invocation." [EJB3.1 Core, 15.1.1]
April 14, 2011
MTA with Java EE / Stateful Session Beans
16
Getting a Remote Reference to the Calculator Bean:
import javax.naming.Context;
import javax.naming.InitialContext;
...
// in some method:
{
// Get the initial JNDI context.
Context ctx = new InitialContext();
// Get a Calculator session bean.
// Alternative: Use a Service Locator.
Calculator calculator =(Calculator)
ctx.lookup(
“java:global/calculator1/calculator1-ejb/CalculatorBean”);
// Use the bean.
calculator.add(12, 25));
calculator.memoryAdd(5);
}
April 14, 2011
// Remove the bean when no longer used:
calculator.close();
// on bean class: @Remove-annotaded close() meth.
MTA with Java EE / Stateful Session Beans
17
Alternative: Getting Remote Reference by
Dependency Injection
●
To get a remote reference in a client
running in an “client application container”:
(Note: Can be used in the client's “main”
class only)
import javax.ejb.EJB;
public class Main {
@EJB
private static Calculator calculator;
// for example, in the main() method:
public static void main(String[] args) {
}
April 14, 2011
}
// Just use the bean.
calculator.add(12, 25);
calculator.memoryAdd(5);
// Remove the bean when no longer used:
calculator.close();
MTA with Java EE / Stateless Session Beans
18
Provoking the Execution of @Remove Method
In a client: Removal of a stateful session bean
●
"A client may remove a stateful session bean by invoking a method of its
business interface designated as a @Remove method."
[EJB3.1, 3.4.5]
Recommendation:
●
"A client should remove a stateful session bean by invoking a method of
its business interface designated as a @Remove method."
April 14, 2011
MTA with Java EE / Stateful Session Beans
19
Session Object Identity For Stateful Session Beans
For stateful session beans, the following code fragment is valid
[EJB3.1 Core, 3.4.7.1]:
@EJB Cart cart1; // alternative: lookup via JNDI
@EJB Cart cart2;
...
if (cart1.equals(cart1)) { // this test returns true
...
}
...
if (cart1.equals(cart2)) { // this test returns false
...
}
April 14, 2011
MTA with Java EE / Stateful Session Beans
20
Compare with Session Object Identity for Stateless S. B.
●
For stateless session beans, the following code fragment is valid
[EJB3.1, 3.4.7.2]:
@EJB Cart cart1;
@EJB Cart cart2;
...
if (cart1.equals(cart1)) {
...
}
...
if (cart1.equals(cart2)) {
...
}
April 14, 2011
// this test returns true
// this test returns true
MTA with Java EE / Stateless Session Beans
21
References
[EJB3.1]
April 14, 2011
JSR 318: Enterprise JavaBeans, Version 3.1
EJB Core Contracts and Requirements
http://jcp.org/aboutJava/communityprocess/final/jsr318/index.html
MTA with Java EE / Stateful Session Beans
22