Ejb examples

591 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
591
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ejb examples

  1. 1. Example: Stateless Session BeanThe Home Interfaceimport javax.ejb.*;import java.rmi.RemoteException;public interface HelloHome extends EJBHome { Hello create() throws RemoteException, CreateException;}
  2. 2. The Remote Interfaceimport javax.ejb.*;import java.rmi.RemoteException;import java.rmi.Remote;public interface Hello extends EJBObject { public String hello() throws java.rmi.RemoteException;}The Bean Classpublic class HelloBean implements SessionBean { // these are required session bean methods public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext ctx) {} // this is our business method to be called by the client public String hello() { return “Hello, world!”; }}
  3. 3. Session Bean Required Methods The following methods are defined in the SessionBean interface: void ejbCreate(…init params…) Performs necessary initializations when an instance is created void ejbRemove() Prepare for the bean’s destruction void ejbActivate() Prepare needed resources to return from hibernation void ejbPassivate() Releases any resources (database connections, files) before an instance is put into hibernation void setSessionContext(SessionContext sc) Stores the session context (used for communicating with the EJB container)The EJB Clientpublic class HelloClient { public static void main(String[] args) { try { InitialContext ic = new InitialContext(); Object objRef = ic.lookup("java:comp/env/ejb/Hello"); HelloHome home = (HelloHome)PortableRemoteObject.narrow( objRef, HelloHome.class); Hello hello = home.create(); System.out.println( hello.hello() ); hello.remove(); } catch (Exception e) { e.printStackTrace(); } }}
  4. 4. Example: Stateful Session BeanThe Home Interfaceimport javax.ejb.*;import java.rmi.RemoteException;public interface CountHome extends EJBHome { Count create() throws RemoteException, CreateException;}
  5. 5. The Remote Interfaceimport javax.ejb.*;import java.rmi.RemoteException;import java.rmi.Remote;public interface Count extends EJBObject { public int count() throws java.rmi.RemoteException;}The Bean Classpublic class CountBean implements SessionBean { private int val = 0; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext ctx) {} // this is our business method to be called by the client public int count() { return ++val; }}
  6. 6. The EJB Clientpublic class CountClient { public static void main(String[] args) { try { InitialContext ic = new InitialContext(); Object objRef = ic.lookup("java:comp/env/ejb/Count"); CountHome home = (CountHome) PortableRemoteObject.narrow(objRef, CountHome.class); Count count = home.create(); System.out.println(“Count:”+count.count() ); System.out.println(“Count:”+count.count() ); System.out.println(“Count:”+count.count() ); count.remove(); } catch (Exception e) {} }} Entity Beans
  7. 7. What Are Entity Beans? They represent data elements Unlike session beans, entity beans last longer than a client session Entity beans last as long as the data they represent lasts In reality, entity beans are a data access wrapper (e.g. database encapsulation) When entity bean’s data is modified, so is the representative data in the database (& vice versa) Whether this is done by the container, or explicitly by the programmer, depends on what kinds of Entity bean it isEntity Bean Data Mappings Entity Bean pkField SomeField … … 1 … … … Entity Bean 2 … … … 3 … … … Entity Bean Entity Bean
  8. 8. Entity Bean Facts The persistent data represented by the entity bean is stored safely Usually in a database Entity beans keep their data even after a crash Multiple entity beans can represent the same data in the database Used for handling multiple clients who want to access the same data Entity beans can be re-used Entity beans of a given type may be placed into a pool and reused for different clients The data the entity beans represent will change Example: Entity Beans Container-Managed Persistence (CMP)
  9. 9. The Home Interface import javax.ejb.*; import java.rmi.RemoteException; import java.util.*; public interface AccountHome extends EJBHome { public Account create(Integer accountID, String owner) throws RemoteException, CreateException; public Account findByPrimaryKey(Integer accountID) throws FinderException, RemoteException; } The Remote Interfaceimport javax.ejb.*;import java.rmi.*;public interface Account extends EJBObject { public void deposit(double amount) throws RemoteException; public void withdraw(double amount) throws RemoteException; public double getBalance() throws RemoteException;}
  10. 10. The Bean Classimport javax.ejb.*;import java.rmi.*;import java.util.*;public abstract class AccountBean implements EntityBean {public abstract void setAccountID(Integer accountID);public abstract Integer getAccountID();public abstract void setBalance(double balance);public abstract bouble setBalance();public abstract void setOwnerName(String ownerName);public abstract String getOwnerName(); public Integer ejbCreate(int accountID, String owner) throws CreateException { setAccountID(accountID); setOwnerName(owner); return new Integer(0); } public void ejbPostCreate(int accountID, String owner) {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbLoad() {} public void ejbStore() {} public void setEntityContext(EntityContext ctx) {} public void setEntityContext() {} The Bean Class (contd.) public void withdraw(double amount) { setBalance( getBalance() - amount ); } public void deposit(double amount) { setBalance( getBalance() + amount ); } }
  11. 11. The EJB Clientpublic class AccountClient { public static void main(String[] args) { try { Context ctx = new InitialContext(); Object ref = ctx.lookup(“java:comp/env/ejb/Account”); AccountHome home = (AccountHome) PortableRemoteObject.narrow(objRef, AccountHome.class); Account newAccount = home.create(123, “John Smith”); newAccount.deposit(100); newAccount.remove(); Collection accounts = home.findByOwnerName(“John Smith”); } catch (Exception e) {} }} Enterprise Applications
  12. 12. J2EE application modelJ2EE is a multitiered distributed application model client machines the J2EE server machine the database or legacy machines at the back end Resource Pooling
  13. 13. Database Connection Pooling Connection pooling is a technique RDBMSthat was pioneered by databasevendors to allow multiple clients toshare a cached set of connectionobjects that provide access to adatabase resource Connection Connection pools minimize the Poolopening and closing of connections Servlet Client 1 …… Client nReferences Developing Enterprise Applications Using the J2EE Platform, http://java.sun.com/developer/onlineTraining/J2EE/Intro2/j2ee.html

×