Title: Chapter 8b: Business Logic Tier: Session EJB
1Chapter 8bBusiness Logic TierSession EJB
Reference Eclipse WTP
2Objectives
- Enterprise Java Beans
- Distributed Application Using EJBs
- Types of EJBs
- Developing Session EJBs
- Writing EJB Clients
3What 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
4Distributed Application Using EJBs
5Types of EJBs
- Session beans
- can perform any kind of processing
- models business processes (coarse-grained
components) - Entity beans
- represent persistent data
- models fine-grained data objects
- Message-driven beans (MDB)
- used with messaging software
6Major Components of an EJB
- Bean class
- Implements the business methods
- Home interface
- Defines the methods that allows clients to
create, find and remove EJB objects - Remote interface
- Defines the business methods callable by the a
remote client
7MVC Architecture
8EJB 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
9J2EE 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 (ejb-jar.xml)
10J2EE Packaging into Archive Files
11Session EJBs
- Session EJBs can do general purpose processing
- Associated with the client that calls them
- There are two types of session EJBs
- stateful
- stateless
- EJB Design Pattern Session Façade (p.327)
- E.g., LeagueFacade EJB wraps the IceHockeyFacade
object
12Stateless Session EJBs
- Can be shared among clients
- Methods defined in the javax.ejb.SessionBean
interface are call-back methods (e.g.,
ejbCreate()) that control the lifecycle of the
bean, that are called by the container
public abstract class LeagueFacadeBean
implements LeagueFacade, javax.ejb.SessionBean
/ _at_ejb.create-method view-type"remote"
/ public void ejbCreate() leagueFacade
IceHockeyFacade.getLeagueFacade()
13Iteration 2 Developing Session EJBs
- Reference Pages 325 -355
- General Steps
- Create an EAR and EJB projects
- Create a Stateless Session Bean
- Note Home and Remote Interfaces generated by
XDoclet - Add business methods to Bean class
- Note can be automatic if interface specified in
Wizard - Add XDoclet annotation to set the view-type (e.g.
both) - Business methods added to Local and Remote
Interface - Build a Web client
- Run the Web client
- Deploy and test the Session Bean
14Developing Session EJBs Using Eclipse WTP
- New XDoclet Enterprise JavaBean
15EJB Component Class DiagramReference page 329
- Assembled into LeaguePlanetEJB.jar
- In the com.leagueplanet.ejb package, only
LeagueFacadeBean code typically changed by
developer the other class/interfaces
auto-created by XDoclet.
16Sample EJB Bean Class
- Implement findLeague() method by calling
findLeague() method of the IceHockeyFacade
(service layer).
import com.leagueplanet.services.LeagueFacade p
ublic abstract class LeagueFacadeBean
implements LeagueFacade, javax.ejb.SessionBean
private LeagueFacade leagueFacade /
_at_ejb.interface-method view-type"both" / public
League findLeague(long id) return
leagueFacade.findLeague(id)
17Stateful Session EJBs
- Stateful session EJBs retain conversational state
between method calls and are used by only one
client - Not emphasized in this course.
- 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 - Note not covered in this course.
18Sample EJB Remote Home Interface
- Generated by XDoclet in LeaguePlanetEJBClient
project
package com.leagueplanet.ejb / Home
interface for LeagueFacade. _at_wtp generated
/ public interface LeagueFacadeHome extends
javax.ejb.EJBHome public static final String
COMP_NAME "javacomp/env/ejb/LeagueFacade"
public static final String JNDI_NAME"LeagueFacade
" public com.leagueplanet.ejb.LeagueFacade
create() throws javax.ejb.CreateException,ja
va.rmi.RemoteException
19Sample EJB Remote Interface
- Generated by XDoclet in LeaguePlanetEJBClient
project
package com.leagueplanet.ejb / Remote
interface for LeagueFacade. _at_wtp generated
/ public interface LeagueFacade extends
javax.ejb.EJBObject public
com.leagueplanet.model.League findLeague( long id
) throws java.rmi.RemoteException
20EJB Clients
- The EJB 2.0 specification adds interfaces to all
entity and session beans specifically for use by
co-located clients (i.e., located in the same
JVM) - Local interfaces added to EJB 2.0
- There are four interfaces (2 remote and 2 local)
that you can define for a session or entity bean - Interfaces automatically generated by XDoclet
based on the view-type
21Writing EJB Clients
- Reference Pages 349 - 352
- The client calls getHome() method generated by
XDoclet. - getHome() performs a lookup to the JNDI namespace
using the JNDI_NAME (e.g., LeagueFacade) - When the client has the EJB home, it uses a
create() method to get a handle to the remote
interface - When the client has the EJB remote object, it
calls the business methods (e.g., findLeague())
22Sample EJB Client
- In schedule.jsp (EJB client), we use the EJB
Session Bean (e.g., LeagueFacade) to call
findLeague() method.
lt_at_page import"com.leagueplanet.ejb.LeagueFacad
e"gt lt_at_page import"com.leagueplanet.ejb.LeagueFa
cadeUtil"gt lt LeagueFacade leagueFacade
LeagueFacadeUtil.getHome().create() League
league leagueFacade.findLeague(1) Iterator
schedules league.getSchedules().iterator() gt
23Next Steps
- Chapter 8 Tutorial
- Iteration 2. Session EJBs
- LeaguePlanetEJB LeagueFacadeBean
- LeaguePlanetEJBClient
- LeaguePlanetWeb schedule.jsp
- Assignment 3
- XXYYSurveyEJB XXYYSurveyFacade
- XXYYSurveyEJBClient
-