Вы находитесь на странице: 1из 61

What is EJB?

 An EJB is just a collection of Java


classes and XML file, bundled into a
single unit. The Java classes must follow
certain rules and provide certain callback
methods.
 EJB is just a specification. It is not a
product.
 EJBs are reusable components.

Mohammed Habeeb Vulla,Lecturer in dept computers


What is EJB?

 EJB is a widely-adopted server-side


component architecture for J2EE.
 EJB components are designed to
encapsulate business logic, and to
protect the application developer from
having to worry about system level
issues.

Mohammed Habeeb Vulla,Lecturer in dept computers


Contents
 Services provided by EJB container
 Circumstances of EJB component usage
 How an EJB component looks like?
 View of an EJB component by client
programmer and EJB developer
 Mechanisms by which EJB container provides
its services
 Rules an EJB developer must follow and how
to use EJBs in a web architecture?

Mohammed Habeeb Vulla,Lecturer in dept computers


Key features of EJB
technology
 EJB components are server-side components written
entirely in the Java programming language
 EJB components contain business logic only - no
System-level programming
 System-level services (i.e. "plumbing") such as
transactions, security, Life-cycle, threading,
persistence, etc. are automatically managed for the
EJB component by the EJB server

Mohammed Habeeb Vulla,Lecturer in dept computers


Key features of EJB
technology
 EJB architecture is inherently transactional, distributed,
portable, multi-tier, scalable and secure
 EJBcomponents are fully portable across any EJB server
and any OS, work with any client.
 Components are declaratively customized
 There are four major parts to every bean: the home
interface, the remote interface, the implementation class
and the XML deployment descriptor

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB vs JavaBeans

 The JavaBeans architecture is meant to


provide a format for general-purpose
components whereas the EJB architecture
provides a format for encapsulation and
management of business logic.
 JavaBeans has tier of execution at Client
and EJB has at Server (specifically
business logic tier)

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB vs JavaBeans

 In JavaBeans the runtime execution


environment provides services like Java
libraries, Java application etc. The EJB
runtime environment provides services
of Persistence, declarative transactions
and security, connection pooling and
lifecycle services.

Mohammed Habeeb Vulla,Lecturer in dept computers


Varieties of Beans

 Session Beans
➙ Stateful session bean
➙ Stateless session bean
 Entity Beans
➙ With container-managed persistence
➙ With bean-managed persistence
 Message-Driven Beans

Mohammed Habeeb Vulla,Lecturer in dept computers


Why use EJBs in your
design?
 EJB specification provides enterprise-level
services, that is, it provides software
services that are fundamental to an
organization’s purpose.
 EJB’s API was designed to keep the
application programmer from having to
provide systems-level services, so that
they are free to concentrate on business
logic.
Mohammed Habeeb Vulla,Lecturer in dept computers
Why use EJBs in your
design?
A requirement of any of the services
provided by an EJB container like
transactions, scalability, persistence,
security, future growth possibilities is an
appropriate reason to use EJB in the
design of the application.

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB Architecture
Client Application Logic Data

J2EE Application Server


RDBMS

EJB Container

Client Corba
Application
JDBC

JMS
RMI

Mail
Java
JTA
Session Bean
Mail
Entity Bean
Mohammed Habeeb Vulla,Lecturer in dept computers
Roles in EJB
Development
 EJB provider - a person who develops
EJB Components
 EJB Deployer - a person responsible for
deploying EJB’s in EJB server
 Application Server/ EJB Container Vendor
- one who provides application server on
which the application is deployed

Mohammed Habeeb Vulla,Lecturer in dept computers


Roles in EJB
Development
 Application assembler - one who
combine the EJB components with other
software to make a complete application
 System administrator - one who
manages the application after it has
been deployed into a target
environment.

Mohammed Habeeb Vulla,Lecturer in dept computers


Roles in EJB
Development

EJB Application
Deployer
Provider Assembler

