Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer...

18
Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Transcript of Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer...

Page 1: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

Enterprise Java Bean

2110472 Computer Networks

Natawut Nupairoj, Ph.D.

Department of Computer Engineering

Chulalongkorn University

Page 2: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

Outline

Overview. EJB Architecture. EJB Interfaces. Example.

Page 3: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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.

Page 4: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

Overview

Distributed Invoke EJB over the network

Interface vs. implementation.

Page 5: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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.

Page 6: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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).

Page 7: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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);

Page 8: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

EJB Basic Architecture

Page 9: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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).

Page 10: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

EJB Container

Page 11: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

EJB Conceptual View

Page 12: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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;

}

Page 13: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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;

}

Page 14: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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);

Page 15: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

Container-Managed Beanpublic 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) { }

Page 16: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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; }

Page 17: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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() { }}

Page 18: Enterprise Java Bean 2110472 Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

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.