Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Adv Java 19

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Enterprise Java Beans

Exploring different types of EJB

Enterprise JavaBeans Components Enterprise Bean:


Are of two types: Entity & Session

Entity bean:
Are enterprise beans that persist across multiple sessions and multiple clients Are of two types:

Bean-managed persistence Container-managed persistence


2

Enterprise JavaBeans Component Architecture (Contd.)

Session bean:
Perform business tasks without having a persistent storage mechanism Are of two types:

Stateful session bean Stateless session bean

EJB Types

Session Beans
Stateless Session Beans
Dont maintain state between calls

Statefull Session Beans Store state between calls

Entity Beans
Used for persistence

Problem
Richard is creating an application for a Super Mall. He needs to create an application that would accept the price of Item and would calculate the tax. Find out the type of enterprise bean to be created.

Session Beans

Life Cycle of a Stateless Session Bean


Life cycle of stateless session bean:
Does Not Exist

newInstance() setSessionContext() ejbCreate()

ejbRemove()

Method - Ready

Situation

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.

Solution

EJB is the appropriate technology to solve the problem:

EJB components automatically handle system level services


Enterprise bean implements the business logic only Create a stateless session bean for the above said converter application The enterprise bean consists of: Remote interface Home interface EJB class

Remote Interface:

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

Home Interface:
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 javax.ejb.CreateException java.rmi.RemoteException 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 interface And finally define the create() method to create an instance of a particular EJB object
10

EJB Class
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.SessionContext javax.ejb.SessionBean

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 constructor methods

11

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

12

//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?

13

Deployment process

Start the J2EE server


Start the deploytool Create a J2EE application

Package the enterprise bean


Deploy the J2EE application

14

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

15

J2EE Application Components

A J2EE application is assembled from three components: J2EE application clients Web components Enterprise beans

16

Deployment Descriptor
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 enterprise bean is an entity bean A value specifying if the enterprise bean is stateful or stateless, in case of a session bean

17

JNDI
Is a naming service called Java Naming and Directory Interface 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

18

Client Program

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

19

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

20

Summary

A J2EE application is assembled from three components: J2EE application clients, enterprise beans, and Web components The Web component files, which are .war files and the enterprise bean and application client files, which are .jar files are assembled into a J2EE application, which has an .ear extension Deployment descriptor is an XML file that contains information about deployment of enterprise bean JNDI is a standard Java extension that provides multiple naming and directory services A naming service provides a mechanism for locating distributed objects

21

Summary (Contd.)

A directory service organizes distributed objects and other resources, such as files into hierarchical structures Client uses JNDI to initiate a connection to an EJB server and to locate 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 home interfaces and enterprise bean class files into a .jar file Deploy the J2EE application to the J2EE server

22

Summary (Contd.)
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

23

You might also like