2. Technical Topics
•What is Enterprise JavaBeans?
•Objectives of EJB
•Enterprise JavaBeans Architecture
•Enterprise Beans (Session and Entity)
•Advanced EJB Topics
2
3. What is Enterprise JavaBeans?
• A specification from Sun Microsystems
• Enterprise JavaBeans defines a server component
model for the development and deployment of
Java applications based on a multi-tier,
distributed object architecture
• The Enterprise JavaBeans specification defines:
– A container model
– A definition of the services the container needs to
provide to an Enterprise JavaBean, and vice versa
– How a container should manage Enterprise JavaBeans
3
4. JavaBeans vs Enterprise JavaBeans
• Enterprise JavaBeans is a framework for
building and deploying server-side Java
components
• JavaBeans is a framework for client-side Java
components
• Conceptually related because both are
components
• The specifications are different
• The specifications do not build on each other
or rely on each other
4
5. What’s in the EJB Specification
• Goals for the Release
• Roles and Scenarios
• Fundamentals (Scope of EJB)
• Session and Entity Beans
• Transactions, Exceptions, Distribution
• EJB Bean and Container Responsibilities
• API Reference
EJB Vendors
Have to do all the WORK
6
6. What EJB Accomplishes
You can take any Java class and with little effort
make it a distributed, secure, transactional class
You can take any data source and make the data
source appear to be a collection of Java objects
– Eliminates distinction between data from a database
and any other source
– All information is accessed through Java objects
– All SQL is cleanly encapsulated in Java objects
• true object-oriented programming
• high reusability
– Database objects work with the full Java class library
7
7. What EJB Means to IT Groups
• Java everywhere - one language, one environment
– Java on the client
– Java in the middle tier
– Java in the database
• Easier to program
• Easier to maintain
• All platforms look the same
8
8. What EJB Means to Developers
• Developers can focus on writing business logic
rather than writing low-level infrastructure like
data access, concurrency, transactions, threading,
etc.
– Reduces complexity & development time
– Increases quality and reliability
• The knowledge you learn about EJB will be
portable among many different products because
EJB products are based on a common standard
• You will see greater reuse because your code is
located in shareable, server objects
9
9. What’s Unique About EJB
• Mandates a container model where common
services are declared, not programmed
– At development and/or deployment time, attributes
defining the bean’s transaction and security
characteristics are specified
– At deployment time, the container introspects the
Enterprise JavaBean attributes for the runtime services
it requires and wraps the bean with the required
functionality
– At runtime, the container intercepts all calls to the
object
10
10. Power of EJB
• Write a simple Java class
• Get a server-based,
transactional and secure class
automatically
• N-Tier made easy
11
13. Enterprise JavaBeans Architecture
The EJB architecture specifies the responsibilities
and interactions among EJB entities
EJB Servers
Enterprise Beans EJB Clients
EJB Containers
EJB Server
EJB Container
Enterprise
Bean
Enterprise
Bean
Java Clients
13
14. EJB Server
EJB Server
• Provides a Runtime Environment
• The EJB Server provides system services and
manages resources
– Process and thread management
– System resources management
– Database connection pooling and caching
– Management API
14
15. EJB Server
EJB Container
EJB Container
• Provides a Run-time Environment for an
Enterprise Bean
• Hosts the Enterprise JavaBeans
• Provides services to Enterprise JavaBeans
– Naming
– Life cycle management
– Persistence (state management)
– Transaction Management
– Security
15
16. EJB Server
Enterprise JavaBeans Components
• A specialized Java class where the real business
logic lives
– May be developer-written or tool-generated
• Distributed over a network
• Transactional & Secure
• Server vendors provide tools that automatically
generate distribution, transaction and security
behavior
EJB Container
Enterprise
Bean
Enterprise
Bean
16
17. EJB Clients
• Client access is controlled by the container in
which the enterprise Bean is deployed
• Clients locates an Enterprise JavaBean
through Java Naming and Directory Interface
(JNDI)
• RMI is the standard method for accessing a
bean over a network
• CORBA is also supported
EJB Server
EJB Container
Enterprise
Bean
Enterprise
Bean
Java Clients
17
19. Written by Developer
Generated at Development
Generated at Deployment
Enterprise Bean Contents
• EJB Class is written by the developer
• Home and Remote interfaces and classes
control access to the EJB class
• Deployment Descriptor and MANIFEST
describe security and transactional
characteristics of the bean
20
EJB Jar File
EJB Class
Home
Interface
Remote
Interface
Home
Class
(Implementation)
Remote
Class
(Implementation)
Deployment
Descriptor
20. The Deployment Descriptor
• Allows declarative customization
• Created at deployment time
• Gives the container instructions on how to
manage the enterprise bean
• Controls behaviors for:
– Transaction
– Security
– Life cycle
– State management
– Persistence
21. Bean Development Process
EJB Jar File
EJB Class
Home
Interface
Remote
Interface
Home
Class
(Implementation)
Remote
Class
(Implementation)
Deployment
Descriptor
• Implement the EJB Class
• Specify the remote interface
• Specify the home interface
• Specify security and transactional characteristics
using vendor tools (Deployment Descriptor)
• Use vendor tools to
generate supporting
code and package
components in EJB-jar
• Iterate...
21
22. EJB Class
• A bean has a single Java class at its core
– This class is written by a developer or generated by a tool
• Implements application-specific business logic
• Implements one of the following contracts:
– javax.ejb.EntityBean &
javax.ejb.SessionBean
• These contracts provide for consistent behavior
when activating beans, passivating beans, reading
data, writing data
• Every container can expect these methods in every
bean
22
23. EJB Jar File
EJB Class
Home
Interface
Remote
Interface
Home
Class
Remote
Class
DD
Manifest
Remote Interface and Class
• Intercepts calls to the EJB Class to add support for:
– Transactions/security/threading
• Remote class has the same methods as the bean
and delegates to the bean for actual behavior
• Remote class checks security and sets up
transaction before delegating method call to the
bean
• Clients can never get a reference
to a bean’s EJB Class, only the
Remote interface
23
24. Home Interface and Class
EJB Jar File
EJB Class
Home
Interface
Remote
Interface
Home
Class
Remote
Class
DD
Manifest
• Used to get a reference to a bean’s remote
interface
• Provides bean creation services
– myFoo = FooHome.create() instead of
myFoo = new Foo()
– Supports multiple signatures to create EJB
instances
• Similar to class factory in COM
and CORBA
• May be generated by tools that
come with an EJB server
24
25. EJB Object Interactions
Remote Interface
Implements
Home Interface
Implements
Client
Calls Calls
Remote Class
Bean Class
Delegates
Home Class
Delegates
Returns
Client
Server
25
26. Comparing Session and Entity
Beans
• Mandatory for EJB 1.0
• Represents a specific
client
(1 instance per client)
• Short-lived
• Transient
• Can be any Java class
• May be transactional
• Optional for EJB 1.0
• Represents underlying
data object or context
(clients share instance)
• Long-lived
• Persistent
• Can be a class that
maps to persistent
data (e.g., database)
• Always transactional
Session Beans Entity Beans
26
28. 27
Session Beans Represents Process
• A transient agent for an individual client that
executes on a server (e.g., ShoppingCart)
• Session beans are often a client of multiple entity
beans
• Implements javax.ejb.SessionBean interface
• State management types for session EJBs
– stateful - session bean may maintain state information
across method calls
– stateless - session bean may be used to service multiple
clients
– a stateless session bean can only have a single no-
argument create() method in its Home interface
29. Entity Bean Represents State
Implements javax.ejb.EntityBean interface
• Maps a data source to a Java class
– table, view, join or stored procedure in a relational
database
– a set of related records in a database
– legacy data
• Each instance of an entity bean is one row of data
• Each instance of an entity bean is uniquely
identified by a primary key
• An Entity Bean can also have additional methods
for business logic, etc.28
30. EJB Persistence
• Provides Entity Beans the ability to store and
retrieve their state
• Can be implemented by a bean
– Bean Managed Persistence
• Can be implemented by a container
– Container Managed Persistence
50
31. Enterprise JavaBean Packaging
EJB Jar File
EJB Class
Home
Interface
Remote
Interface
Home
Class
Remote
Class
DD
Manifest
• Enterprise JavaBeans are comprised of many
Java files
• These files are put in a JAR file
– A JAR file is a ZIP file with a MANIFEST that
describes the contents of the file
– A MANIFEST is a simple text file
• A JAR file can contain more
than one Enterprise JavaBean
35
32. EJB Supporting
Technologies
Java Transaction Service (JTS)
Java Naming and Directory Interface (JNDI)
The Object Bus (RMI, CORBA)
Java Security
33. Java® 2 Platform Enterprise
Edition (J2EE)
Go to http://java.sun.com/marketing/enterprise/apis.html
for more information37
34. Java Transaction Service (JTS)
• JTS is an API specification that is used to
provide a common interface to transaction
managers
• JTS is needed because clients no longer talk
directly to databases, so a database commit is
not accessible to client
• Object-level transactions are provided in
place of database transactions by JTS
• The objects in the middle-tier will eventually
delegate to database for transactions
JTS
Java
Transaction
Service
A Standard
Transaction
Management
API
38
36. Java Naming and Directory
Interface (JNDI)
• JNDI is an API specification that is used to
provide naming/directory services
• JNDI can be used to find files, printers, objects,
etc. on a network
• JNDI provides a common API on top of any
directory service product
– JNDI is partially implemented by JavaSoft
– Directory service products (LDAP, DNS, NDS, etc)
implement most of the specification
• JNDI is used in conjunction with RMI and EJB
to locate Enterprise JavaBeans on a server
JNDI
Java Naming
and Directory
Interface
Connectivity to
Enterprise
Naming and
Directory
Services
43
37. Java Application
JNDI Implementation
Manager
LDAP NDS
JNDI-
COSNaming
JNDI-RMI
Object Object Object
JNDI API
JNDI SPI
JNDI Architecture
JNDI
Java Naming
and Directory
Interface
Connectivity to
Enterprise
Naming and
Directory
Services
38. Remote Method Invocation (RMI)
• Remote Method Invocation (RMI) is a
standard that allows client computers to
access objects located on a server
– Java Software provides a complete implementation of RMI
– RMI puts a stub on the client computer that looks like the
object but is instead a proxy to the real object located on the
server
– Primary network protocol for RMI is Java Remote Method
Protocol (JRMP)
– RMI is used to make an Enterprise JavaBean accessible to
clients over a network
RMIStub
RMI
Skeleton
Application
Server
Bean
RMI
Java Remote
Method
Invocation
Distributed
Java-to-Java
Applications,
in JDK 1.1
46
39. Common Object Request
Broker Architecture (CORBA)
• CORBA is a standard for remotely referencing objects
written in any language and calling the methods on
those objects
• CORBA supports multi-language programming
– Client and server do not have to be the same language
• Primary network protocol for CORBA is IIOP
• Enterprise JavaBeans can use CORBA instead of RMI
– Makes the beans accessible from languages other than Java
– Simplifies configuration for organizations that have already
standardized on CORBA before introducing Java and EJB
into the organization
47
40. Summary
• Enterprise JavaBeans delivers mult-vendor,
OS independent standard for developing web-
based N-tier applications
• Java 2 Platform, Enterprise Edition is the
server-side platform delivering all the
Enterprise APIs