enterprise java beans.ppt
TRANSCRIPT
-
8/9/2019 Enterprise Java Beans.ppt
1/56
Enterprise Java Beans
-
8/9/2019 Enterprise Java Beans.ppt
2/56
What is Distributed
Computing?
-
8/9/2019 Enterprise Java Beans.ppt
3/56
Basic Concepts
Client-Server:
The clientis the entity accessing the remote
resource and the serverprovides access to the
resource. Operationally, the client is the caller
and the server is the callee.
In Java terms: The client is the invoker of the method and the
server is the object implementing the method.
-
8/9/2019 Enterprise Java Beans.ppt
4/56
Basic Concepts (continued)
The client and the server can beheterogeneous:
Different implementation languages
Different operating systems
The roles can be transient
The definition is with respect to a particularinteraction.
Client and Server refer both to the code andthe system on which the code is running
-
8/9/2019 Enterprise Java Beans.ppt
5/56
y= F(x)
F(x) {
return 5;}
Network
Client Server
1
2
341. Send message to call F with
parameter X
2. Receive message that F wascalled with the given
parameter
3. Send message with the result
of calling F
4. Receive message with the
result of calling F
Client Server Interactions
-
8/9/2019 Enterprise Java Beans.ppt
6/56
Finding the Server
How does the client find a server?
One approach is a Name Service:
Associate a name with each server
When server starts, it registers with a namingservice using the name
When the client wants to find the server, it asksthe naming service
Naming service can itself be a server
How does the client find the naming server?
-
8/9/2019 Enterprise Java Beans.ppt
7/56
y= F(x)
F(x) {
return 5;
}
Network
Client Server1
4
56
1. Register "F" with name server
2. Lookup "F" using name server3. Send message to call F with
parameter X
4. Receive message that F was called
with the give parameter
5. Send message with the result of
calling F
6. Receive message with the result of
calling F
Name Server
2
3
Naming Services
-
8/9/2019 Enterprise Java Beans.ppt
8/56
Parameter Passing
Distribution complicates parameters passing
Parameters are passed via a message and not via alocal stack
Issues include: Different representations of primitive types
convert representation
Pointers are address space relative
Composite Types (e.g., structures) embedded pointers
need to be flattened and reconstructed
-
8/9/2019 Enterprise Java Beans.ppt
9/56
Marshaling/Unmarshaling
Marshaling:
done by client (i.e., caller)
packing the parameters into a message
flatten structures (e.g., objects)
perform representation conversions if necessary
also done by server (i.e., callee) for results
Unmarshaling: done by receiver of message to extract
parameters
-
8/9/2019 Enterprise Java Beans.ppt
10/56
1. MarshalX
2. Send Msg
Network
Client Server
7. ReceiveMsg w/ Result
8. Unmarshal Result
y= F(x)
F(x) {
return 5;
}
3. R
eceiveM
sg4. UnmarshalX
5. MarshalResult
6. Send Msg w/ Result
Parameter Passing Flow
-
8/9/2019 Enterprise Java Beans.ppt
11/56
Stubs and Skeletons
Stub is on the client
implements original interface
contains information to find the server in an OO language, the stub object is a proxyfor
the real object
Skeleton is on the server
calls original routine
-
8/9/2019 Enterprise Java Beans.ppt
12/56
1. Marsh
allX
2. Send Msg
Network
Client Server
8. ReceiveResult Msg
9. Unmarshal Result
F(x) { // stub
3. ReceiveMsg
4. UnmarshalX
5. Call F(X)
6. MarshalResult
7. Send Msg w/ Result
}
F_skeleton() {
}
Stubs and Skeletons: Flow
-
8/9/2019 Enterprise Java Beans.ppt
13/56
Where do Stubs and Skeletons come
from? Writing (un)marshaling code is bug-prone
communication code has many details
structure of code is very mech
anical Answer:
Stubs and Skeletons can be generated from a
description of the code to be remotely invoked
A separate Interface Definition Language (IDL)
Description can be generated from code to be
distributed
-
8/9/2019 Enterprise Java Beans.ppt
14/56
Server Architecture
Servers can typically handle concurrentrequests from multiple clients
Typically the same address spaces providesmultiple interfaces
A common server architecture:
accepta request (i.e., a call from a client)
determine which routine is being invoked dispatch request to a thread of execution
start the thread executing in the appropriateskeleton
-
8/9/2019 Enterprise Java Beans.ppt
15/56
Dispatcher Worker
ThreadsCall f_skel
fg
Call g_skel
ServerClients
network
f
g
Server Architecture (continued)
-
8/9/2019 Enterprise Java Beans.ppt
16/56
What is EJB?
-
8/9/2019 Enterprise Java Beans.ppt
17/56
Enterprise Java Beans
Server side component written in Javaprogramming language.
Encapsulates the business logic- the code that
fulfils the purpose of the application. This platform functions as transaction-server
technology.
By using this you can control transactions across
multiple transaction platforms including bothdatabases and other enterprise processingsystems.
-
8/9/2019 Enterprise Java Beans.ppt
18/56
Enterprise Java Beans: Components
and Containers An Enterprise Java Bean (EJB) is a server side component
that provides reusable business logic functionality and/ora representation of a persistent business entity.
An EJB Containerexecutes an EJB due to a client request. Provides the plumbing necessary to execute the EJB including
non-business logic related functionality such as transactions, security,concurrency, remote access, etc.
life cycle functions, e.g., creating, destroying, etc.
Clientuses an interface to access the Bean indirectly A deploymentdescriptordescribes the structure of the
Bean and how to execute the Bean as part of anapplication
-
8/9/2019 Enterprise Java Beans.ppt
19/56
-
8/9/2019 Enterprise Java Beans.ppt
20/56
EJB Goals
For Bean Provider, Application Assembler andDeployer Simplicity
P
roductivity Reuse
Merchant market for components
Enterprise qualities Distribution
Integrity Security
Transactions
. . .
-
8/9/2019 Enterprise Java Beans.ppt
21/56
EJB Architecture
A typical EJB Architecture comprises of
following components
EJB Server EJB Client
EJB Container
-
8/9/2019 Enterprise Java Beans.ppt
22/56
EJB Architecture
EJB Server
EJB Client
EJB Container
Enterprise Java Bean
Invoke EJB
methodsInvoke EJB
Container
Methods
-
8/9/2019 Enterprise Java Beans.ppt
23/56
EJB Server
Provide the system services likemultiprocessing, load balancing, deviceaccess, naming and transaction services and
makes container visible.In a typical development and deployment
scenario, there will be an
EJB Server provider
EJB Provider
Application Assembler
-
8/9/2019 Enterprise Java Beans.ppt
24/56
EJB Client
For their operations they use the EJB
Beans.
They locate the EJB container thatcontains the bean through the Java
Naming and Directory Interface (JNDI).
They then use the EJB Containermethods to invoke EJB bean methods.
-
8/9/2019 Enterprise Java Beans.ppt
25/56
EJB Container
An EJB container is an interface provider
between an EJB and outside world.
An EJB clienth
as no access to bean directly. Any bean access is done through container
generated methods, which in turn invoke the
bean methods.
The two type of containers are
Session Containers and Entity Containers
-
8/9/2019 Enterprise Java Beans.ppt
26/56
EJB Roles
Bean Provider
Produces a component of reusable business logic in an ejb-
jar file
Application Assembler
Combines multiple beans (or jar files) into an application
described by a deployment descriptor
Deployer
Customizes the application for a specific server/container
The deployment is a two stage process The deployer first generates the additional classes and interfaces
that enable the container to manage the EJBs at run time.
The deployer performs the actual installation of the EJB and the
additional classes and interfaces into the EJB container.
-
8/9/2019 Enterprise Java Beans.ppt
27/56
EJB Roles
Server and Container Provider
Provides the tools to allow deploying an application and
the runtime support to execute the application according
to the deployment descriptor
System Administrator
Owns the responsibility of the configuration and
adminstration of the Enterprise computing.
-
8/9/2019 Enterprise Java Beans.ppt
28/56
Benefits of EJB
Simplify the development of large, distributed applications
Firstly, because these provide system level services toenterprise beans, the developer can solve businessproblems with ease. The EJB container and not the bean
developer is responsible for system level services such astransaction management and security authorization.
Secondly, because the beans and not the clients containthe applications business logic, t he client developer canconcentrate on the presentation. As a result clientsbecome thinner so they can be run on small devices.
Thirdly, because they are portable components, theapplication assembler can build new applications from theexisting beans.
-
8/9/2019 Enterprise Java Beans.ppt
29/56
Limitations of EJB
Managing or Synchronizing threads
Accessing files or directories with the java.iopackage.
Using AWT functionality to display informationor to accept information from the keyboard.
Listening on a socket, accepting connections ona socket.
Setting a socket factory using ServerSocket, orthe stream handler factory used by the URLclass.
Loading the native library
-
8/9/2019 Enterprise Java Beans.ppt
30/56
Local vs. Remote Interfaces
Entity and Session Beans can support local and remoteinterfaces Client is written to a specific interface Interface(s) supported is not transparent to Bean provider
Local interface Not location independent
Client and EJB run in the same JVM Example: A Bean always accessed by other Beans
Parameter passing is by reference (same as standard Java) Supports fine grained access
Remote interface Location independent Parameters passed by value (RMI semantics)
-
8/9/2019 Enterprise Java Beans.ppt
31/56
EJB Interfaces Home Interface
Can be viewed as a collection of Beans
Lifecycle functions, e.g., create, remove, find
Home business methods
Business methods that are not instance specific
Component Interface Business logic
Define clients view of t he Bean
Client never directly access Bean instance
Client finds home interface via JNDI
Client uses home interface to obtain a reference to the Beanscomponent interface
Defined by the Bean Provider
Client side implementations are generated when the Bean isdeployed
Delegate invocations to Bean instance
-
8/9/2019 Enterprise Java Beans.ppt
32/56
Architecture with Remote Interfaces
-
8/9/2019 Enterprise Java Beans.ppt
33/56
Types of Beans
Session
Client and application logic focus
Entity Persistent data focus
Message
Asynchronous message processing
-
8/9/2019 Enterprise Java Beans.ppt
34/56
-
8/9/2019 Enterprise Java Beans.ppt
35/56
How a Client Uses a Session Bean
Home
Client
Component
JNDI Server
Bean Instance
Container
-
8/9/2019 Enterprise Java Beans.ppt
36/56
-
8/9/2019 Enterprise Java Beans.ppt
37/56
Lifecycle of a Stateful Session Bean
1. The client initiates the life
cycle by invoking the
create method.2. The EJB container
instantiates the bean and
then invokes the
setSessionContext and
ejbCreate methods in thesession bean.
3. The bean is now ready to
have its business methods
invoked.
Following figure illustrates the stages that a session bean passes
through during its lifetime.
-
8/9/2019 Enterprise Java Beans.ppt
38/56
Lifecycle of a Stateful Session Bean While in the ready stage, the EJB container may decide to
deactivate, orpassivate, the bean by moving it from memory to
secondary storage
The EJB container invokes the bean's ejbPassivate method
immediately before passivating it.
If a client invokes a business method on the bean while it is in
the passive stage, the EJB container activates the bean, moving
it back to the ready stage, and then calls the bean's ejbActivate
method.
At the end of the life cycle, the client invokes the remove
method and the EJB container calls the bean's ejbRemove
method. The bean's instance is ready for garbage collection.
Your code controls the invocation of only two life-cycle
methods--the create and remove methods in the client. All
oth
er meth
ods are invoked by th
e EJB container.
-
8/9/2019 Enterprise Java Beans.ppt
39/56
Stateless Session Beans
Not tied to any particular client Can use instance variables only if they are not client
related
All Stateless Session Beans are equivalent A container can choose To serve the same instance of a Bean to multiple clients
To serve difference Bean instances to the same client atdifferent times
A container may maintain a pool of Stateless SessionBeans No necessary relation between when a client creates the
Bean and when the Container creates the Bean
Provide very high scalability
-
8/9/2019 Enterprise Java Beans.ppt
40/56
Life Cycle of Stateless Session Bean
The figure illustrates the
stages of a stateless
session bean.
Because a stateless
session bean is never
passivated, its life cycle
has just two stages:
1. nonexistent and
2. ready for theinvocation of business
methods.
-
8/9/2019 Enterprise Java Beans.ppt
41/56
Entity Beans
Represent persistent data Typically represent a row from a database Can also represent entities implemented by legacy applications
Can be shared across multiple users
Long lived Lifetime is tied to life of data and not to a particular client Entity objects (not Beans) can be created outside of a Container, e.g.,
from a pre-existing database.
Data persistence can managed either by Bean or Container
Client can either create a new Entity Bean of find anexisting Bean Home interface provides finder methods
findByPrimaryKey unique key within a home Application specific finder methods
-
8/9/2019 Enterprise Java Beans.ppt
42/56
Life Cycle of Entity Bean After the EJB container creates the instance, it calls the
setEntityContextmethod of the entity bean class. The
setEntityContextmethod passes the entity context to
the bean.
After instantiation, the entity bean moves to a pool of
available instances.While in the pooled stage, the
instance is not associated with any particular EJB
object identity. All instances in th
e pool are identical.The EJB container assigns an identity to an instance
when moving it to the ready stage.
There are two paths from the pooled stage to the
ready stage. On the first path, the client invokes the
create method, causing the EJB container to call the
ejbCreate and ejbPostCreate methods. On the second
path, the EJB container invokes the ejbActivate
method.While in the ready stage, an entity bean's
business methods may be invoked.
There are also two paths from the ready stage to the
pooled stage. First, a client may invoke the remove
method, which causes the EJB container to call the
ejbRemove method. Second, the EJB container may
invoke the ejbPassivatemethod.
-
8/9/2019 Enterprise Java Beans.ppt
43/56
Life Cycle of Entity Bean
At the end of the life cycle, the EJBcontainer removes the instance from thepool and invokes the unsetEntityContextmethod.
In the pooled state, an instance is notassociated with any particular EJB objectidentity. With bean-managed persistence,when the EJB container moves an instancefrom the pooled state to the ready state, itdoes not automatically set the primarykey. Therefore, the ejbCreate andejbActivate methods must assign a valueto the primary key. If the primary key isincorrect, the ejbLoad and ejbStoremethods cannot synchronize the instance
variables with the database. In the pooled state, the values of the
instance variables are not needed. You canmake these instance variables eligible forgarbage collection by setting them to nullin the ejbPasssivate method.
-
8/9/2019 Enterprise Java Beans.ppt
44/56
Message Beans
Executes upon receipt of a client JMS message
Asynchronous
No return value
Stateless and short lived
May access persistent data but does not represent persistent
data
Not tied to a client
A single Message Bean can process messages from multipleclients
Has neither Home nor Component interface
-
8/9/2019 Enterprise Java Beans.ppt
45/56
Life Cycle ofMessage Bean The EJB container usually creates a pool of
message-driven bean instances. For each
instance, the EJB container instantiates
the bean and performs these tasks:
It calls the setMessageDrivenContext
method to pass the context object to the
instance. It calls the instance's ejbCreatemethod.
Like a stateless session bean, a message-
driven bean is never passivated, and it has
only two states:
1. nonexistent and
2. ready to receive messages.
At the end of the life cycle, the container
calls the ejbRemove method. The bean's
instance is then ready for garbage
collection.
-
8/9/2019 Enterprise Java Beans.ppt
46/56
-
8/9/2019 Enterprise Java Beans.ppt
47/56
Download and install OpenEJB
After extracting the file contents, you should now see a directory namedopenejb-3.0. If you look under this directory, you will find a few moredirectories:
bin: Contains commands to start/stop the server (You can also do alot of other stuff like deploy/undeploy, but we will just talk about
things needed to get you started) lib: Contains several jar files (you only need of few of these jars in
your classpath to do EJB development)
apps: Once you create your EJB's and jar them up, you can place yourjar file in this directory and start the server. The server willautomatically deploy all the EJB's contained in this JAR.
conf: Th
is directory contains all th
e configuration files. Alth
ough
youmay not see any file except for a README.txt file right now, but afteryou start the server, the required configuration files will beautomatically created. It is highly recommeded to read theREADME.txt file under this directory
logs: Contains log files.
-
8/9/2019 Enterprise Java Beans.ppt
48/56
Setup your development environment
Create a working directory
Assuming you are in your home directory,
create a directory named projects
c:\> md projects
Go to the projects directory
c:\> cd projects
We will do all our work in this directory.
-
8/9/2019 Enterprise Java Beans.ppt
49/56
Next .
Install Java
Download and install Java (version 5 or higher).Also set it up so that you can run the java and
javac commands from any directory
Set OPENEJB_HOME We will setup this variable to refer to the openejb
install location.
OPENEJB_HOME=directory of open EJB
Write an EJB Whatever files you create should be placed under
the projects directory
-
8/9/2019 Enterprise Java Beans.ppt
50/56
Create the Remote Interface
Using your favorite editor, create a file
named Hello.java (shown below)
package org.acme;
import javax.ejb.Remote;
@Remote
public interface Hello{
public String sayHello();
}
-
8/9/2019 Enterprise Java Beans.ppt
51/56
Create the Bean Class
Now create a file named HelloBean.java
package org.acme;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements Hello {
public String sayHello() {return "HelloWorld!!!!";
}
}
-
8/9/2019 Enterprise Java Beans.ppt
52/56
Compile the source code
Since we have imported the javax.ejb.Stateless andjavax.ejb.Remote annotations, we need these inour classpath to compile our source code. Theseannotations can be found in the
OPENEJB_HOME/lib/javaee-5.0-1.jar.
Compile our source (make sure you are in theprojects directory)c:\> javac -cp $OPENEJB_HOME/lib/javaee-5.0-1.jar -d . *.java
The above will compile all the .java files and alsocreate the required packages.
-
8/9/2019 Enterprise Java Beans.ppt
53/56
Package the EJB
To package the EJB into a JAR, run the following command while youare in the projects directory
C:\> jar cvfhello.jar org
The above command will package everything under the org directory(including the org directory itself) into a jar file named hello.jar. Below
is the output from running the above command:added manifest
adding: org/(in = 0) (out= 0)(stored 0%)
adding: org/acme/(in = 0) (out= 0)(stored 0%)
adding: org/acme/Hello.class(in = 203) (out= 168)(deflated 17%)
adding: org/acme/HelloBean.class(in = 383) (out= 275)(deflated 28%)
-
8/9/2019 Enterprise Java Beans.ppt
54/56
-
8/9/2019 Enterprise Java Beans.ppt
55/56
Next .
Compile HelloClient.java
Run the following command:
C:\> javac -d . HelloClient.javaStart the Server
Go to the OpenEJB install directory (i.e.
OPENEJB_HOME) and run the following
command:
start
-
8/9/2019 Enterprise Java Beans.ppt
56/56
Next ..
Deploy the EJB -While you are in the projects directory,run the following command:
C:\> deploy hello.jar
Run the Client -While you are in the projects directory,run the following command to run the client:
C:\> java -cp $OPENEJB_HOME/lib/openejb-client-3.0.jar:$OPENEJB_HOME/lib/javaee-5.0-1.jar:.org.acme.HelloClient
The above should give you the following output:
HelloWorld!!!!