Title: Enterprise JavaBeans Umer Farooq
1Enterprise JavaBeansUmer Farooq
CS6704 Design Patterns Component Frameworks
February 25, 2002
2Enterprise 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.
3Agenda
- 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
4J2EE 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
5J2EE Application Model
6Types 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?
7Life Cycles
Stateful Session Bean
8Life Cycles
Stateless Session Bean
9Life Cycles
Entity Bean
10Life Cycles
Message-Driven Bean
11Client 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
12A 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
13CartBean.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()
14CartBean.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)
-
15CartHome.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 -
16Cart.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
17Client 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!")
18Industry 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.
19Comparison 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!
20Discussion
- 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)
21References
- 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