J2EE Part 2: Enterprise JavaBeans

Download Report

Transcript J2EE Part 2: Enterprise JavaBeans

J2EE Part 2:
Enterprise JavaBeans
CSCI 4300
Images and code samples from jGuru EJB tutorial,
http://java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.html
EJB container
• Created by an
application server
program
• We use SUN
Application Server on
zion.cs.uga.edu
• Must write EJB to
specified interfaces
EJB Interfaces
• Client: a Java
program (servlet,
bean)
• Home interface: local
object
• Remote interface:
access the actual
EJB, possibly across
a network
EJB Interface example
• CustomerHome home = // ...
• // Use the home interface to
create a new instance of
the Customer bean.
• Customer customer =
home.create(customerID);
• // using a business method
on the Customer.
• customer.setName(someName);
Entity beans represent data
objects:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
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;
}
Session Beans represent business
processes:
public interface HotelClerk extends EJBObject {
public void reserveRoom(Customer cust,
RoomInfo ri, Date from, Date to)
throws RemoteException;
public RoomInfo availableRooms( Location loc,
Date from, Date to)
throws RemoteException; }
An Entity Bean class
• The Bean class actually
implements the
EntityBean interface
(business logic)
• For example, maintains a
database connection for
customer info
• Client reads and writes
the bean, and does not
need to do DB access
EJB lifecycle methods (home
interface)
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;
}
-- these return instances of Customer, the remote interface
Stub and Skeleton Methods
• Stub method on local system represents the
remote object
• Skeleton method on remote system represents
the local client
Container-Managed Persistence
• Persistence: arranging that data stored
will be available in a later session
• Container-Managed Persistence: EJB
container automatically saves EntityBean
contents to a database and restores on
demand
• Developer writes no DB code!
• Easiest for developer, hardest for EJB
container
EntityBean creation code
EntityBean business logic
EntityBean Callback methods
Customer Data Members
Serializable objects
• Serializable: an object can be saved to a string and
correctly restored from the string
• A class whose data members are primitive values is
automatically serializable
• Classes that use trees, etc. can be made serializable:
Automatically generated SQL for
this EntityBean:
Bean-Managed persistence
The entity bean itself must contain the DB
code to:
• Store the bean data contents into the
database
• Restore the bean from its stored contents
These methods invoked as callbacks from
the EJB container
Stateless Session Beans
• Represent business processes (transactions)
• Contain no persistent data
• Shared among multiple clients
Acme SessionBean creation
• JNDI: Java Directory and Naming Interface (find
objects in a networked environment)
• InitialContext: provided by EJB container, gives
JNDI access
Acme SessionBean operations
• Normally, we would open an output stream and
write the request to the server!
EJB Descriptor
Assembly descriptor
EJB Deployment
The EJB jar file contains:
• Home interface class
• Remote interface class
• Bean implementation class
• META-INF/ejb-jar.xml
Then, drop it into the JBOSS deploy
directory!
WAR and EAR files
Buildfile source:
http://www.developer.com/java/data/article.php/10932_3405781_1