App Server/
EJB Container
Provider

System
Administrator

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB Container and its
Services
A container is an execution environment
for a component. The component lives in
the container and the container provides
the services for the component.
 Similarly, a container lives in an
application server, which provides an
execution environment for it and other
containers.

Mohammed Habeeb Vulla,Lecturer in dept computers


Services provided by an EJB
container
 Persistence
➙ Ex: simple connection pooling,
automatic persistence, etc. EJBs created
with application development tools will
encapsulate data access in components.

Mohammed Habeeb Vulla,Lecturer in dept computers


Services provided by an EJB
container
 Declarative transactions
 Data caching
 Declarative Security
 Error Handling
 Component Framework for Business Logic
 Scalability and Fall-Over
 Portability
 Manageability

Mohammed Habeeb Vulla,Lecturer in dept computers


How the Container Provides
Services
There are three basic ideas:
 First, there are clearly defined responsibilities between the
various parts of an application using EJB component
namely the client, the EJB container and the EJB
component. The definition of these responsibilities is
formally known as a contract.
 Second, the services that the container provides are
defined in such a way that they are orthogonal to the
component. In other words, security, persistence,
transactions are separate from the Java files that
implement the business logic of the component.

Mohammed Habeeb Vulla,Lecturer in dept computers


How the Container Provides
Services
 Third, the container interposes on each
and every call to an EJB component so
that it can provide its services. In other
words, the container puts itself between
the client and the component on every
single business method call.

Mohammed Habeeb Vulla,Lecturer in dept computers


Contracts

EJB Container/Application
Server

Enterprise JavaBean

Client

Mohammed Habeeb Vulla,Lecturer in dept computers


Rules for the bean
programmer
 The developer of the EJB component must implement
the business methods in the implementation class
 The bean provider must implement the ejbCreate(),
ejbPostCreate(),ejbRemove() methods and the
ejbFind<METHOD>() methods if the bean is an entity
with bean managed persistence
 The bean provider must define the enterprise bean’s
home and remote interfaces
 For session beans, the bean provider must implement
the container callbacks defined in the
javax.ejb.SessionBean interface

Mohammed Habeeb Vulla,Lecturer in dept computers


Rules for the bean
programmer
 For entity beans, the provider must
implement the container callbacks defined
in the javax.ejb.EntityBean interface
 The bean provider must not use
programming practices that would interfere
with the container’s runtime management
of the enterprise bean instances

Mohammed Habeeb Vulla,Lecturer in dept computers


Interposition : method call to an
EJB Container from a remote
client
 First, the client makes a call on the RMI
stub
 This RMI stub interposes on the method
call in order to marshal parameters and
send the information across the network
 A skeleton on the server side
unmarshals the parameters and delivers
them to the EJB Container

Mohammed Habeeb Vulla,Lecturer in dept computers


Interposition diagram

RMI Container
Client Network RMI
Stub generated EJB
Stub class

Interposition class
Mohammed Habeeb Vulla,Lecturer in dept computers
Interposition : from EJB
Container to EJBs
 The container will examine the security credentials of
the caller of the method
 It will start or join with any required transactions
 It will make any necessary calls to persistence functions
 It will trigger various callbacks to allow the EJB
Component to acquire resources
 Only after all this is done will the actual business
method be called
 Once it is called, the container will do some more work
with transactions, persistence, callbacks and returns
data or exception to the remote client

Mohammed Habeeb Vulla,Lecturer in dept computers


Working with EJBs

The Enterprise JavaBeans specification


is written for three audiences:
 The Client developer
 The EJB developer
 The EJB container developer

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB Clients
 EJB Clients are applications that access EJB
components in EJB containers. There are two
possible types. The first category is application
clients which are stand-alone applications
accessing the EJB components using the RMI-
IIOP protocol. The second category of
application clients are components in the web
container. They are java servlets and JSPs
which also access the EJB components via the
RMI-IIOP protocol.

