Enterprise JavaBeans Today's Lecture JNDI JNDI

farflungconvyancerSoftware and s/w Development

Dec 2, 2013 (3 years and 11 months ago)

114 views

1
26.2.2003 Tuomas Mikkola 1
Enterprise JavaBeans
Lecture 2
Tuomas Mikkola
Solita Oy
26.2.2003 Tuomas Mikkola 2
Today’s Lecture
• JNDI
• Session Beans
• Entity Beans
• Message-Driven Beans
• Slides available on cource’s web site :
http://www.cs.uta.fi/~hako/
26.2.2003 Tuomas Mikkola 3
JNDI
Initial context
Initial context
Context bound
Context bound
into another
into another
context
context
Same object bound into two
Same object bound into two
different contexts
different contexts
A binding is the association
A binding is the association
of an object with a logical
of an object with a logical
name and a context
name and a context
26.2.2003 Tuomas Mikkola 4
JNDI
2
26.2.2003 Tuomas Mikkola 5
Usage of JNDI
• Simple usage: binding and looking up objects by
name
• Putting Objects into JNDI (binding objects into the
name space)
– 1) Get an initial context - the first handle into the name
space
– 2) Bind object to context
• Getting Objects out of JNDI (resolving objects out of
the name space)
– 1) Get an initial context - the first handle into the name
space
– 2) Object lookup
26.2.2003 Tuomas Mikkola 6
InitialContext
• javax.naming.InitialContext is a subclass of
javax.naming.Context
• Javasoft attempted to create a vendor-neutral
approach for initialisation that would not
require recompilation of code
• Obtaining initial context leverages
java.util.Properties objects and system
properties for initialisation
26.2.2003 Tuomas Mikkola 7
JNDI Example
//Initialization
Hashtable?env =?new Hashtable();??????
env.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url",?
"jnp://localhost:1099");
env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
m_context =?new InitialContext(env);
//Bind?
Fruit?fruit?=?new?Fruit("orange");
m_context.bind("favorite",?fruit);?
//Lookup
Object o?=?m_context.lookup("java:comp/env/ejb/FaqHome")
//Initialization
Hashtable?env =?new Hashtable();??????
env.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url",?
"jnp://localhost:1099");
env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
m_context =?new InitialContext(env);
//Bind?
Fruit?fruit?=?new?Fruit("orange");
m_context.bind("favorite",?fruit);?
//Lookup
Object o?=?m_context.lookup("java:comp/env/ejb/FaqHome")
26.2.2003 Tuomas Mikkola 8
Usage of JNDI
EJB
Home
client
JNDI
Naming
Service
Container
Lookup
Bind
3
26.2.2003 Tuomas Mikkola 9
Usage of Home Interface
EJB
Home
EB
EB
EB
Create,
find,
remove
client
JNDI
Naming
Service
Container
Home Interface
26.2.2003 Tuomas Mikkola 10
Usage of Remote Interface
EJB
Home
EB
Create,
find,
remove
client
JNDI
Naming
Service
Container
EJB
Object
Remote Interface
EB
EJB
Object
EB
EJB
Object
EJB
Object
Stub
26.2.2003 Tuomas Mikkola 11
Example
FaqHome home?=?(FaqHome) PortableRemoteObject.narrow(
m_context.lookup("java:comp/env/ejb/Faq",
FaqHome.class);
Faq?faqBean =?home.create();
faqBean.createGroup("FooBar");
faqBean.remove();
FaqHome home?=?(FaqHome) PortableRemoteObject.narrow(
m_context.lookup("java:comp/env/ejb/Faq",
FaqHome.class);
Faq?faqBean =?home.create();
faqBean.createGroup("FooBar");
faqBean.remove();
26.2.2003 Tuomas Mikkola 12
Home Interface
Remote Interface
public void create ()
… <businessMethod1> (...)
… <businessMethod2> (...)
… <businessMethod1> (...)
{
// implementation
}
... <businessMethod2> (...)
{
// implementation
}
public void ejbCreate ()
{
// implementation
}
Interface Signatures Bean Class
Implementation of Business methods
and Container-contract' methods
Interfaces & Bean Class
4
26.2.2003 Tuomas Mikkola 13
EJB Object
• Is the network-visible, remote object
• Is NOT instantiated enterprise Bean
• Acts as a proxy for the enterprise Bean
26.2.2003 Tuomas Mikkola 14
EJB Session Bean
• Acts as a logical interface of the client
• Non-persistence object that implements
business logic
• The business logic is implemented in methods
defined in remote interface.
26.2.2003 Tuomas Mikkola 15
Session Bean Life Cycle
• Stateless
Business Method
26.2.2003 Tuomas Mikkola 16
Allocating new instance
: FaqBean
Container
: Session
Context
new
new
setSessionContext()
ejbCreate()
<Bean Instance>
5
26.2.2003 Tuomas Mikkola 17
Business Method Invoke
: Client
: FaqBean
createGroup
createGroup
Obtain a free Bean instance from the pool
<Bean Instance>
<EJB Object>
“Delegation”
createGroup
26.2.2003 Tuomas Mikkola 18
Stateful Session Bean
• Used only by one client during the client session
– Does not represent any persistent information of the system
– May however use some persistent data, needed for the
client
• Maintain a conversational state
– Group of attributes related to the client session
– May contain connection resources such as file, socket,
database connection, …
• May be activated or passivated by the container
– to improve resource management
26.2.2003 Tuomas Mikkola 19
Session Bean Life Cycle
• Stateful
26.2.2003 Tuomas Mikkola 20
Activation - Passivation
• During passivation, the state of the EJB instance is
stored automatically by the container.
• During activation, the state of the EJB instance is
loaded automatically by the container.
• An EJB instance may be passivated if none of its
methods are invoked during a specified amount of
time (customisable)
• An EJB instance is activated by any method call (if
not active).
• If working with EJB 1.1, bean must be aware of the
Passivation-Activation mechanism if connection
resources
are involved
6
26.2.2003 Tuomas Mikkola 21
Remove method invoked by a client
: Client
: FaqImpl
: FaqBean
remove()
ejbRemove()
<<Remote Object>> <<Bean Instance>>
26.2.2003 Tuomas Mikkola 22
Deployment Descriptor
• XML based ejb-jar.xml
• this file tells the EJB server which classes
make up the bean implementation
• must be in the directory ./META-INF
26.2.2003 Tuomas Mikkola 23
Session Bean – Deployment Descriptor
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Faq</ejb-name>
<home>fi.solita.examples.ejb.example.FaqHome</home>
<remote>fi.solita.examples.ejb.example.Faq</remote>
<ejb-class>fi.solita.examples.ejb.example.FaqBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Faq</ejb-name>
<home>fi.solita.examples.ejb.example.FaqHome</home>
<remote>fi.solita.examples.ejb.example.Faq</remote>
<ejb-class>fi.solita.examples.ejb.example.FaqBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
26.2.2003 Tuomas Mikkola 24
Example
• Business logic for simple Frequently Asked
Questions (FAQ) application
7
26.2.2003 Tuomas Mikkola 25
FAQ - Home Interface
package?fi.solita.examples.ejb.example;
import?java.io.Serializable;
import?java.rmi.RemoteException;
import?javax.ejb.CreateException;
import?javax.ejb.EJBHome;
public?interface?FaqHome extends?EJBHome {
public?Faq create()?throws?RemoteException,CreateException;
}

package?fi.solita.examples.ejb.example;
import?java.io.Serializable;
import?java.rmi.RemoteException;
import?javax.ejb.CreateException;
import?javax.ejb.EJBHome;
public?interface?FaqHome extends?EJBHome {
public?Faq create()?throws?RemoteException,CreateException;
}


26.2.2003 Tuomas Mikkola 26
FAQ - Remote Interface
package?fi.solita.examples.ejb.example;
import?javax.ejb.EJBObject;
import?java.rmi.RemoteException;
public?interface?Faq extends?EJBObject {
public?void?createGroup(String?group)?throws?RemoteException;
}
package?fi.solita.examples.ejb.example;
import?javax.ejb.EJBObject;
import?java.rmi.RemoteException;
public?interface?Faq extends?EJBObject {
public?void?createGroup(String?group)?throws?RemoteException;
}
26.2.2003 Tuomas Mikkola 27
FAQ - Bean implementation
• Notice that most of the methods are empty;
they have to exist because they're specified
by the SessionBean interface, but they not
needed for this simple example EJB.
26.2.2003 Tuomas Mikkola 28
Bean Implementation
package?fi.solita.examples.ejb.example;
import?javax.ejb.*;
import?java.rmi.RemoteException;
public?class?FaqBean implements?SessionBean {
private?SessionContext?mySc =?null;
public?void?ejbCreate()?throws?CreateException {
System.out.println("Faq bean?is?created?now!");
}
public?void?ejbRemove()?{}
public?void?ejbActivate()?{?}
public?void?ejbPassivate()?{}
public?void?setSessionContext(SessionContext sc)?{
System.out.println("Session?Context?initialized!");
mySc =?sc;
}
public?void?createGroup(String?group)?throws?RemoteException {
System.out.println("create?Group?called?"?+?group);
}
}
package?fi.solita.examples.ejb.example;
import?javax.ejb.*;
import?java.rmi.RemoteException;
public?class?FaqBean implements?SessionBean {
private?SessionContext?mySc =?null;
public?void?ejbCreate()?throws?CreateException {
System.out.println("Faq bean?is?created?now!");
}
public?void?ejbRemove()?{}
public?void?ejbActivate()?{?}
public?void?ejbPassivate()?{}
public?void?setSessionContext(SessionContext sc)?{
System.out.println("Session?Context?initialized!");
mySc =?sc;
}
public?void?createGroup(String?group)?throws?RemoteException {
System.out.println("create?Group?called?"?+?group);
}
}
8
26.2.2003 Tuomas Mikkola 29
Persistence
• The attributes of the object must be stored
using a persistent mechanism
• Examples of persistence management:
– Use Java serialization mechanism
– Use a relational database through JDBC
26.2.2003 Tuomas Mikkola 30
Persistence
C
l
i
e
n
t
-
c
l
i
e
n
t
I
D
-
c
o
r
p
o
r
a
t
e
N
a
m
e
-
a
d
d
r
e
s
s
-
t
o
w
n
-
p
o
s
t
c
o
d
e
persistent storage
database,
files,
etc...
Object in memory
26.2.2003 Tuomas Mikkola 31
Object / Relational Mapping (O/R)
• Associate a database table with a class
Each instance of the class (each object) is
stored in a row
• Associate a database column with an
attribute of the class
26.2.2003 Tuomas Mikkola 32
O/R Mapping
Client
clientId
clientId
Corporatename
address
town
Postcode
clientId
clientId Corporatename address town Postcode
411MEB MEB 1,Lichfield Road B ’ham B7 4TU
411PIT PITCH 34, London Road Chester C60 7YJ
411WC World Company 1,The Strand London W1 8PL
Primary
key
Database table
java class
9
26.2.2003 Tuomas Mikkola 33
EJB Entity Bean
• Entity lifetime is not dependent on the client
connection.
• Shared data between clients.
• Has a home interface
• Has a remote interface
• Lives in a Container
26.2.2003 Tuomas Mikkola 34
Entity Bean
• Should be transactional in most cases
• May be accessed by multiple clients
concurrently
– Concurrent access is managed by the container or
by the underlying database
• Persistence may be managed by the
container or by the EJB itself
• Persistent: stored in a database or comes
from another wrapped application.
26.2.2003 Tuomas Mikkola 35
CMP vs. BMP
• Bean-managed persistence (BMP)
– A lot of hand-written code for persistence
management
– Persistence management cannot be changed at
deployment time
– More control over SQL queries if a relational DBMS
is used for persistence
26.2.2003 Tuomas Mikkola 36
CMP vs. BMP
• Container-managed persistence
– Not a single line of hand-written code for
persistence management
– Flexible persistent storage
– Less control over SQL queries (generated by the
container)
– Possibly performance problems when complicated
operations are needed
10
26.2.2003 Tuomas Mikkola 37
Entity Bean
Home Interface
Remote Interface
EJB instance
EJB Client
“Create”
Remote Object
“Find”
Home Object
EJB Container
26.2.2003 Tuomas Mikkola 38
Parts of Entity Bean
• The Home interface
– Provided by the developer
– Manages the EJB life cycle (create, find
, remove)
• The Primary Key class
– represents the identity of the EJB within its Home
– Java class implementing Serializable.
– provided by the EJB developer
• The Bean Implementation class
– Provided by the developer
– Implements business services
– Implements the Container-contract methods
26.2.2003 Tuomas Mikkola 39
Primary Key
• Must be a Serializable class
• The attributes must be declared as public
• Must have a default (parameter-less) constructor
• The primary key attributes represent the columns in
database table.
• The primary key attributes must have the same name
and type
as used in the abstract accessors found of
the Entity Bean Class.
• May also be a standard Java class (for example
java.lang.Integer)