j2ee part 2: enterprise javabeans csci 4300 images and code samples from jguru ejb tutorial,

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

Upload: justin-oswin-sims

Post on 11-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

EJB InterfacesEJB Interfaces

• Client: a Java program (servlet, bean)

• Home interface: local object

• Remote interface: access the actual EJB, possibly across a network

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

EntityBean creation codeEntityBean creation code

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

EntityBean business logicEntityBean business logic

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

EntityBean Callback methodsEntityBean Callback methods

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

Customer Data MembersCustomer Data Members

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

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:

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

Automatically generated SQL for Automatically generated SQL for this EntityBean:this EntityBean:

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

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

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

Stateless Session BeansStateless Session Beans

• Represent business processes (transactions)• Contain no persistent data• Shared among multiple clients

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

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

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

Acme SessionBean operationsAcme SessionBean operations

• Normally, we would open an output stream and write the request to the server!

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

EJB DescriptorEJB Descriptor

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

Assembly descriptorAssembly descriptor

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

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!

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

WAR and EAR filesWAR and EAR files

Buildfile source:

http://www.developer.com/java/data/article.php/10932_3405781_1