Java Programming: Advanced Topics
1
Enterprise JavaBeans
Chapter 14
Java Programming: Advanced Topics
2
Objectives• Explore an enterprise application’s needs for secure
distributed access, scalability with high performance, robustness, data persistence with transactional integrity, and management of distributed and often disparate resources
• Discover how the EJB framework provides the quality of services enterprise applications require
• Learn how stateful and stateless session EJBs provide components that perform business logic
Java Programming: Advanced Topics
3
Objectives (Cont.)
• Learn how to program an EJB client
• Discuss exception handling in EJBs and EJB clients
• Learn how entity beans represent persistent data in Java objects
Java Programming: Advanced Topics
4
Objectives (Cont.)
• Discuss mapping fields of entity beans to elements in databases using CMP and BMP
• Learn how to use container-managed relationships and EJB query language with container-managed persistence
• Learn what Java Message Service is and learn how message-driven beans consume asynchronous messages
Java Programming: Advanced Topics
5
Objectives (Cont.)
• Discuss transactional properties of EJBs
• Discuss elements of application security that relate to EJBs
• Consider some best practices for designing applications that use EJBs
Java Programming: Advanced Topics
6
Enterprise Programming • EJB specification: a description of this
environment and the framework for building distributed objects that implement a standard interface
• Distributed systems: software systems that reside on several physical hosts – the components must interoperate over a network
that may be a local area network (LAN) or the Internet
Java Programming: Advanced Topics
7
Enterprise Programming (Cont.)• Scalability: the ability of a system to continue to
give high performance as the number of users increases
• Persistence: any data that outlives any software component
• Resource management is required whenever the system makes high demands on databases and other resources
Java Programming: Advanced Topics
8
Enterprise Programming (Cont.)• Connection pooling: keeping a set of
connections open so that different software components can be given an open connection for short-term exclusive use
• Security: usually of critical importance in enterprise applications
• Transactional integrity: often the quality that most concerns developers of enterprise applications
Java Programming: Advanced Topics
9
What are EJBs • Enterprise JavaBeans (EJBs): server-side
software components that conform to the J2EE architecture for development and deployment of distributed systems
• J2EE-compliant application servers must provide a run-time environment for the EJBs: an EJB container
Java Programming: Advanced Topics
10
What are EJBs (Cont.)
• EJBs come in three basic kinds:– Session beans:
• can perform any kind of processing
– Entity beans:• represent persistent data
– Message-driven beans (MDB):• have a specialized purpose and are used with
messaging software
Java Programming: Advanced Topics
11
What are EJBs (Cont.)
• The major components of an EJB:– Bean class– Home interface– Local or Remote interface
Java Programming: Advanced Topics
12
MVC Architecture
Java Programming: Advanced Topics
13
EJB Containers and Services
• The role of the EJB container is to provide the following:– The distribution infrastructure and a naming service to
help client code locate and access EJBs.– The ability to place EJBs in a scalable architecture– Support for concurrent access– Resource management, including connection pooling– Security services in addition to the secure
environment that can be configured for applications loaded into the application server
– Transaction managers that interact with JDBC drivers and resource adapters
Java Programming: Advanced Topics
14
J2EE Enterprise Application Packaging and Deployment
• EJBs are always packaged in J2EE enterprise applications
• Files that compose EJBs are packaged in jar files
• An EJB jar must contain a deployment descriptor file
Java Programming: Advanced Topics
15
J2EE Packaging into Archive Files
Java Programming: Advanced Topics
16
Session EJBs
• Session EJBs can do general purpose processing
• They are associated with the client that calls them
• There are two types of session EJBs:– stateful – stateless
Java Programming: Advanced Topics
17
Stateless Session EJBs
• Stateless session beans can be shared among clients
• Methods defined in the javax.ejb.SessionBean interface are call-back methods that control the lifecycle of the bean, that called by the container
Java Programming: Advanced Topics
18
Stateless Session EJBs (Cont.)• Steps to create a stateless session EJB:
– 1. Define a class that extends the SessionBean interface
– 2. Create a home interface – 3. Add business methods to the session bean class
and write implementations of those methods– 4. Add a remote interface and include all the
business methods that can be called by remote clients
– 5. Write the deployment descriptor
Java Programming: Advanced Topics
19
Stateful Session EJBs
• Stateful session EJBs retain conversational state between method calls and are used by only one client
• Conversational state: information that must be retained as long as the client is actively interacting with the application
• Transactional state: data that must be permanently recorded when the client activity ends
Java Programming: Advanced Topics
20
EJB Clients
• The EJB 2.0 specification adds interfaces to all entity and session beans specifically for use by co-located clients
• There are four interfaces that you can define for a session or entity bean
Java Programming: Advanced Topics
21
The Client Interfaces
Java Programming: Advanced Topics
22
The Client Interfaces (Cont.)
Java Programming: Advanced Topics
23
The Client Interfaces (Cont.)
Java Programming: Advanced Topics
24
The Client Interfaces (Cont.)
Java Programming: Advanced Topics
25
Writing EJB Clients
• The client starts by accessing the JNDI namespace, by instantiating an object of type javax.naming.InitialContext
• When the client has the EJB home, it uses a create method to get a handle to the remote interface
Java Programming: Advanced Topics
26
Handling Exceptions in EJB Clients
• Define and use application-level exception classes to encapsulate anticipated problem conditions
• Application-level exceptions: exceptions that business logic can anticipate and possibly recover from.
Java Programming: Advanced Topics
27
Entity EJBs• Entity beans represent persistent data• Use entity beans as the interface between Java
components and relational or object-oriented databases
• The container uses the primary key to locate the data when a client requests an entity bean and creates only one bean to represent that data in a Java object
Java Programming: Advanced Topics
28
EJB to Database Schema Mapping
• There are three approaches to database schema mapping:– Top-down mapping:
• possible when a new database is required and Java developers are allowed to create databases
– Bottom-up mapping: • occurs when you have a database and can design your EJBs
to match the tables and columns defined in the database schema
– Meet-in-the-middle mapping: • most common solution in practice
Java Programming: Advanced Topics
29
EJB to Database Schema Mapping (Cont.)
• The EJB specification allows two approaches:– Container Managed Persistence (CMP):
• involves declaring the mapping between the deployment descriptor and delegating all code generation to the container
– Bean Managed Persistence (BMP):• is a do-it-yourself solution
Java Programming: Advanced Topics
30
Sample CMP Entity Bean
Java Programming: Advanced Topics
31
EJB Query Language
• Enterprise JavaBean Query Language (EJB QL) was introduced in the EJB 2.0 specification to support the abstract persistence model
• EJB QL: language for expressing the equivalent of SQL SELECT statements for CMP beans
Java Programming: Advanced Topics
32
Comparing EJB QL and SQL Statements
Java Programming: Advanced Topics
33
Using EJB QL
Java Programming: Advanced Topics
34
Message-Driven Beans
• An message-driven bean (MDB) initiates processing in response to an external event
• To create an MDB, build the bean class• The MDB class must implement
javax.ejb.MessageDrivenBean and javax.jms.MessageListener
Java Programming: Advanced Topics
35
A Deployment Descriptor with MDB
Java Programming: Advanced Topics
36
A Possible Scenario that Uses MDBs
Java Programming: Advanced Topics
37
EJB Transactional Characteristics • The EJB specification gives you options for
defining transactions:– Container managed transactions (CMT)– Bean managed transactions (BMT)– Client demarked transaction
Java Programming: Advanced Topics
38
EJB Transactional Levels
Java Programming: Advanced Topics
39
EJB Transactional Levels (Cont.)
Java Programming: Advanced Topics
40
EJB Transactional Levels (Cont.)
Java Programming: Advanced Topics
41
EJB Security
• The concept of passing security roles down the call stack as EJB methods call other EJB methods is called delegation
• Role-based security can be applied to entire EJBs or to individual methods
• EJB developers are not responsible for user authentication or authorization (mapping the user to a role)
Java Programming: Advanced Topics
42
Summary• EJBs are distributable server-side components
that run in EJB containers provided by J2EE-compliant application servers
• EJBs come in three basic kinds: session beans, entity beans, and message-driven beans (MDB)
• EJB clients locate bean instances by looking up the name in a JNDI server
• Stateless session beans can be shared among clients, while stateful session EJBs are used by only one client
Java Programming: Advanced Topics
43
Summary (Cont.)
• Entity beans represent persistent data and can be designed for container-managed persistence (CMP) or bean-managed persistence (BMP)
• EJB QL is a SQL-like language for writing database queries in terms of CMP bean classes and fields
• The concept of passing security roles down the call stack as EJB methods call other EJB methods is called delegation