Presentation is loading. Please wait.

Presentation is loading. Please wait.

J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,

Similar presentations


Presentation on theme: "J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,"— Presentation transcript:

1 J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial, http://java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.html

2 EJB container Created by an application server program We use SUN Application Server on zion.cs.uga.edu Must write EJB to specified interfaces

3 EJB Interfaces Client: a Java program (servlet, bean) Home interface: local object Remote interface: access the actual EJB, possibly across a network

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

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

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

7 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

8 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

9 Stub and Skeleton Methods Stub method on local system represents the remote object Skeleton method on remote system represents the local client

10 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

11 EntityBean creation code

12 EntityBean business logic

13 EntityBean Callback methods

14 Customer Data Members

15 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:

16 Automatically generated SQL for this EntityBean:

17 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

18 Stateless Session Beans Represent business processes (transactions) Contain no persistent data Shared among multiple clients

19 Acme SessionBean creation JNDI: Java Directory and Naming Interface (find objects in a networked environment) InitialContext: provided by EJB container, gives JNDI access

20 Acme SessionBean operations Normally, we would open an output stream and write the request to the server!

21 EJB Descriptor

22 Assembly descriptor

23 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!

24 WAR and EAR files Buildfile source: http://www.developer.com/java/data/article.php/10932_3405781_1


Download ppt "J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,"

Similar presentations


Ads by Google