Enterprise JavaBeans Umer Farooq - PowerPoint PPT Presentation

About This Presentation
Title:

Enterprise JavaBeans Umer Farooq

Description:

import javax.ejb.EJBHome; public interface CartHome extends EJBHome ... import javax.rmi.PortableRemoteObject; public class CartClient ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 22
Provided by: muralik9
Category:

less

Transcript and Presenter's Notes

Title: Enterprise JavaBeans Umer Farooq


1
Enterprise JavaBeansUmer Farooq
CS6704 Design Patterns Component Frameworks
February 25, 2002
2
Enterprise JavaBeans (EJB)
  • Enterprise JavaBeans is the server-side
    component architecture for the J2EE platform. EJB
    enables rapid and simplified development of
    distributed, transactional, secure and portable
    Java applications.

3
Agenda
  • Overview of J2EE platform
  • EJB and J2EE?
  • Types of EJB
  • Life Cycles of EJB
  • Client access to EJB
  • Code example of an EJB
  • Applications using EJB
  • Comparison of EJB with Microsofts technology!
  • Comments/Questions/Discussion

4
J2EE Platform
  • Approach to developing highly scalable internet
    or intranet based applications
  • Transaction management, life-cycle management,
    resource pooling automatically handled
  • J2EE application model encapsulates the layers of
    functionality in specific types of components

5
J2EE Application Model
6
Types of EJB
  • Session Performs a task for a client
  • Entity Represents a business entity object that
    exists in
  • persistent storage
  • Message-Driven Acts as a listener for the Java
    Message
  • Service API, processing messages
    asynchronously
  • Examples?

7
Life Cycles
Stateful Session Bean
8
Life Cycles
Stateless Session Bean
9
Life Cycles
Entity Bean
10
Life Cycles
Message-Driven Bean
11
Client access to EJB
  • Client access only through interfaces
  • Remote access
  • May run on a different JVM
  • Web component, J2EE client, EJB, etc.
  • Location is transparent

12
A Session Bean Example
  • The CartEJB session bean represents a shopping
    cart in an online bookstore
  • Session bean class (CartBean)
  • Home interface (CartHome)
  • Remote interface (Cart)
  • Two helper classes BookException and IdVerifier

13
CartBean.java
import java.util. import javax.ejb. public
class CartBean implements SessionBean
String customerName String customerId
Vector contents public void
ejbCreate(String person) throws CreateException
if (person null) throw new
CreateException("Null person not allowed.")
else customerName person customerId
"0" contents new Vector()
public void ejbCreate(String person, String id)
throws CreateException if (person
null) throw new CreateException("Null person not
allowed.") else customerName person
IdVerifier idChecker new IdVerifier()
if (idChecker.validate(id)) customerId id
else throw new CreateException("Invalid id
" id) contents new Vector()
14
CartBean.java (cont)
  • public void addBook(String title)
  • contents.addElement(title)
  • public void removeBook(String title) throws
    BookException
  • boolean result contents.removeElement(titl
    e)
  • if (result false) throw new
    BookException(title "not in cart.")
  • public Vector getContents()
  • return contents
  • public CartBean()
  • public void ejbRemove()
  • public void ejbActivate()
  • public void ejbPassivate()
  • public void setSessionContext(SessionContext sc)

15
CartHome.java
  • import java.io.Serializable
  • import java.rmi.RemoteException
  • import javax.ejb.CreateException
  • import javax.ejb.EJBHome
  • public interface CartHome extends EJBHome
  • Cart create(String person) throws
    RemoteException, CreateException
  • Cart create(String person, String id) throws
    RemoteException, CreateException

16
Cart.java
import java.util. import javax.ejb.EJBObject
import java.rmi.RemoteException public
interface Cart extends EJBObject public void
addBook(String title) throws RemoteException
public void removeBook(String title) throws
BookException, RemoteException public Vector
getContents() throws RemoteException
17
Client code
import java.util. import javax.naming.Context i
mport javax.naming.InitialContext import
javax.rmi.PortableRemoteObject public class
CartClient public static void main(String
args) try Context initial
new InitialContext() Object objref
initial.lookup("MyCart") CartHome
home (CartHome)PortableRemoteObject.narrow
(objref, CartHome.class) Cart
shoppingCart home.create("Duke DeEarl","123")
shoppingCart.addBook("The Martian
Chronicles") shoppingCart.removeBook("
Alice in Wonderland")
shoppingCart.remove() catch
(BookException ex)
System.err.println("Caught a BookException "
ex.getMessage()) catch (Exception ex)
System.err.println("Caught an
unexpected exception!")
18
Industry Applications for EJB
  • Ford Financial Saves Money, Achieves Business
    Goals With Sun February 20, 2002
  • Amazon.com's ObjectStore Deployment Ensures
    Best-in-Class Experience for Customers And
    Merchants February 19, 2002
  • Borland Wins Again With Web Services Solution for
    Linux February 12, 2002
  • PointBase Demonstrates World's First Enterprise
    Data Synchronization Across Multiple Devices And
    Networks February 12, 2002
  • Over 40 licencees (who can ship J2EE products)
    including Nokia, Oracle, IBM, NEC, Compaq, BEA,
    etc.

19
Comparison with Microsoft
  • 78 percent viewed J2EE (Java 2 Enterprise
    Edition) server software as the most effective
    platform for building and deploying Web services
    to Microsofts .Net
  • (http//www.infoworld.com/articles/hn/xml/01/12/2
    1/011221hnjavasurvey.xml)
  • What is Microsofts corresponding technology?
  • Read handout!

20
Discussion
  • When to use which EJB?
  • When to use local and remote interfaces?
  • What would you choose Sun or Microsoft?
    (Remember Windows had crashed on the last day of
    your project submission and you lost it all ?)
  • Thank you
  • (ufarooq_at_vt.edu)

21
References
  • http//www.java.sun.com/j2ee\
  • http//java.sun.com/j2ee/tutorial/1_3-fcs/doc/J2ee
    TutorialTOC.html
  • Special Edition Using EJB 2.0 by Dan Chuck
    Cavaness and Brian Keeton
Write a Comment
User Comments (0)
About PowerShow.com