Chapter 22a: Enterprise JavaBeans (EJB) Session Beans

1 / 29
About This Presentation
Title:

Chapter 22a: Enterprise JavaBeans (EJB) Session Beans

Description:

WSAD has a Universal Test Client to test EJBs. 13. Working with EJBs. Reference: ... There are four interfaces (2 remote and 2 local) that you can define for a ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 30
Provided by: conest

less

Transcript and Presenter's Notes

Title: Chapter 22a: Enterprise JavaBeans (EJB) Session Beans


1
Chapter 22a Enterprise JavaBeans (EJB)Session
Beans
Reference Building J2EE Apps with WebSphere
2
Objectives
  • 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
  • Learn how to program an EJB client
  • Discuss exception handling in EJBs and EJB
    clients

3
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

4
What are EJBs (2)
  • 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
  • not covered in this course

5
What are EJBs (3)
  • The major components of an EJB
  • Bean class
  • Home interface
  • Local or Remote interface

6
MVC Architecture
7
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

8
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

9
J2EE Packaging into Archive Files
10
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

11
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 are called by the
    container

12
Stateless Session EJBs (2)
  • Steps to create a stateless session EJB
  • 1. Define a (bean) class that extends the Session
    Bean interface (i.e.,
    javax.ejb.SessionBean)
  • 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
    public business methods
  • 5. Write the deployment descriptor (ejb-jar.xml)
  • EJB Support in WSAD 5.1
  • EJB Wizards for all the five steps except for
    Step 3.
  • WSAD has a Universal Test Client to test EJBs

13
Working with EJBs
  • Reference Pages 526 -535
  • General Steps using WSAD 5.1
  • Create an Enterprise Application project
  • Create a Stateless Session Bean
  • Home and Remote Interfaces generated by WSAD 5.1
  • Add business methods to Bean class
  • Promote business methods to the remote interface
  • Business methods added to Remote Interface
  • Generate Deployment Code and RMI Stubs
  • Deploy EJB (Session Bean)
  • Test the Session Bean using WSAD UTC

14
Sample EJB Bean Class
  • Add echo() method to the generated Bean class

package com.xyz.ejb / Bean implementation class
for Enterprise Bean SalesFacade / public class
SalesFacadeBean implements javax.ejb.SessionBean
/ EJB SessionContext get/set methods
/ / EJB lifecycle methods / public void
ejbCreate() throws javax.ejb.CreateException
public void ejbActivate() public void
ejbPassivate() public void ejbRemove()
/ business method / public String
echo(String aString) return "SalesFacade "
aString
15
Sample EJB Test with WSAD UTC
16
Stateful 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

17
EJB 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

18
Remote Home Interface
19
Sample EJB Remote Home Interface
  • Generated by WSAD 5.1

package com.xyz.ejb / Home interface for
Enterprise Bean SalesFacade / public interface
SalesFacadeHome extends javax.ejb.EJBHome
/ Creates a default instance of Session
Bean SalesFacade / public com.xyz.ejb.SalesFa
cade create() throws javax.ejb.CreateException,
java.rmi.RemoteException
20
Remote Interface
21
Sample EJB Remote Interface
  • Generated by WSAD 5.1

package com.xyz.ejb / Remote interface for
Enterprise Bean SalesFacade / public interface
SalesFacade extends javax.ejb.EJBObject /
business method / public String
echo(String aString) throws java.rmi.RemoteExce
ption
22
Writing EJB Clients
  • Reference Page 548
  • 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
  • Lab 5 Build and Test SalesDemo EJB Client
  • Do the tutorial provided (detailed steps not
    found in your textbook)

23
Class Diagram for Sales Example
Note Clients use the remote interfaces to call
methods from a remote location.
24
SalesDemo main() Method
SalesFacade sales null try // 1. Get the
default initial context of WebSphere InitialConte
xt ctx new InitialContext() // 2. Obtain
remote home interface stub // from the
initial context of the desired EJB
(SalesEJB) Object o ctx.lookup("ejb/SalesFacade
Home") // Convert the object to the appropriate
EJB Home (class factory) SalesFacadeHome salesH
(SalesFacadeHome) PortableRemoteObject.narrow(
o, com.xyz.ejb.SalesFacadeHome.class) // 3.
Create a new EJB instance sales
salesH.create() System.out.println("Trace EJB
created") // 4. Call the method on the remote
interface System.out.println(sales.echo("Hello
World.")) catch (NamingException ne)
System.err.println("NamingException Cannot
locate EJB by name") catch (RemoteException
re) System.err.println("RemoteException
Cannot create EJB instance") catch
(CreateException ce) System.err.println("Create
Exception Cannot create EJB instance")
25
Troubleshooting JNDI Issues
  • JNDI allows the client to lookup an EJB using an
    alias.
  • E.g., ctx.lookup("ejb/SalesFacadeHome")
  • Convention EJB JNDI name starts with ejb/
  • The JNDI names on both the EJB module and
    Application Client module must match.
  • JNDI names are saved in the deployment
    descriptors
  • Can be modified by updating XML files outside the
    Java program code.

26
Modifying the JNDI Names
  • Sample Deployment Descriptor for EJB, AppClient
  • Object o ctx.lookup("ejb/SalesFacadeHomer")

27
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.
  • Lab 6 Stateless Session Beans and EJB Client
    with Application-level Exceptions
  • Do the tutorial provided (no detailed steps in
    your textbook)

28
EJBs with Application-Level Exceptions
29
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
Write a Comment
User Comments (0)