Adv Java 19
-
Upload
rajendran-praj -
Category
Documents
-
view
220 -
download
0
Transcript of Adv Java 19
-
8/4/2019 Adv Java 19
1/23
Enterprise Java Beans
Exploring different types of EJB
-
8/4/2019 Adv Java 19
2/23
2
Are of two types: Entity & Session
Entity bean:
Are enterprise beans that persist across multiplesessions and multiple clients
Are of two types:
Bean-managed persistence
Container-managed persistence
Enterprise JavaBeans Components
Enterprise Bean:
-
8/4/2019 Adv Java 19
3/23
3
Session bean:
Perform business tasks without having a persistent storage
mechanism
Are of two types:
Stateful session bean
Stateless session bean
Enterprise JavaBeans Component Architecture (Contd.)
-
8/4/2019 Adv Java 19
4/23
4
EJB Types
Session Beans
Stateless Session Beans
Dont maintain state between calls
Statefull Session Beans
Store state between calls
Entity Beans Used for persistence
-
8/4/2019 Adv Java 19
5/23
5
Problem
Richard is creating an application for a Super Mall. He needsto create an application that would accept the price of Itemand would calculate the tax. Find out the type of enterprise
bean to be created.
-
8/4/2019 Adv Java 19
6/23
6
Life Cycle of a Stateless Session Bean
Life cycle of stateless session bean:
Does Not Exist
Method - Ready
newInstance()
setSessionContext()
ejbCreate()
ejbRemove()
Session Beans
-
8/4/2019 Adv Java 19
7/237
RBS Bank wants an Currency Converter tool to convert Dollars to
Rupees. Identify the choice of EJB to create the component and
specify the code.
Situation
-
8/4/2019 Adv Java 19
8/238
EJB is the appropriate technology to solve the problem:
EJB components automatically handle system level services
Enterprise bean implements the business logic only
The enterprise bean consists of:
Remote interface
Home interface
EJB class
Create a stateless session bean for the above said converter
application
Solution
-
8/4/2019 Adv Java 19
9/239
Defines all the business methods of the enterprise bean
Steps to write the remote interface:
Import the javax.ejb.EJBObject and
java.rmi.RemoteException interfaces
Then, create a remote interface by extending the EJBObject
interface
Finally, define all the business methods that will be implemented in
the EJB class
Remote Interface:
-
8/4/2019 Adv Java 19
10/2310
Defines method that allow EJB clients to create and find EJB
components
Steps to write the home interface:
Import the following interfaces:
java.io.Serializable java.rmi.RemoteException
javax.ejb.CreateException javax.ejb.EJBHome
Then, create a home interface by extending the EJBHome interface
define the create() method to create an instance of a particular
EJB object
Then, create a home interface by extending the EJBHome interfaceAnd finally define the create() method to create an instance of a particularEJB object
Home Interface:
-
8/4/2019 Adv Java 19
11/2311
Implements all the business methods declared in the remote
interface
Steps to write the EJB class:
Import the following interfaces:
java.rmi.RemoteException javax.ejb.SessionBean
javax.ejb.SessionContext
Then, create the EJB class by implementing the SessionBean interface.Then, implement the business method defined in the remote interface.write the ejbCreate(), ejbRemove(), ejbActivate(), ejbPassivate(),setSessionContext(), and the default implementation for the constructormethods
EJB Class
-
8/4/2019 Adv Java 19
12/2312
To create the enterprise bean called CurrencyConverter, create the
following files:
Converter.java containing code for the remote interface
ConverterHome.java containing code for the home interface
ConverterEJB.java containing code for the EJB class
-
8/4/2019 Adv Java 19
13/2313
//The remote interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface myRemote extends EJBObject
{
public int methodOne(int i);
}
Find out the mistake in the above code?
-
8/4/2019 Adv Java 19
14/2314
Start the J2EE server
Start the deploytool
Create a J2EE application
Package the enterprise bean
Deploy the J2EE application
Deployment process
-
8/4/2019 Adv Java 19
15/23
15
Start the J2EE server
Type the command:
J2ee verbose
Type the command:
deploytool
Package the enterprise bean
Use the New Enterprise Bean Wizard of the Application Deployment
Tool
-
8/4/2019 Adv Java 19
16/23
16
A J2EE application is assembled from three
components:
J2EE application clients
Web components
Enterprise beans
J2EE Application Components
-
8/4/2019 Adv Java 19
17/23
17
Is an XML file
Contains the following information:
The access control list (ACL)
Name of the EJB class
Name of the home interface
Name of the remote interface
A list of container-managed fields if the enterprisebean is an entity bean
A value specifying if the enterprise bean is stateful orstateless, in case of a session bean
Deployment Descriptor
-
8/4/2019 Adv Java 19
18/23
18
Is a naming service called Java Naming and DirectoryInterface
Is used to access the enterprise bean methods
Is a standard extension to the Java platform that provides
multiple naming and directory services
Is used to locate and search for distributed objects
JNDI
-
8/4/2019 Adv Java 19
19/23
19
Steps to locate the home interface:
First, create a JNDI Naming Context
Next, use the InitialContext class to locate the JNDI name
specified when the J2EE application was deployed. The lookup()method is used to lookup for the JNDI name
Finally, the object returned by the lookup() method must be cast to a
variable by using the PortableRemoteObject.narrow() method
Client Program
-
8/4/2019 Adv Java 19
20/23
20
To create an instance of enterprise bean:
The EJB client must invoke the create() method of the home
interface
The remote interface defines the business methods implemented in
the EJB class
The client invokes these methods by using the remote interface object
returned by the create() method
Set the classpath to the remote application client.Save and compile the application client
-
8/4/2019 Adv Java 19
21/23
21
A J2EE application is assembled from three components: J2EEapplication clients, enterprise beans, and Web components
The Web component files, which are .war files and the enterprisebean and application client files, which are .jar files are assembledinto a J2EE application, which has an .ear extension
Deployment descriptor is an XML file that contains informationabout deployment of enterprise bean
JNDI is a standard Java extension that provides multiple namingand directory services
A naming service provides a mechanism for locating distributed
objects
Summary
-
8/4/2019 Adv Java 19
22/23
22
A directory service organizes distributed objects and otherresources, such as files into hierarchical structures
Client uses JNDI to initiate a connection to an EJB server and tolocate a specific EJB Home
The steps to deploy an enterprise bean are:
Start the J2EE server
Start the deployment tool
Create a J2EE application and assemble the remote and homeinterfaces and enterprise bean class files into a .jar file
Deploy the J2EE application to the J2EE server
Summary (Contd.)
-
8/4/2019 Adv Java 19
23/23
23
The target server is the name of the host where the J2EE server is
running
The Return Client Jar file is used by the client program to locate the
server
The lookup() method of the InitialContext class is used to
locate the JNDI name
The PortableRemoteObject.narrow() method is used to cast
the object returned by the lookup() method to the home interface
type
Summary (Contd.)