Professional Documents
Culture Documents
A Component Technology
EJB
1. Learning objectives
1. Component Technology characteristics
2. Standard Infrastructure needs
3. Application Servers
4. Containers
5. J2EE Architecture and its components
6. EJB Model, Overview & JNDI
7. EJB Specifications, Roles
8. EJB Design Approaches, Architecture A STAR or a
different color
9. Types of Beans represents
important
10. EJB Interfaces & Session Contexts points to NOTE
Properties Reusability
Operations Shareable
Events Distributable
Self description
6. Component P O E
Properties
Operations
Events
7. Deployable Components
• Hardware
• The underlying operating system
• Their Application Server
• The network protocol they use
8. Reusable Components
Remote
EJB Container
CLIENTS
EJB
Local
CLIENTS Remote
Possible CLIENTS
Remote
CLIENTS
Remote
CLIENTS
11. SelfContained Components
• Directory Services
• Distributed transaction management
• Security management
• Concurrent access management
• Persistence management
• Resource pooling (e.g. DB connections)
• Administration interface
• Load Balancing
• Fault tolerance
13. Applicatoin Servers
App Server
EJB Container
EJB
EJB
EJB
CLIENT
Pool
EJB EJB DB
Network
CLIENT EJB
15. Directory of Application Servers
Application Servers provide clients access to software or
other applications that run only on the server
App Server
EJB Container
EJB
EJB EJB
Container
Network intercepts
STUB
requests
CLIENT
Container knows what to do, as you describe everything in a special descriptor file
17. J2EE Overview
Deployment
Descriptors (DDs)
Applet RMI/
Web Container EJB Container
IIOP
Container HTTP/S
JSP Servlet EJB
Applet
JSP Servlet ENGINE
JAX- J J W J Java Mgmt J J JAX- J J W J Java Mgmt J J
J C J C
J2SE RPC A A E M Mail N D RPC A A E M Mail N D
T O T O
X C B S JMX A D N B X C B S D B
SAAJ JAF SAAJ JAF JMX A N
R C S I C R C S I C
3 TIER 4 TIER
Client
RMI/
EIS
JAX- J W Mgmt J IIOP
J C DAO
RPC A E D
M O
X B JMX B DB
SAAJ S N
R S C
J2SE 2 TIER
MainFrames
EJB Client
EJB Impl
LocalHome Local create timeout
Client Process create invoke remove passivate
remove find activate
find invoke load
Home store
STUB STUB
create
remove
EJB find
Client Remote Network
EJB
Delegate
Delegate
Delegate Pool
invoke Delegate
J2EE EJB Container/Server
21. When to use EJB
Deployment Specialist
deploys apps and understands architecture issues
deploys system
supplies tools builds application
Application Assembler
Tools Provider Deployer System Administrator
maintains deployment
javax.ejb.EnterpriseBean interface
extends
we have written
Container implements/autogenerates code for the Interfaces
We will write only this Container manages TRANSACTIONS, PERSISTENCE, SECURITY & POOLING
27. EJB Flow Chart
Start C
JNDI Server returns ref of Home Stub 2 goes to the JNDI Server
7 and gets the EJB Object
C End
28. Client View of EJB
ENTITY BEANS are like NOUNS as they represent data or data related logic.
App Server
EJB Container
EJB Home or e.g
EJB Local Home Session Bean •Bank teller
instance •Credit Card authorization
EJB Object or
EJB Local Object Entity Bean e.g
Application RMI/ instance •Bank account balance
Client IIOP
•Purchase order
SESSION BEANS are like VERBS as they represent action or business process related logic.
If all the processing is done in the same Application server then we use
EJB Local(Object & Home) interfaces. Using local interfaces are optional.
Local interfaces pass by REFERENCE and EJB (Object/Home) pass by VALUE.
Using local interfaces avoids stubs, skeletons, network and so it is faster.
29. EJB Client
EJB clients
EJB implementation
ejbRemove()
setSessionContext()
ejbCreate()
Pool of equivalent
method ready instances
Client called another
Transactional business method
on the EJB Object. Transactional
Business Method
37. Stateful Session EJBs View
Stateful Session
EJB Client
LocalHome Local Logic
Client Process
State
Home
EJB input
Remote Network
Client EJB Pool output
The create method takes arguments The create method does not take arguments
e.g. create(String id) , create(int I , String id) e.g create()
There can be one or more arguments in a create method There can be no arguments in a create method
e.g e.g
An EJB that unzips 100 bytes of data An EJB that books a flight and rents a car at a travel agent’s
An EJB that checks to see if a stock symbol is valid web site.
App Server
EJB Container
CLIENT
EJB DB
Network
CLIENT EJB
44. Life Cycle of BMP Entity Bean
Each method call is an
Invocation from the
container to the Bean
Container decided it
Container decided it Does not exiist doesn’t need the
needs another
Entity Bean instance
Entity Bean insinstance
anymore
newInstance() unsetEntityContext()
Container decided it
Container decided it does not exiist doesn’t need the
needs another
Entity Bean instance
Entity Bean in
anymore
newInstance() unsetEntityContext()
The developer takes care of handling persistence Vendor takes care of everything by using O-R or OODB
mappings using metadata.
BMP uses hard coded queries so we can optimize our A developer cannot optimize performance as the vedor
queries takes care of it
We should start developing CMP beans, unless we require some kind of special bean, like multi-tables, that
cannot be completely realized with a single bean. Then when we realize that we need something more or that
we prefer handling the persistence (performance issue are the most common reason), we can change the bean
from a CMP to a BMP
Asynchronous messaging
Container decided it
needs another Does not exiist
Message DrivenBean
Instance
newInstance()
ejbRemove()
setMessageDrivenContext()
ejbCreate()
Pooled
onMessage()
51. Deployment
Declaring without programming helps the application assembler to change the XML file easily.
BEAN Provider declares components’ middleware service requirements in a DEPLOYMENT DESCRIPTOR File.
Bean Provider describes how the Container should perform the LifeCycle Management, Persistence, Transaction and Security.
52. Other interfaces and classes
Local interfaces are used for access within server context
javax.ejb.EJBLocalHome E.g.:
•Inter component calls
javax.ejb.EJBLocalObject •Servlet invocation
2
Write the Deployment descriptor Start your EJB container and
6 confirm that it has loaded
the EJB Jar file.
Compile all the STEP 1 files into 3
.class files
Connect to your EJB by writing a test
7 Client .java file compile it and run it .
Using the Jar utility create an EJB Jar
file containing STEP 2 & STEP 3 files 4
End
C
Please download the trial Application Server software from the links provided on the Reference slide at the end.
55. Hello World Object Model using RMI
We will apply the previous slide flow chart procedure to make up our first EJB
example
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import java.io.Serializable;
import java.rmi.RemoteException; Importing the required files
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface HelloHome extends javax.ejb.EJBHome
Our interface HelloHome is extending EJBHome means it has all the behaviour of EJBHome
{
Hello create() throws java.rmi.RemoteException,
javax.ejb.CreateException; Interface methods should end with a semicolon ;
This method creates/manufactures the EJBObject and returns it . This create() method corresponds to the
ejbCreate() method in HelloBean.
As for every Interface we need implementation,Note
so Container will implement
In Home Interface we are(autogenerate code) forRemote
throwing 2 exceptions this Home
andInterface
Create . for us, which is the EJBHome Object
}
NOTE : Rules of Interfaces states that there should be no implementation so you will find only method signatures.
58. HelloLocalHome Interface
So now lets code the HelloLocalHome Interface
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import javax.ejb.EJBLocalHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.util.Collection;
Our interface HelloLocalHome is extending EJBLocalHome means it has all the behaviour of EJBLocalHome
Clients interact with the EJB Object through this Remote interface that is why we need to write this interface.
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import java.util.*;
import javax.ejb.EJBObject; Importing the required files
import java.rmi.RemoteException;
public interface Hello extends javax.ejb.EJBObject
{ Our interface Hello is extending EJBObject means it has all the behaviour of EJBObject
Clients interact with the EJB Object through this Local interface when there is no network involved..
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import javax.ejb.EJBLocalObject;
public interface HelloLocal extends javax.ejb.EJBLocalObject
Our interface Hello is extending EJBLocalObject means it has all the behaviour of EJBLocalObject
{
public String hello() ; Interface methods should end with a semicolon ;
As our requirement wants a business logic to greet Hello World a single method would be sufficient .
} As Local interface is used to interact with the Bean we need to mirror all the method signatures what
are there in a Bean class so that is why we are writing only one method in this Local interface.
Note As there is no remote calls no Remote Exception is thrown
As for every Interface we need implementation, so Container will implement (autogenerate code) for this Local Interface for us, which is the EJB Local Object
NOTE : Rules of Interfaces states that there should be no implementation so you will find only method signatures.
61. The Bean Class
So now lets code the actual EJB Class
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import java.util.*;
Importing the required files
import javax.ejb.*;
public class HelloBean implements javax.ejb.SessionBean
NOTE : Note
If it is: Entity
this isBean
a class
weand
will not
implement
an Interface
javax.ejb.EntityBean
like the other two
and
(Home
if it is /Message
Remote).driven
This isbean
implementing
we will implement
SessionBean
javax.ejb.MessagedrivenBean
so it is a Session Bean
{ As per the below note in red we need to write all the methods what are in javax.ejb.SessionBean .
As a Bean provider we need to specify the Middleware needs through this descriptor.
<!DOCTYPE ejb-jar PUBLIC “-//Sun Microsystems, Inc. // DTD Enterprise JavaBeans 2.0 //
EN “ “ http://java.sun.com.dtd/ejb-jar_2_0.dtd”> XML Standard requirement
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name> Hello </ejb-name> The nickname for this particular Bean
<home>examples.HelloHome</home> The fully qualified name of the Home Interface
<remote>examples.Hello</remote> The fully qualified name of the Remote Interface
<local-home>examples.HelloLocalHome</local-home>
The fully qualified name of the Local HomeInterface
<local>examples.HelloLocal</local> The fully qualified name of the Local Interface
<ejb-class>examples.HelloBean</ejb-class> The fully qualified name of the EJB Class
<session-type>Stateless</session-type> Whether the session bean is stateful or stateless
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
63. The Client
So now lets code our Client to access our simple stateless session bean.
package examples; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import javax.naming.Context;
import javax.naming.InitialContext; importing the necessary files
import java.util.Properties;
public class HelloClient { As there is static key word this main method executes first before any other method in that class
NOTE : To call Local Interface you need not cast with the PortableRemoteObject. So the
block in red will be changed to
Object obj = ctx.lookup(“java:comp/env/HelloLocalHome”); ( to get a ref to LocalHome Object)
HelloLocalHome home = (HelloLocalHome)obj;
64. EJB Jar File
The folder structure within the Ejb-jar file is
META-INF/MANIFEST.MF
META-INF/ejb-jar.xml
examples/HelloBean.class
examples/HelloLocalHome.class
examples/HelloLocal.class
examples/Hello.class
Properties Remote/Local
File Interface
Deployment Jar
Descriptor Manifest
DEPLOYED
Jar file is a .ZIP compression format . META-MF file is a listing file automatically created by the Jar utility.
Please read the Deployment instructions provided by the Application server as it is Vendor specific
65. EJB Application Assembler
Deployment
.JAR .WAR
Jar Descriptor
file
Deployment
Tool
JAR ARC HIEVE (.jar)
WEB ARCHIEVE (.war)
EJB-JAR.XML
REMOTE WEB.XML
HOME Enterprise Archive HTML
EJB JSP
(.EAR Files) Servlets
(Application.XML)
EXAMPLE 2
STATEFUL BEAN
66. Stateful Session Remote Interface
package declaration should be in the beginning
package samples.ejb.stateful.simple.ejb;
import java.util.*;
import javax.ejb.EJBObject; Importing the required classes
import java.rmi.RemoteException;
import samples.ejb.stateful.simple.tools.BookException;
Remote Cart is extending EJBObject means Cart has all the behaviour of it
public interface Cart extends EJBObject Our bean class CartBean methods are mirrored here
{
import java.io.Serializable;
import java.rmi.RemoteException;
Importing the required classes
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
In a Stateful Bean you can have more than one create method taking arguments
Cart create(String person) throws RemoteException, CreateException;
import java.util.*;
import javax.ejb.*;
import samples.ejb.stateful.simple.tools.BookException; Importing the required classes
import samples.ejb.stateful.simple.tools.IdVerifier;
customerId = "0";
contents = new Vector(); as this is the Bean class we will implement the business logic here
}
NOTE : AS we are implementing and not extending we need to write all the methods that are there in the Sessionbean
69. Stateful Session Bean Class
public void ejbCreate(String person, String id) throws CreateException {
Werepresents
if (person == null) { This also defined this
themethod
Home in the Cart remote
create(String interface
person, String id) method we defined
throw new CreateException("Null person not allowed.");
} else {
customerName = person;
}
The new operator is instantiating the instance of a class
IdVerifier idChecker = new IdVerifier();
if (idChecker.validate(id)) {
customerId = id; We are calling the validate(id) method from the class ID Verifier
} else {
throw new CreateException("Invalid id: " + id);
}
// This block is a supporting business method written in idVerifier class
public void ejbRemove() {} We need to write these methods as we are implementing SessionBean
Rules of implement states we need to implement all methods that are there in the implemented interface
71. Stateful Deployment Descriptor
THERE ARE MANY
TOOLS WHICH
<?xml version="1.0" encoding="UTF-8"?> CREATES THE
<!-- DESCRIPTOR
Copyright 2004 Sun Microsystems, Inc. All rights reserved. FOR US
SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
-->
<ejb-jar version="2.1" 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">
<display-name>CartJAR</display-name>
<enterprise-beans>
required by any XML document
<session>
<display-name>CartEJB</display-name>
The nickname for this particular Bean
<ejb-name>CartEJB</ejb-name>
<home>samples.ejb.stateful.simple.ejb.CartHome</home> The fully qualified name of the Home Interface
<remote>samples.ejb.stateful.simple.ejb.Cart</remote> The fully qualified name of the Remote Interface
<ejb-class>samples.ejb.stateful.simple.ejb.CartBean</ejb-class> The fully qualified name of the EJB Class
<session-type>Stateful</session-type>
Bean type is mentioned here
<transaction-type>Container</transaction-type>
<security-identity>
<use-caller-identity/>
</security-identity>
</session> Closing session and ejb
</enterprise-beans>
72. Stateful Deployment Descriptor
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>CartEJB</ejb-name>
<method-intf>Remote</method-intf> Passing Bean class methods and parameter info
<method-name>getContents</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>CartEJB</ejb-name>
<method-intf>Remote</method-intf>
<method-name>removeBook</method-name> Passing Bean class methods and parameter info
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</method>
<trans-attribute>NotSupported</trans-attribute>
</container-transaction>
73. Stateful Deployment Descriptor
<container-transaction>
<method>
<ejb-name>CartEJB</ejb-name>
<method-intf>Remote</method-intf>
<method-name>addBook</method-name> Passing Bean class methods and parameter info
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
74. Stateful Session Client
package samples.ejb.stateful.simple.ejb;
package declaration should be in the beginning
import java.util.*;
import javax.naming.Context;
Importing the required classes
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
shoppingCart.removeBook("Alice in Wonderland");
shoppingCart.remove(); Removing a particular book using the bean class method removeBook through
remote interface Cart –as we defined it this way
System.exit(0); Cart shoppingCart = home.create("Duke DeEarl","123");
/**
* Default constructor.
*/
public BookException() {
}
/**
* Constructor with a <code>String<code> as a parameter.
* @param msg message, describing the exception.
*/
public BookException(String msg) {
super(msg);
}
}
77. ID Verifier
package samples.ejb.stateful.simple.tools;
/**
* Default constructor.
*/
public IdVerifier() {
}
ActiveX
Uses Microsoft’s architecture for determining how clients’
invocation requests are sent to components. DCOM used as
underlying transport mechanism
CORBA
ORB makes use of GIOP and other protocols to perform IPC
79. Component Comparison
Components provide interfaces in the form of Operations
EJB
exports operations through Remote Interface
ActiveX
exports operations in the same interface
CORBA
exports methods through Interface Repository
80. Component Comparison
Self – Container Components
EJB
Containers inject code to handle transactions, database management,
security, distributability and other customisable services. Containers
can be provided independent of App Server and EJB providers
ActiveX
Transactions, database management, legacy integration have to be coded
into the logic of the component. Security policy can be inherited from
NTLM. Transactions can be aided through MTS
CORBA
CORBAServices have different APIs. CORBAServices can be complicated
to code with. CORBAServices are not supported by all ORBs
81. Component Comparison
Components provide interfaces in the form of Properties.
EJB
Properties are not explicitly exported but can be
mimicked through accessor operations
ActiveX
exports Properties in the same interface
CORBA
Properties not explicitly exported but can be
mimicked through attributes
82. Component Comparison
Components provide interfaces in the form of Events.
EJB
Events supported through JMS with Message
Driven Bean
ActiveX
exports Events in the same interface
CORBA
Events supported through Event Service but cannot
be exported
83. Component Comparison
Component Reusability
EJB
Incorporating the contents of an EJB can be done
in two ways:
• by having the new EJB use the services of an old
EJB as a direct client
or
• by writing an EJB class that inherits from an
existing one
Active X
Incorporating the contents of an ActiveX can be done
in two ways:
CORBA
Every CORBA object has to have its own interface
definition
ActiveX
uses Microsoft's in–process/out-of-process architecture for sharing
multiple ActiveX components and Client components.
CORBA
ORB makes multiple instances of CORBA objects. Clients’ contexts are
managed by ORB but can behave differently based upon vendor’s
Implementation.