j2ee part 2: enterprise javabeans csci 4300 images and code samples from jguru ejb tutorial,
TRANSCRIPT
J2EE Part 2:J2EE Part 2:Enterprise JavaBeansEnterprise JavaBeans
CSCI 4300CSCI 4300Images and code samples from jGuru EJB tutorial,Images and code samples from jGuru EJB tutorial,http://java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.htmlhttp://java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.html
EJB containerEJB container
• Created by an application server program
• We use SUN Application Server on zion.cs.uga.edu
• Must write EJB to specified interfaces
EJB InterfacesEJB Interfaces
• Client: a Java program (servlet, bean)
• Home interface: local object
• Remote interface: access the actual EJB, possibly across a network
EJB Interface exampleEJB 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 Entity beans represent data objects: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 Session Beans represent business processes: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 classAn 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 EJB lifecycle methods (home interface)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 MethodsStub and Skeleton Methods
• Stub method on local system represents the remote object
• Skeleton method on remote system represents the local client
Container-Managed PersistenceContainer-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 codeEntityBean creation code
EntityBean business logicEntityBean business logic
EntityBean Callback methodsEntityBean Callback methods
Customer Data MembersCustomer Data Members
Serializable objectsSerializable 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 Automatically generated SQL for this EntityBean:this EntityBean:
Bean-Managed persistenceBean-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 BeansStateless Session Beans
• Represent business processes (transactions)• Contain no persistent data• Shared among multiple clients
Acme SessionBean creationAcme SessionBean creation
• JNDI: Java Directory and Naming Interface (find objects in a networked environment)
• InitialContext: provided by EJB container, gives JNDI access
Acme SessionBean operationsAcme SessionBean operations
• Normally, we would open an output stream and write the request to the server!
EJB DescriptorEJB Descriptor
Assembly descriptorAssembly descriptor
EJB DeploymentEJB 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 filesWAR and EAR files
Buildfile source:
http://www.developer.com/java/data/article.php/10932_3405781_1