0% found this document useful (0 votes)
8 views23 pages

Understanding Enterprise JavaBeans Types

The document provides an overview of Enterprise JavaBeans (EJB), detailing the types of EJB components, including Entity and Session beans, and their respective characteristics. It also outlines the lifecycle of stateless session beans, the deployment process, and the use of JNDI for accessing EJB methods. Additionally, it emphasizes the structure of a J2EE application and the steps required for deploying enterprise beans.

Uploaded by

Rajendran Praj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Understanding Enterprise JavaBeans Types

The document provides an overview of Enterprise JavaBeans (EJB), detailing the types of EJB components, including Entity and Session beans, and their respective characteristics. It also outlines the lifecycle of stateless session beans, the deployment process, and the use of JNDI for accessing EJB methods. Additionally, it emphasizes the structure of a J2EE application and the steps required for deploying enterprise beans.

Uploaded by

Rajendran Praj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

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 [Link] and [Link] 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:
[Link] [Link] [Link] [Link]

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:
[Link] [Link] [Link]

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:
[Link] containing code for the remote interface [Link] containing code for the home interface [Link] containing code for the EJB class

12

//The remote interface

import [Link];
import [Link]; 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 [Link]() 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 [Link]() method is used to cast the object returned by the lookup() method to the home interface

type

23

Common questions

Powered by AI

Separating the EJB Remote Interface, Home Interface, and the EJB Class in an enterprise application provides clear architectural demarcation, enhancing modularity, scalability, and maintainability. The Remote Interface defines accessible business methods, ensuring encapsulated interaction points for clients. The Home Interface manages lifecycle operations such as creation or removal of bean instances, focusing on bean management rather than business logic. The EJB Class implements the methods in the remote interface, containing the actual business logic. This separation of concerns facilitates easier testing, upgrades, and scalability by allowing developers to change or enhance one aspect of the application architecture without affecting others, adhering to best practices in software engineering .

To locate an Enterprise Bean in a J2EE application using JNDI, the process involves: (1) Creating a JNDI naming context using the 'InitialContext' object, (2) Using the 'lookup()' method of the InitialContext class to find the JNDI name specified at deployment, and (3) Casting the object returned by 'lookup()' to a specific EJB Home interface using 'PortableRemoteObject.narrow()'. This process is crucial because it allows clients to interact with EJB components, enabling them to use the distributed objects and services provided by the beans, thus facilitating remote method invocation and resource access .

JNDI plays a critical role in distributed enterprise applications by providing a directory service that allows applications to locate network services and distributed components, such as EJB home interfaces, across multiple systems. It offers a standard way to look up resources and services within a network, using a naming system that simplifies accessibility to remote objects and services. This facilitation is crucial because it abstracts the complexity of network interactions, enabling scalable and flexible deployment of applications in diverse network environments. JNDI's ability to manage distributed resources efficiently directly supports the development of robust networked applications, leading to higher resilience and performance in enterprise-level systems .

The deployment of a J2EE application involves several steps: (1) Starting the J2EE server, (2) Using the deployment tool to create a J2EE application, (3) Assembling the application by packaging enterprise beans, web components, and application clients into .jar and .war files, which are then combined into an .ear file, (4) Deploying the application to the J2EE server. The deployment descriptor, an XML file, plays a vital role by containing metadata about the application, including the components' configurations, access control details, and references to JNDI resources, which guide the application server on how to configure runtime settings for the application .

A Stateless Session Bean would be appropriate for a currency converter application because such applications typically perform discrete, self-contained operations that do not require the retention of data between method calls. Operations like currency conversion are inherently stateless, requiring only input for the amount, currency type, and possibly other contextual parameters each time the conversion is performed. This aligns perfectly with the properties of Stateless Session Beans, which are designed to handle single, transactional operations without preserving state across requests .

The Home Interface in an EJB application defines the methods responsible for creating and finding instances of EJB components. It includes methods like 'create()', which allow clients to obtain references to the EJB objects. Conversely, the Remote Interface outlines the business methods that can be invoked by the client on the EJB. It represents the functionality the bean provides, allowing client interactions specific to the business logic implemented within the bean. While the Home Interface manages the lifecycle aspects of an EJB, the Remote Interface handles client interactions with the bean's business logic .

The 'ejbCreate()' method is crucial in the lifecycle of a Session Bean as it triggers the creation of an instance of the bean, initializing any bean-specific resources or state required before the bean becomes ready to handle client requests. This method is invoked immediately after the bean instance is created and before any client interactions occur, ensuring that the bean is correctly set up within the container environment. Its role is to perform any initialization tasks necessary for the bean's intended operations, aligning it with the overall architecture and resource management strategies of the application .

Entity Beans are vital in enterprise applications as they handle persistence, allowing data to persist beyond the duration of a session and be shared across multiple clients. They can manage data storage either through Bean-managed persistence, where the developer explicitly writes code to connect and interact with the database, or Container-managed persistence, where the EJB container automatically handles all database interactions during specified lifecycle events. This functionality allows organizations to maintain robust and consistent data access, supporting the needs of distributed and data-intensive applications .

Stateful Session Beans maintain state across multiple method calls and transactions, allowing them to hold onto data between calls, which is stored in instance variables. In contrast, Stateless Session Beans do not maintain any state between client interactions; each method call is independent, with no data retained beyond the duration of the call. The lifecycle of a Stateful Session Bean involves states such as 'Does Not Exist', 'Ready', and potentially 'Passivated' to manage state preservation. A Stateless Session Bean, however, does not undergo passivation since it is either in the 'Does Not Exist' or 'Ready' state .

In the provided remote interface code, the interface 'myRemote' does not declare any RemoteException thrown by its method 'methodOne'. In EJB, all methods in a Remote Interface must declare a 'RemoteException', as remote method calls can result in issues like network errors. This is a requirement for ensuring robustness in handling various network-related failures, which is fundamental for distributed systems .

You might also like