Mohammed Habeeb Vulla,Lecturer in dept computers


The Client Developer’s
View
 The client has a smaller set of concerns then a
bean developer with regard to using EJBs.
Basically, he need to know :
➙ how to find or create a bean,
➙ how to use its methods and
➙ how to release its resources
 The client need not worry about the
implementation of the EJB, callbacks that the EJB
container will make on the EJB or nature of the
services provided to the EJB.

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB’s interface
 Home Interface : It is primarily for the life cycle
operations of the bean: creating, finding, and
removing EJBs. The home interface is not
associated with a particular bean, just with a
type of bean.
 Remote Interface : It is for business methods.
Logically, it represents a particular bean on the
server. The remote interface also provides
some infrastructure methods associated with a
bean instance, rather than a bean type.

Mohammed Habeeb Vulla,Lecturer in dept computers


Sample client application
pseudo code
A client programmer will acquire an EJB’s
home interface through JNDI, and they use
this home interface to :

Create or find Execute methods Reference Remove bean


instance of bean (Handle)
Mohammed Habeeb Vulla,Lecturer in dept computers
Client.java

Package orderMgmt;
import java.util.properties;
import java.naming.Context; // for name-to-object findings
import java.naming.InitialContext;// context for naming operations
public class Client {
try {
Properties prop = new Properties();
// server dependent properties for InitialContext
prop.put(Context.INITIAL_CONTEXT_FACTORY,
“org.jnp.interfaces.NamingContextFactory”);
prop.put(Context.PROVIDER_URL, “localhost:1099”);
Context ctx = new InitialContext(prop);
Object objref = ctx.lookup(“OrderManagement”);

Mohammed Habeeb Vulla,Lecturer in dept computers


Client contd..
// casting home interface reference to the OrderManagementHome
OrderManagementHome home = (OrderManagementHome)
javax.rmi.PortableRemoteObject.narrow(objref,
OrderManagementHome.class);
// home interface to create an instance of the OrderManagement
OrderManagement orderManagement = home.create();
// calling placeOrder()
orderManagement.placeOrder("Dan OConnor",
"Wrox books on programming", 1000);
orderManagement.remove();
System.out.println("Order successfully placed.");
} catch (Exception e) { e.printStackTrace(); }
}}

Mohammed Habeeb Vulla,Lecturer in dept computers


The Bean Programmer’s
view
 Main responsibility is write business logic and
structure the code in a particular structure. The
structure has 4 files, the home interface, remote
interface, business logic class file and the XML
file. The XML file called the deployment
descriptor, contains the structural information
about the bean, declares the bean’s external
dependencies and specifies certain information
about how services such as transaction and
security work.

Mohammed Habeeb Vulla,Lecturer in dept computers


Interface EJBObject
package javax.ejb;
public interface javax.ejb.EJBObject extends java.rmi.Remote {
EJBHome getEJBHome() throws java.rmi.RemoteException;
Handle getHandle() throws java.rmi.RemoteException;
Object getPrimaryKey() throws java.rmi.RemoteException;
boolean isIdentical(EJBObject obj) throws
java.rmi.RemoteException;
void remove() throws java.rmi.RemoteException;
}

Mohammed Habeeb Vulla,Lecturer in dept computers


OrderManagement
code..
 package orderMgmt;
 import javax.ejb.*;

 public interface OrderManagement extends javax.ejb.EJBObject


 {
 public void placeOrder(String custName, String prodName, int quantity)
throws java.rmi.RemoteException;
 public void cancelOrder(String custName, String prodName)
 throws java.rmi.RemoteException;
 public boolean isShipped(String custName, String prodName) throws
java.rmi.RemoteException;
 }

Mohammed Habeeb Vulla,Lecturer in dept computers


