EJB Architecture
EJB Architecture
1
Agenda
• Why EJB™?
– Technologies leading up to EJB™.
– Goals of EJB.
• What is EJB™?
– EJB™ architecture.
• How do you develop, assemble, and deploy
enterprise beans?
– Roles.
• Who supports EJB™?
– Application server providers, bean
developers and assemblers.
• An example scenario.
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
2
EJB™: Architecture for Distributed
Transactional Components
3
Evolution of Distributed
Transactional Computing
4
Mainframe or Single Tier
5
Traditional Client/server
SQL
Data
SQL DB
Data
This changed in the 1980s with the advent of client-server computing, which was brought about by the
arrival of the PC. With smarter clients, you could have logic running on the client and nicer GUIs. This
era of computing marked a shift to the decentralized model of computing. Also, the task of programming
became easier, as PC development environments evolved.
Two Tier, Client Server:
•PC Client, Database Server
•"Fat" Client contains GUI Presentation Logic and Business Logic, and Data Access code,
•Client uses database API's for transactions.
Advantages:
•Nice user interface, powerful gui tools
Disadvantages:
•Data Access logic mixed with Business Logic (on client) makes reuse difficult.
•DB connection for every client process causes Scalability issues, DataBase Server can be bottleneck
•Application and database drivers must be installed and configured on each client.
•versioning,software distribution and maintenance of "fat" clients to lots of PCs very difficult, any change
means re-installing on every client.
6
Traditional Client/server: Fat Client
Disadvantages:
•Fat clients make maintenance and reuse difficult.
•Update and maintenance costs are high, because changes have to be re-
deployed to every client. Any change in the logic must be redistributed to all
clients.
•Fat Clients directly access the DB using SQL queries, meaning they are
tightly bound to the database schema. If the schema changes the client code
must be updated and redeployed to all clients.
•Poor network performance because all data is transferred for processing at
the client.
•Database connection costs are high because there is one database connection
per user with no connection pooling or multiplexing.
7
3 Tier and RPC
Server
DB
RPC SQL
3 Tier:
•Middle tier responsible for business logic: business methods run on the server,
and the client requests that the server execute these methods. The client and
server use a protocol that represents a conversation of business “services”,
instead of SQL queries and raw data returned.
•Client: user interface logic
•Database: repository of information
•RPC Remote Procedure Call: standard, transparent way to call procedures
remotely. But not object oriented.
Advantages:
•Client Easier to keep up to date, cheaper client deployment and maintenance.
•Separating Presentation tier, business tier, Database tier, makes it easier to
change one layer without affecting other layers.
•Scalability: Resources can be pooled.
Disadvantages:
•More difficult to program because server programmers need to manage
multithreaded concurrency, transactions, security.
8
3 Tier Distributed Object
Architecture
Object
SQL
RMI, IIOP Object
9
Web Server & CGI
HTML
network
network
CGI application
Database
The early 1990’s saw the popularization of the Internet. In terms of enterprise computing, this
had 2 effects, which would seem to be in opposition to each other. First, the Internet allowed
“internetworking”, which broke down the geographical barriers of the enterprise LAN. This in
turn led to a move toward “browser-based” clients, like Mosaic and its commercial cousin,
Netscape. The Internet and the browser made distributed computing accessible to everyone.
The second effect the Internet had on enterprise computing was to reverse the trend of de-
centralization. This happened because of the emergence of HTTP, HTML and the Java™
platform as standards for client-based computing. This created a huge number of “thin” clients
that could be counted on to have the same set of functionality. Because of this defined, but
limited, set of functionality, this created the trend of centralizing business logic on the server.
The server in this context was no longer a mainframe of mini-computer, but any machine
running a web server.
The problem in this model was that there were a myriad of programming and connectivity
models. CGI, NSAPI, and ISAPI to name a few. Also, the web server tended to mitigate
access between the browser-based client and each individual enterprise applications, causing a
programming and maintenance nightmare.
Advantages:
•Web makes applications available everywhere to any desktop with a browser
Disadvantages:
•Mission critical transaction-oriented applications can not be done easily and maintain-ably
with CGI and Perl.
10
Components
Monolithic:
•1 binary file
•Recompiled & Components:
linked for any changes •plug-gable Parts.
•Better design,
•better re-use,
•easier updates
•Implementation separated
from Interface, only Interface
published
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
Monolithic application:
•composed of 1 binary file.
•Any change means recompiling and redeploying the application. This makes it difficult to maintain
because the requirements and environment usually change with time and updates are then needed.
Component Application:
•software components can be changed or updated without recompiling and replacing the entire application,
like hardware components can be changed. Simplifies deployment of updates. Upgrades and bug fixes are easier
to make.
•application is separated into logical separate plug-gable parts like presentation parts, business process
parts, business logic…
•Component models provide a standard interface and mechanism for the components to interact with each other.
Component programming separates the interface from the implementation, meaning that a component’s
implementation can be changed without changing the interface or other components using the interface.
•Components are pre-developed pieces of application code that can be assembled to create an application. By only
publishing the interface, application assemblers do not have to understand how a component is coded, just how to use
the interface, in order to use a component.
•dividing an application into components improves the application’s design. Component programming forces
developers to define the application in terms of well structured objects, which have well defined interfaces so that they
can interact properly with each other.
•business logic can be reused, multiple instances of the same component can be used in multiple applications.
•Updates are easier because you can change a part without changing the whole.
•Development is easier because it allows you to test and build small parts incrementally (spiral development model),
you can also divide development into smaller parts potentially developed by different people.
11
Lessons Learned:
12
Application Servers With Component
Transaction Monitors
Application components
Server
More recently, application servers have allowed the aggregation of enterprise services into a
single web-facing display. This had the effect of simplifying the programming process when
dealing with multiple enterprise applications.
Application Servers and Platform Lock
The problem with this model is that there is a minimum of 25 application servers on the
market and each of them (as of late 1997) had a programming model that was product specific.
This has the unfortunate effect of locking the application builder into one specific product or
platform. This is a damper on the pace of innovation of eCommerce. Applications written to
one of these servers will be locked into the sever architecture, and will be unable to take
advantage of feature and performance enhancements found in the rest of the industry by virtue
of this “lock-in.”
The only solution to this problem would be to rewrite the application to a new application
server with the desired new features or performance characteristics. This would be an
expensive proposition, since an entirely new application model would have to be developed.
Application Servers with components and TP:
•With application servers the programmer can focus on business logic by developing
components which are deployed in the application server.
•Application Server provides transaction management, multi-threading, database connection
and resource pooling, performance…
•BUT Problem: until recently there was no standard on how those components had to be
developed, and what the API was between the components and the application server.
•Not Portable: components developed for one application server couldn't be deployed in
another one without a major re-write. Developer was locked to Application Server Vendor.
13
Why EJB™?
14
Why EJB™?
•W r i t e y o u r b u s i n e s s , n o t
–thread/connection pooling
–threading
–life cycle
–m a n a g e m e n t
15
J2EE™ Solution
Heterogeneous
ERP DB Mail Dir TP Servers
Uncontrolled
Customer Partner Supplier Clients
XML + Java™
Unified platform with standard Model and
standard APIs © for developing Enterprise Applications
Copyright 2000 Sun Microsystems, Inc., All rights reserved.
Taken in this context, the solution to the previous problems would be a single,
unified platform that would enable eCommerce in a distributed computing
environment across a wide array of competing products and would have a
single easy-to-use model for building applications. To be more specific, the
solution is the Java 2 platform, Enterprise Edition™.
16
J2EE™
Unified Platform:
•APIs for naming, security, transactions, messaging
Database, remote communication.
•EJB™ is cornerstone
©
of these APIs
Copyright 2000 Sun Microsystems, Inc., All rights reserved.
Prior to J2EE™ there were a bunch of enterprise java apis. This platform
weaves them together into a cohesive unifying platform.
Enterprise Java APIs provide programming interfaces for diverse middleware
implementations, such as naming, security, transactions, messaging and
database.
Enterprise JavaBeans (EJB™) are the cornerstone of these APIs.
J2EE™ provides a component-based, server-centric multi-tier application
architecture.
By defining the J2EE™ platform, developers are :
•ensured consistent, integrated runtime environment
•which enables application portability and interoperability.
17
J2EE™ Architecture
18
J2EE™ Deliverables
J2EE™ J2EE™
Specification Reference
Implementation
J2EE™
J2EE™
J2EE™
Compatibility
Test Suite BluePrints
19
J2EE™ Platform Specification
J2SE
RMI/IIOP
RMI/IIOP
JDBC
JDBC
JavaMail JavaMail
JNDI
JMS
JNDI
JTA
JMS
JTA
App Client
Container JAF JAF
App HTTP/
Client HTTPS
J2SE
RMI
RMI/IIOP
JDBC
JNDI
JMS
J2SE J2SE
Database
Platform specifies a container for each component type. Each container type
must support a set of standard services as defined in the spec.
20
Containers and Components
Containers Components
Handle Handle
• Concurrency • Presentation
• Security
• Availability
• JSP ™
, Servlets,
Applets
• Scalability
• Business Logic
• Transactions
• Distribution • EJB ™
• Data Access Logic
(optional)
• EJB ™
21
EJB™ Provides:
• Easier development
• open industry standard support
• Multi-tiered architecture
• Scalability
• Distributed transaction support
• Portable cross platform component
model, Platform independence
EJB™ Provides
-EJB™ Components provide reusable Data and Business Logic
-EJB™ Servers provide transactions and security support
-n-tier Architecture provides scalability and performance
-component container contract provides cross platform portability
22
n-tier Architecture with EJB™
Application Server
Container Beans
RDBMS
Java RMI / IIOP
clients
Data Base
network
network
C++, VB..
clients HTTP PeopleSoft
SAP R/3
Legacy
Browser
clients
Systems
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
23
EJB™ Component Oriented N-tier
Development
Application Server
Container
Client O
R EJB EJB EJB
B
Environment Services
DB
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
EJB™ Server:
provides EJB™ Infrastructure, provides underlying system services required by Container and EJB.
•Provides a Container Runtime Environment.
Containers:
provides the environment in which the beans run (Actually combination of classes/interfaces).
•transparently provides services by intercepting all method calls to the EJB™ component.
•provides management and control services:
•Naming services: JNDI ™ registration of EJB™ when loaded
•Lifecycle, state mgmt: creates and removes bean instances, Instance pooling, activation and passivation
•security checks - performs authentication & access control. Declarative & programmatic security.
•serializing method calls
•resource pooling
•Transaction coordination: declarative transaction mgmt
•Persistence management: storing of beans
•bean runtime context information (meta data)
The container simplifies nontrivial aspects of a distributed application such as security, transaction
coordination, and data persistence. The container and service providers implement the EJB™ infrastructure. This
infrastructure deals with distribution aspects, transaction management, and security aspects of an application. The
EJB™ specification defines the Java APIs for the infrastructure.
Developers focus on their area of expertise: the business logic and leave the infrastructure to the platform specialists.
24
Container Responsibility
Application Server
Life Cycle
Persistence State
Management
Container
Remote
Transaction Security
Interface
25
What features must a server
#3
offer?
Transaction Connection Pooling
Management
Security Load
Balancing
Web
Developer
EJB™ Servers
Servers are primarily resource managers in the EJB™ architecture. Each server manages the allocation
of resources to the containers it controls. At the coarsest level of granularity, this resource management
includes determining which containers and how many of each may run within the server. At a finer level
of granularity, this resource management occurs at three levels. At the client level, the server manages
incoming client requests. This management includes providing connection for clients, dispatching
client requests to containers, and routing responses back to clients. At the container level, the server
manages the processing resources available to the container. This management includes allocating
available worker processes and threads to running containers. At the service level, the server manages
container access to shared services. Such services may include a centralized security manager, a
transaction service implementation, a pool of JDBC drivers, a global cache manager, and an
asynchronous messaging service.
An EJB™ application may run on more than one server. So in addition to allocating resources to their
managed containers, servers must cooperate as a group to allocate resources efficiently across all the
servers in a cluster. This cooperation involves publishing load information to a load balancing process
that then assigns client requests to the least-loaded server that can provide the requested services. It also
includes system management infrastructure such as demons that automatically start server processes,
alerts to a management console when server load exceeds a certain level, and management interfaces for
controlling the resource allocation policies of the server.
26
Open Standard Solution:
27
EJB™ Architecture
create
lookup home context
remove
create
lookup
client remove S
bean
methods
object
Business
methods Container
Automatically invokes services based on
requirements defined in deployment descriptor
Deployment
Descriptor
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
•The Bean Remote Interface provides the client-side view of the Bean: It specifies all
the methods or public interfaces of the Bean that clients can call
•The Home Interface specifies methods to create an actual instance of a Bean
•EJBHome object plays role of "factory" object design pattern. Provides methods
to Create, remove and find enterprise beans
•EJBObject “proxy” object design pattern: The Client does not have direct access to
the EJB.The container provides an EJBObject as mediator or “proxy” between the
client, the container, and the EJB™ Instance. The EJBObject allows the container to
intercept calls to provide transaction management, security, persistence, or
activation/passivation related tasks. EJBObject is kind of like “Remote Control” for the
bean (like VCR remote control).
•The Bean is the actual code that implements the business logic of the bean.
•EJBContext is implemented by the container and provides the bean with information
about the client, the container, and the bean itself. The bean can use this information
while processing requests.
•The Deployment Descriptor: is a xml file which specifies Bean properties, this
allows the bean provider, application assembler and the deployer to set attributes for
security, transaction, and environment variables.
28
Enterprise Java Beans
Fundamentals
29
MVC Thin Client design model
servlet Entity
Session DB
JSP Entity
•View - The UI view involves the presentation layout (the actual HTML output), for the results of the
interaction. For HTML clients, this can be done using JSP™.
•Controller- presentation logic (the logical connection between the user and EJB™ services), necessary
to process the HTTP request, control the business logic, and select an appropriate response. This can be
provided by Java Servlets or Java Beans which call EJB’s for the business logic, and then call JSP’s to
display the result.
•Business logic (Model) -the business logic (the core business transactions and processing), necessary to
accomplish the goal of the interaction (calculations, queries, and/or updates to a database, etc). Business
logic should be separated from UI logic. This makes it possible to have different clients (for example
browser and GUI) using the same beans. Separating the 2 also makes the business components easier to
re-use, maintain, and update or change.
•Session Beans represent the business rules or process part of Business Logic. Session Beans
manage information relating to a conversation between the client and the server.
•Entity Beans represent the business data (stored in data base) part of Business Logic. Entity
beans represent and manipulate persistent application domain data.
Separating an application into layers like this allows you to change one layer without changing the other
layers. For example you can change the business rules without changing the business data, or change the
UI without changing the backend.
30
Session Beans
Represents business rules or process.
• Session beans are process, workflow or
control objects
• 2 types of session EJBs:
– stateful - maintains client specific state
(E.G. ShoppingCart).
– Stateless – provides services without
storing client specific information
• Session beans are often a client of multiple
entity beans: control workflow, process,tasks
between entity beans.
Session Beans:
Session beans are process or control objects specific to a particular client.
responsible for controlling workflow, managing processes or tasks , manage
activities (make reservation, purchase...) coordinate processes between entity
beans,Control interactions of objects, transient not stored in database. Do not
represent data in database , but they can access it. Life span is less than or
equal to the client.
•Executes on behalf of a single client
•can be transaction aware
•can update shared data in an underlying database but does not represent data
that should be stored in a database
•Is relatively short-lived (life typically is that of its client)
•Is removed when the EJB™ server crashes
Stateless
•execute a request and return a result without saving any state information
Stateful
•extension of client. performs tasks for client and maintain state on behalf of
client.
31
Entity Beans
Entity Beans
•persistent object: wraps "object data" stored in a database and provides
operations to manipulate this data.
•shared among multiple clients.
•Model permanent business “entities” or concepts that can be expressed as
nouns (i.e. customer inventory item)
•usually persistent records in database
•If the App Server crashes entity beans can be re-instantiated from the
database entity data.
32
Session vs. Entity Bean
33
Session & Entity Beans
Session
Entity Row
Session Row
1
Entity
1 1
1 n
Session 1
34
Example Scenario: Use Case
Transfer
Money
Customer
35
Example scenario: Classes
ATM Account
0…* 0…*
withdraw()
transfer()
accesses deposit()
Checking Savings
Account Account
36
Example Scenario: Sequence
Diagram
1: transfer()
2: debit()
3: credit())
37
Example scenario: EJB™
debit Account
Entity Bean
ATM
Client account1
Session bean
Transfer account2
credit Account
Entity Bean
38
EJB™ Packaging
EJBHome
Interface
Bean
EJBObject
Interface
Deployment
descriptor
EJB™ Jar
EJBs are packaged in a Jar file along with the EJBHome (“factory”)
interface, the EJBOjbect (“proxy”) interface and the XML deployment
descriptor.
39
EJB™ Roles: Bean Provider
Deployment tools
EJB
home
ejb
EJB
object
Deployment
descriptor
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
40
EJB™ Roles: Application Assembler
Deployment tools
Application Assembler
The application assembler is a domain expert who composes applications by combining existing beans.
The Application Assembler combines enterprise beans into larger deployable application units.
The input to the Application Assembler:
•one or more ejb-jar files produced by the Bean Provider(s). The Application Assembler works with the enterprise
Bean’s deployment descriptor and the enterprise Bean’s client-view contract. Although the Assembler must be
familiar with the functionality provided by the enterprise Beans’ remote and home interfaces, he or she does not need
to have any knowledge of the enterprise Beans’ implementation.
The Application Assembler outputs:
•one or more application ejb-jar files that contain the enterprise beans along with their application assembly
instructions. The Application Assembler has inserted the application assembly instruction into the deployment
descriptors.
•The Application Assembler can also combine enterprise beans with web components (e.g. Java ServerPages™ or
Servlets) when composing an application.
Assembling an application:
•take a set of components & assemble them into complete application.
•Reconcile deployment parameters:
•link internal dependencies of all components, such as EJB™ names
•synchronize security role-names across components in the application
•create an XML application deployment descriptor
•must be named application.xml
•package the application as an Enterprise Archive format (.ear)
•This step will typically be done via wizards and tools, and developers will usually not be editing the XML
deployment descriptors by hand
41
EJB™ Roles: Deployer
Application Jar
Deployment
descriptor
Deployment tools
EJBHome EJBObject
Implementations,
Container
Enterprise Beans
Client stubs
context
meta attributes
client
Database
Deployer
•The deployer is an expert at a specific operational environment and is responsible for deployment of a EJB™
application in its container.
•The Deployer takes one or more ejb-jar files produced by a Bean Provider or Application Assembler and deploys the
enterprise beans contained in the ejb-jar files in a specific operational environment. The operational environment
includes a specific EJB™ Server and Container.
•The Deployer must resolve all the external environment dependencies declared by the Bean Provider, and must
follow the application assembly instructions defined by the Application Assembler.
•The Deployer uses tools supplied by the EJB™ Container Provider to perform the deployment tasks.
•The Deployer’s output is enterprise beans (or an assembled application that includes enterprise beans) that have
been customized for the target operational environment, and that are deployed in a specific EJB™ Container.
Deployment:
•installation: copy all material to server
•configuration: resolve all external dependencies:
•execution: start up new application
Configure environment:
•resolve external references
•to EJBs
•to resource factories, like databases
•to URLs
•set application config parameters
assign security:
•map security roles to user groups and accounts that exist in the enterprise
•set up authorization
•choose authentication mechanisms
•provide authentication data for resource access
42
EJB™ Roles: Server/Container Provider
Application Server
Container
O
R
B
Environment Services
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
43
Lifecycle Illustration
Deploy
J2EE Container
Enterprise
Components
44
Declarative Programming
45
Bean Provider Example
Car
Reservation
EJB
46
Bean Provider Example
Flight
Reservation
EJB
47
Application Assembler
AAA
Application Assembler
Creates Travel agency
Application
Travel Agency
Session, JSPs
Car Flight
Reservation Reservation
EJB EJB
48
Deployment in a Target Container
Car
Database
EJB™ Container
Car
Reservation
Client Travel
Agency Flight
Database
Flight
Reservation
Deployment
Descriptor
49
Broad Industry Adoption
50
Enterprise Deployment
51
EJB™ Links & References:
O'REILLY: Enterprise JavaBeans By Richard Monson-Haefel
http://www.oreilly.com/
Wiley: Mastering Enterprise JavaBeans By Ed Roman
Prentice Hall: Enterprise JavaBeans by Example
http://www.prentice-hall.com/ptrbooks/ptr_0130224758.html
Programming with Enterprise JavaBeans, JTS, and OTS
by Andreas Vogel, Madhavan Rangarao
Enterprise Javabeans : Developing Component-Based Distributed Applications by Thomas
C. Valesky http://adams.patriot.net/~tvalesky/ejb.html
Java EJB™ site:
http://java.sun.com/ejb
DEV-x javabeans:
http://www.javabeans-zone.com/
Special Interest Group - Enterprise JavaBeans
http://www.mgm-edv.de/ejbsig/ejbsig.html
Links to EJB™ articles
http://www.ejbnow.com
Cetus Distributed Objects & Components: Enterprise JavaBeans
http://www.rheinneckar.de/~cetus/oo_javabeans.html
http://www.ejbportal.com/
Javaworld
http://www.javaworld.com/javaworld/topicalindex/jw-ti-ejb.html
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
52
EJB™ Characteristics Summary
53
First Exercise
54
Example Solution: Online Shopping
Browse Catalog
customer
55