Download Stateless 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
Stateless
Session Beans
Course Multi Tier Business Applications with Java EE
Prof. Dr. Eric Dubuis
Berner Fachhochschule
Biel
Content
●
●
●
●
●
●
●
02/02/10
Characteristics of stateless session beans
Architectural view
Key concepts and characteristics of stateless session beans
Example: Business interface, bean implementation
Bean's life cycle
Callback annotations
Client view
MTA with Java EE / Stateless Session Beans
2
Architectural View: Application Servers, EJB Containers
Revisited
●
Application server and EJB container (JBoss):
1
1099 (JBoss)
: Client
Naming
Service
(JNDI)
IF
3
10
2
5
4
●
02/02/10
9
EJB container
IF
: Stub
Message legend:
see next slide
IF:
“business
interface”
anonymous
8
:
6
7
: Session
Bean
About enterprise beans:
– they live in the EJB container
– there are session beans (stateful, stateless) and message-driven
beans
– EJB3: entities are no longer named as enterprise beans
MTA with Java EE / Stateless Session Beans
3
Messages Explained
(1) Client looks up, by name, remote service at naming service
(2) Naming service returns remote reference (JBoss: stub [proxy] object,
standard requires that all appservers support also CORBA object
reference)
(3) Client operates on stub
(4) Stub serializes parameters and delegates request over the network
(5) Appserver gets request, de-serializes parameters, and passes it over
to helper object (pre EJB3: EJBObject). This object is an instance of a
generated class
(6) Helper object delegates request to session bean after EJB container
has managed (order may change): retrieving bean from pool, injecting
dependencies, handling transactions, handling security, ...
(7) Session bean returns response
(8) Appserver serializes response data
(9) Response is passed back over the network to client's stub
(10) Stub de-serializes response data and passes response to client
02/02/10
MTA with Java EE / Stateless Session Beans
4
Some Key Concepts of Enterprise Bean Class
The Enterprise Bean Class
●
The “component”
●
Primary artifact in EJB programming
●
Is a “normal” Java class ...
– ... but it is annotated
–
●
Alternatively or additionally: XML deployment descriptor
The annotation specifies the semantics of the bean and the
requirements for the EJB container
The Business Interface
●
Sometimes referred to as the component interface
●
Is a plain Java interface.
Optional: May have an EJB3 annotation, too.
●
02/02/10
An enterprise bean class may implement more than one
business interface
MTA with Java EE / Stateless Session Beans
5
Characteristics of a Stateless Session Beans
●
●
●
●
●
●
●
02/02/10
Provides a service to its client.
The client-bean association is short-lived; it exists during the execution
of one of the bean's methods only.
Cannot maintain per-client state between two succinct method calls:
– client must pass all state information to the bean as parameters.
Can maintain global state information such as JNDI context, ...
Can easily be pooled by the container.
Are typically used to provide actions in business processes such as
"Process Order".
"Stateless session bean" is a misnomer; it should be called "service
bean".
MTA with Java EE / Stateless Session Beans
6
A Stateless Session Bean Example: The Business
Interface
/**
* Defines a simple calculator component.
*/
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);
}
Remarks
●
No interface inheritance of a specialized, remote interface!
●
No remote exception even for the case that there is a remote
implementation.
●
Can have an EJB3 annotation.
02/02/10
MTA with Java EE / Stateless Session Beans
7
The Calculator Bean Class
import javax.ejb.Remote;
import javax.ejb.Stateless;
/**
* Implements a simple calculator component.
*/
@Stateless
@Remote(value = { Calculator.class })
public class CalculatorBean implements Calculator {
public double add(double x, double y) {
return x + y;
}
public double subtract(double x, double y) {
return x – y;
}
public double multiply(double x, double y) {
return x * y;
}
public double divide(double x, double y) {
return x / y;
}
}
02/02/10
MTA with Java EE / Stateless Session Beans
8
Remarks Regarding The Calculator Bean Class
●
●
●
●
The @Stateless annotation tells the EJB container that CalculatorBean
is a stateless session bean.
The @Remote annotation designates the remote interface of the bean
[EJB3, 10.2]. The value element is specified only when the annotation is
applied to the bean class.
By default, the bean's name as published in the JNDI is formed of:
– Bean class name + “/” + “remote”
Example:
–
class name: CalculatorBean
–
JNDI name: CalculatorBean/remote
JNDI: Java Naming and Directory Interface
02/02/10
MTA with Java EE / Stateless Session Beans
9
Stateless Session Bean Life Cycle [EJB-Core]
02/02/10
MTA with Java EE / Stateless Session Beans
10
Callbacks for Stateless Session Beans
Supported life cycle event callbacks for stateless 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
Usage pattern:
For example to establish a connection to a database
●
@PreDestroy: A method being executed ...
– ... at the time the bean instance is destroyed.
Usage pattern:
For example to disconnect the database connection
Above kind of methods occur in a unspecified transaction and security
context.
02/02/10
MTA with Java EE / Stateless Session Beans
11
Client View
●
Communication in a Distributed Environment
1
Naming
Service
(JNDI)
: Client
Calculator
3
8
02/02/10
Calculator
Calculator
2
: Stub
Message legend:
see next slide
–
EJB container
5
4
7
:
6
: Calculator
Bean
"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-Core, 15.1.1]
MTA with Java EE / Stateless Session Beans
12
Messages Explained
(1) Client looks up, by name, remote service at naming service
(2) Naming service returns remote reference (JBoss: stub [proxy] object,
standard requires that all appservers support also CORBA object
reference)
(3) Client operates on stub
(4) Stub serializes parameters and sends request over the network
(5) After de-serialization, helper object (= instance of generated class)
delegates request to session bean
(6) Session bean returns response
(7) Appserver's helper object serializes response data and sends it back
over the network
(8) Stub de-serializes response data and passes response to client
02/02/10
MTA with Java EE / Stateless Session Beans
13
Client View: Getting Remote Reference
To get a remote reference in a client:
●
import javax.naming.Context;
import javax.naming.InitialContext;
«interface»
Context
lookup
list
...
// in some method:
{
// Get the initial JNDI context.
Context ctx = new InitialContext();
1
// Get a Calculator session bean.
lookup
list
Calculator calculator = (Calculator)
...
ctx.lookup(“CalculatorBean/remote”);
// Alternative:
// delegate the lookup to a Service Locator
2
3
}
●
Initial
Context
// Use the bean.
calculator.add(12, 25);
In a client: Removal of a stateless session bean
"The life cycle of a stateless session bean does not require that it be removed
by the client. Removal of a stateless session bean instance is performed by
the container, transparently to the client." [EJB-Core, 3.4.3]
02/02/10
MTA with Java EE / Stateless Session Beans
14
Session Object Identity
For stateless session beans, the following code fragment is valid
[EJB-Core, 3.4.5]:
●
Yet another alternative
to get a reference to a
bean: dependency
injection
(available in
containers only!)
@EJB Cart cart1;
@EJB Cart cart2;
...
if (cart1.equals(cart1)) {
...
}
...
if (cart1.equals(cart2)) {
...
}
02/02/10
// this test must return true
// this test must also return
// true
MTA with Java EE / Stateless Session Beans
15
References
[EJB]
JSR 220: Enterprise JavaBeans, Version 3.0
EJB 3.0 Simplified API
http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html
[EJB Core]
JSR 220: Enterprise JavaBeans, Version 3.0
EJB Core Contracts and Requirements
http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html
02/02/10
MTA with Java EE / Stateless Session Beans
16