Adv Java 19
Adv Java 19
Adv Java 19
Entity bean:
Are enterprise beans that persist across multiple sessions and multiple clients Are of two types:
Session bean:
Perform business tasks without having a persistent storage mechanism Are of two types:
EJB Types
Session Beans
Stateless Session Beans
Dont maintain 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
ejbRemove()
Method - Ready
Situation
Solution
Remote Interface:
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 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
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
import javax.ejb.EJBObject;
import java.rmi.RemoteException; public interface myRemote extends EJBObject
{
public int methodOne(int i); }
13
Deployment process
14
15
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
18
Client Program
Finally, the object returned by the lookup() method must be cast to a variable by using the PortableRemoteObject.narrow() method
19
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
type
23