Download Enterprise Java Bean

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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


Location independence



Use method as you call Java class.
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 Basic Architecture
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 Container
EJB Conceptual View
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
customerID = id.intValue();
return null;
}
public void ejbPostCreate(Integer
public Customer ejbCreate(Integer
myName = name;
return ejbCreate(id);
}
public void ejbPostCreate(Integer
id) {
id) { }
id, Name name) {
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
public void
public void
public void
public void
public void
public void
public void
}
METHODS
setEntityContext(EntityContext cntx) { }
unsetEntityContext() { }
ejbLoad() { }
ejbStore() { }
ejbActivate() { }
ejbPassivate() { }
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-04middleware_p.html.
Related documents