OrderManagementBean
code..
 package orderMgmt;
 import javax.ejb.*;

 public class OrderManagementEJB implements javax.ejb.SessionBean


 {
 public void placeOrder(String custName, String prodName, int
quantity)
 { // ... Business logic ...}
 public void cancelOrder(String custName, String prodName)
 { // ... Business logic ...}
 public boolean isShipped(String custName, String prodName)
 { // ... Business logic … return true; }

Mohammed Habeeb Vulla,Lecturer in dept computers


OrderManagementBean
code..
 public void ejbCreate()
 { // Can be empty }
 public void ejbRemove()
 { // Can be empty }
 public void ejbActivate()
 { // Can be empty}
 public void ejbPassivate()
 { // Can be empty}
 public void setSessionContext( SessionContext ctx )
 { // Can be empty}
 }

Mohammed Habeeb Vulla,Lecturer in dept computers


Interface EJBHome
Package javax.ejb;
public interface EJBHome extends java.rmi.Remote {
EJBMetaData getEJBMetaData () throws
java.rmi.RemoteException;
HomeHandle getHomeHandle() throws
java.rmi.RemoteException;
void remove(Handle handle) throws java.rmi.RemoteException,
java.ejb.RemoveException;
void remove(Object primary key) throws
java.rmi.RemoteException, java.ejb.RemoveException;
}

Mohammed Habeeb Vulla,Lecturer in dept computers


OrderManagementHome
code..
 package orderMgmt;
 import javax.ejb.*;

 public interface OrderManagementHome extends


javax.ejb.EJBHome
 {
 public OrderManagement create()
 throws java.rmi.RemoteException,
javax.ejb.CreateException;
 }

Mohammed Habeeb Vulla,Lecturer in dept computers


The xml file : ejb-jar.xml
<?xml version=“1.0”?>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>OrderManagement</ejb-name>
<home>orderMgmt.OrderManagementHome</home>
<remote>orderMgmt.OrderManagement</remote>
<ejb-class>orderMgmt.OrderManagementBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>

Mohammed Habeeb Vulla,Lecturer in dept computers


The xml file : ejb-jar.xml
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>OrderManagement</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

Mohammed Habeeb Vulla,Lecturer in dept computers


Structure of JAR file

META -INF\
ejb-jar.xml
orderMgmt\
OrderManagement.class
OrderManagementHome.class

OrderManagementBean.class

Mohammed Habeeb Vulla,Lecturer in dept computers


What you can’t do in an EJB
component?
 You cannot use Reflection API to access
information inaccessible to you.
 You cannot create a class loader or replace a
security manager.
 You cannot set the socket factory used by
ServerSocket or Socket
 You cannot use the object substitution
features of the serialization protocol

Mohammed Habeeb Vulla,Lecturer in dept computers


What you can’t do in an EJB
component?
 use Threads or the Threading API
 use the AWT
 Act as a Network Server

 use Read/Write static fields


 use java.io package
 Load a native library
 use “this” as an Argument or Return value
 use Loopback Calls

Mohammed Habeeb Vulla,Lecturer in dept computers


EJB Components on the
Web
Three classes of objects in MVC architecture:
 Model : This is the data and business-logic
component. It can serve multiple views.
 View : This is the presentation component or the
user-interface component. There can be different
presentations of a single model.
 Controller : This is the component that responds
to user input. Translates user-interface events
into changes to the model and defines the way
the user-interface reacts to those events.

Mohammed Habeeb Vulla,Lecturer in dept computers


Implementation of MVC in a
web site

Model
4
2
view1.jsp
3 Main.jsp
view2.jsp
view3.jsp Controller

Views 5
Browser 1
Client
Mohammed Habeeb Vulla,Lecturer in dept computers
Design of the EJB Tier

 UML use cases: UML is the Unified


Modeling Language, the standard
language for expressing the model of the
software system that we intend to build.
 Use cases are subset of UML that
expresses the functionality of the
software to be delivered. Use cases
describe what to do, but not how to do it.

Mohammed Habeeb Vulla,Lecturer in dept computers


Analysis Objects

 InterfaceObjects : The interface object


is responsible for controlling access to
the EJB tier from any client. An interface
object should always be represented by
a session bean in the implementation.
Ex : controller servlet for the web
application’s model-view-controller
architecture.

Mohammed Habeeb Vulla,Lecturer in dept computers


Control Objects

 Controlobjects provide services to the


application. They model functionality that
is not naturally associated with a
particular entity or interface. Control
objects should be represented by
session beans in the implementation.

Mohammed Habeeb Vulla,Lecturer in dept computers


Entity Objects

 Entity objects model those business


objects that should maintain their state
after the use case completes. This
means they represent data in the
database. Entity beans are often
represented by entity beans in the
implementation model.

Mohammed Habeeb Vulla,Lecturer in dept computers


An Example of EJB Design
 Consider the case of a company that develops
products, takes orders for those products, and
then manufactures and ships them.
 Actors in the company : An engineer, a web
customer, a phone operator who takes orders
from a catalog, floor manager who manages
the manufacturing process, a crew member
that actually builds the product ordered and a
manager who tracks overdue orders.

Mohammed Habeeb Vulla,Lecturer in dept computers


Use Cases

 Create a Product
 Place an Order
 Cancel an Order
 Select an Order for Manufacture
 Build a Product
 Ship an Order
 List Overdue Orders

Mohammed Habeeb Vulla,Lecturer in dept computers


Use case diagram from
analysis
Create Product
Engineer

Place Order
Customer

Cancel Order
Operator
Select Order
Manager
Build Product
Crew
Ship an Order
M’ment Overdue Orders
Mohammed Habeeb Vulla,Lecturer in dept computers
Stereotype icons in UML

 Interface Object :

Entity Object :

Control Object :

Mohammed Habeeb Vulla,Lecturer in dept computers


Translation of analysis model into
implementation

 Actor User Interface Type Interface Object Impl’ation

Engineer Visual Basic Session Bean (RMI/IIOP)


Customer Web Application JavaBean proxy / S Bean
Operator Swing GUI Session Bean
Manager Web Application JavaBean proxy / S Bean
Crew Palm Pilot XHTML Servelet to Session Bean
Management Web Application JavaBean proxy / S Bean

Mohammed Habeeb Vulla,Lecturer in dept computers


View of use case actors and their
respective interface
objects
Engineer
VB App

Customer
Web App

Operator Swing app

Manager Web App

Crew Palm App

Manage-
Web App
-ment
Mohammed Habeeb Vulla,Lecturer in dept computers
View of interaction of
interface
and control objects
VB Create
App
Web
App Place

Swing
App Cancel

Web
App Select

Palm Build
App Product
Web Ship List
App order Overdue
Mohammed Habeeb Vulla,Lecturer in dept computers
View of interaction between
control objects and entity
objects
Create
Product Product Routing
Place Order
Order
Cancel
Order Shipment
Select for Account
Manufacture
Build
Product Supplier
Ship Shipping
Order Company
List
Overdue Customer
Mohammed Habeeb Vulla,Lecturer in dept computers
Summary
 EJBs are intended for transactional systems
 EJBs are portable, reusable server-side
components that execute in a container
 Assist developer productivity, extend application
capability, and improve system stability
 Are accessible from many different types of clients
 There are three types of beans : stateful session,
stateless session, and entity

Mohammed Habeeb Vulla,Lecturer in dept computers


Summary
 There are four major parts to every bean: the
home interface, the remote interface, the
implementation class, and the XML deployment
descriptor
 The enterprise bean developer must follow
certain rules to get the benefits of EJB
technology
 The roles of EJBs can be understood by
analyzing a model of your enterprise in terms of
interface, control and entity objects

Mohammed Habeeb Vulla,Lecturer in dept computers


Mohammed Habeeb Vulla,Lecturer in dept computers

Вам также может понравиться