Creating an Enterprise JavaBean ©NIIT Enterprise JavaBeans/Lesson 1/Slide 1 of 33 Objectives In this lesson, you will learn to: Identify the EJB components Create an Enterprise JavaBean
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 1 of 33
ObjectivesIn this lesson, you will learn to:
Identify the EJB components
Create an Enterprise JavaBean
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 2 of 33
Introduction In a multitier architecture, the client contacts a layer called
middleware The middleware instantiates and manages the server
objects The low-level services such as thread handling, security,
and transaction management are handled by the middleware
Some examples of middleware are ORB, Transaction Processing Monitor (TPM), and Component Transaction Monitor (CTM)
CTM is a hybrid of TPM and ORB Enterprise JavaBeans (EJB) is a Sun Microsystem venture
into server-side component architecture
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 3 of 33
Introduction Enterprise JavaBeans are interprocess components unlike
JavaBeans, which are intraprocess components
EJB is a specification for creating server-side components
EJB Enables and simplifies the task of creating distributed objects
EJB components are Server-side components written using Java
EJB components implement the business logic only
EJB can maintain state information across various method calls
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 4 of 33
Enterprise JavaBeans Component ArchitectureConsists of:
EJB server
EJB container
Enterprise bean
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 5 of 33
Enterprise JavaBeans Component Architecture (Contd.)EJB component architecture
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 6 of 33
Enterprise JavaBeans Component Architecture (Contd.)EJB Server:
Contains the EJB container
Provides the following services to the container:
Instance passivation
Instance pooling
Database connection pooling
Precached instances
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 7 of 33
Enterprise JavaBeans Component Architecture (Contd.)EJB Container:
Contains the enterprise beans
Clients communicate with the enterprise bean through remote and home interfaces
Provides the following services:
Security
Transaction management
Persistence
Life cycle management
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 8 of 33
Enterprise JavaBeans Component Architecture (Contd.)Enterprise Bean:Consists of methods that implement the business logicAre of two types:
Entity bean: Are enterprise beans that persist across multiple
sessions and multiple clients Are of two types:
Bean-managed persistence Container-managed persistence
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 9 of 33
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
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 10 of 33
Just a Minute… Jane is creating an application for a departmental
store. She needs to create an application that would accept the price and would calculate the sales tax. Identify the type of enterprise bean that can be created.
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 11 of 33
Life Cycle of a Stateless Session Bean Life cycle of stateless session bean:
Does Not Exist
Method - Ready
newInstance()setSessionContext()ejbCreate()
ejbRemove()
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 12 of 33
Problem Statement 1.D.1 Earnest bank wants the development team to develop
a calculator component to convert Dollars to Rupees. Justify the choice of EJB to create the component and specify the code that has to be written.
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 13 of 33
Task List Identify the mechanism
Identify the type of enterprise bean to be created
Identify the code to be written
Write the code for the remote interface and save the file
Write the code for the home interface and save the file
Write the code for the enterprise bean class and save the file
Compile source files
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 14 of 33
Task 1: Identify the mechanism EJB is the appropriate technology since:
EJB components automatically handle system level services
Enterprise bean implements the business logic only
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 15 of 33
Task 2: Identify the type of enterprise bean to be createdCreate a stateless session bean for the banking
application
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 16 of 33
Task 3: Identify the code to be written The enterprise bean consists of:
Remote interface
Home interface
EJB class
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 17 of 33
Task 3: Identify the code to be written (Contd.)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
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 18 of 33
Task 3: Identify the code to be written (Contd.)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 java.rmi.RemoteException
javax.ejb.CreateException
javax.ejb.EJBHome
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 19 of 33
Task 3: Identify the code to be written (Contd.) Then, create a home interface by extending the EJBHome interface
Finally, define the create() method to create an instance of a particular EJB object
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 20 of 33
Task 3: Identify the code to be written (Contd.)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.SessionBean
javax.ejb.SessionContext
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 21 of 33
Task 3: Identify the code to be written (Contd.) Next, create the EJB class by implementing the SessionBean interface
Then, implement the business method defined in the remote interface
Finally, write the ejbCreate(), ejbRemove(), ejbActivate(), ejbPassivate(), setSessionContext(), and the default implementation for the constructor methods
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 22 of 33
Task 3: Identify the code to be written (Contd.) To create the enterprise bean called Calculator,
create the following files:
Calculator.java containing code for the remote interface
CalculatorHome.java containing code for the home interface
CalculatorEJB.java containing code for the EJB class
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 23 of 33
Just a Minute…//The remote interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface myRemote extends EJBObject
{
public int mymethod(int i);
}
What is wrong in the above code?
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 24 of 33
Task 4: Write the code for the remote interface and save the file
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 25 of 33
Task 5: Write the code for the home interface and save the file
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 26 of 33
Task 6: Write the code for the enterprise bean class and save the file
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 27 of 33
Task 7: Compile source files
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 28 of 33
Problem Statement 1.P.1 Earnest Bank wants an application that would help
their customers to view the rates of various currencies. Create an application to display the USD, Euro, GBP, and AED rates.
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 29 of 33
SummaryIn this lesson, you learned that:
Middleware handles all low-level services such as thread handling, security, and transaction management
ORB, TPM, and CTM are examples of middleware
EJB is a specification for creating server-side enterprise components that enables and simplifies the task of creating distributed objects
The EJB components are:
EJB server, which contains the EJB container
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 30 of 33
Summary (Contd.) EJB container, which contains the enterprise
beans
Enterprise bean, which contains methods that implement business logic
EJB server provides some low-level services, such as network connectivity, threads, memory, and database connections to the container
Instance passivation is a process where the EJB container temporarily swaps out a bean if it requires a resource
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 31 of 33
Summary (Contd.) Clients communicate to the enterprise bean through the
remote and home interfaces provided by the EJB container
Access control list, which is a list of user groups or persons authorized to access a particular functionality, is used to ensure security
Persistence means that a state of an object is permanently stored in a data store, such as database
The two types of enterprise beans are:
Entity beans
Session beans
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 32 of 33
Summary (Contd.) Entity beans are enterprise beans that persist across
multiple sessions and multiple clients
The two types of entity beans are:
Bean-managed persistence
Container-managed persistence
Session beans perform business tasks without having a persistent storage mechanism, such as database
The two types of session beans are:
Stateful session bean
Stateless session bean
Creating an Enterprise JavaBean
©NIIT Enterprise JavaBeans/Lesson 1/Slide 33 of 33
Summary (Contd.) Remote interface defines all the business methods of
the enterprise bean
Home interface defines methods that create and look up for EJB components
The EJB class implements the business methods defined in the remote interface