180 likes | 301 Views
Enterprise Java Bean. 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University. Outline. Overview. EJB Architecture. EJB Interfaces. Example. Overview. What is EJB ? Component-based Server-side (similar to Servlet).
E N D
Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University
Outline • Overview. • EJB Architecture. • EJB Interfaces. • Example.
Overview • What is EJB ? • Component-based Server-side (similar to Servlet). • Same as Servlet ? • Transaction • Atomic execution of multiple operations. • Ex: Transfer money between two accounts • Withdraw from the source account. • Deposit to the destined account. • Must execute all-or-nothing.
Overview • Distributed • Invoke EJB over the network • Interface vs. implementation.
Overview • The caller feels like invoke local class instance • Use method as you call Java class. • Location independence • Can be on any server. • Advance techniques: load balancing, fault tolerance, … • Security • Who can use what ? • Access control.
Data as an Object • Typical JSP: • Built-in SQL statement in JSP page. • Problems ? • For the user of EJB • Data are objects. • Each field is a data member of a Java class. • No direct contact to database (let EJB handle it).
Example: Using EJB Integer id = new Integer(351); Customer cust = custHome.findByPrimaryKey(id); System.out.println(cust.getName()); Customer customer = custHome.create(new Integer(804)); Name name = new Name("Richard", "Wayne", "Monson-Haefel"); customer.setName(name);
EJB Type • Entity Bean • Data object with actual storage • Room, customer, etc. • Permanent / persistence. • Container-managed vs. bean-managed. • Session Bean • Business processes / methods • Reserve_room, availableRoom, etc. • Stateless (one action) vs. stateful (sequence of actions).
EJB Home Interface • Allow client to manage the bean (factory) • Create a new bean or find the existing bean. • Developer must define the primary key (type). • No need to write “Implementation”. public interface CustomerHome extends EJBHome { public Customer create(Integer customerNumber) throws RemoteException, CreateException; public Customer findByPrimaryKey(Integer customerNumber) throws RemoteException, FinderException; public Enumeration findByZipCode(int zipCode) throws RemoteException, FinderException; }
EJB Remote Interface • Interface to the actual data • Define how the data looks like. • Access (get/set) data in each field. public interface Customer extends EJBObject { public Name getName() throws RemoteException; public void setName(Name name) throws RemoteException; public Address getAddress() throws RemoteException; public void setAddress(Address address) throws RemoteException; }
Ex: Using EJB Interfaces Integer id = new Integer(351); Customer cust = custHome.findByPrimaryKey(id); System.out.println(cust.getName()); Customer customer = custHome.create(new Integer(804)); Name name = new Name("Richard", "Wayne", "Monson-Haefel"); customer.setName(name);
Container-Managed Bean public class CustomerBean implements EntityBean { int customerID; Address myAddress; Name myName; CreditCard myCreditCard; // CREATION METHODS public Customer ejbCreate(Integer id) { customerID = id.intValue(); return null; } public void ejbPostCreate(Integer id) { } public Customer ejbCreate(Integer id, Name name) { myName = name; return ejbCreate(id); } public void ejbPostCreate(Integer id, Name name) { }
Container-Managed Bean // BUSINESS METHODS public Name getName() { return myName; } public void setName(Name name) { myName = name; } public Address getAddress() { return myAddress; } public void setAddress(Address address) { myAddress = address; } public CreditCard getCreditCard() { return myCreditCard; } public void setCreditCard(CreditCard card) { myCreditCard = card; }
Container-Managed Bean // CALLBACK METHODS public void setEntityContext(EntityContext cntx) { } public void unsetEntityContext() { } public void ejbLoad() { } public void ejbStore() { } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { } }
References • jGuru, Enterprise JavaBeans Technology Fundamentals, http://developer.java.sun.com/developer/onlineTraining/. • EJB Tutorial, http://www.ejbtut.com/Overview.jsp. • A. Hemrajani, The state of Java middleware, Part 2: Enterprise JavaBeans, JavaWorld, April 1999, http://www.javaworld.com/javaworld/jw-04-1999/jw-04-middleware_p.html.