Sunday, February 6, 2011

EJB2,x example

EJB2.x
In EJB 2.x and earlier specifications, stateless session beans required two interfaces: the remote interface (or local interface) for defining business methods and the home interface for defining lifecycle methods. A session bean can also implement multiple interfaces.

package stockquote;import java.rmi.RemoteException;import javax.ejb.EJBObject;public interface StockQuote extends EJBObject {    public double getStockQuote(String Symbol) throws        RemoteException;} // Home Interface in EJB 2.x - StockQuoteHome.javapackage stockquote;import java.rmi.RemoteException;import javax.ejb.EJBObject;import javax.ejb.CreateException;public interface StockQuoteHome extends EJBHome {   public StockQuote create() throws RemoteException,      CreateException;} // Bean Implementation Class in EJB 2.x - StockQuoteBean.javapackage stockquote;import java.util.HashMap;import javax.ejb.SessionBean;import javax.ejb.SessionContext;public class StockQuoteBean implements SessionBean {    private SessionContext context = null;    private HashMap mapSQ = null;    public void setSessionContext(SessionContext context) {        this.context = context.    }    public void ejbRemove() {    }    public void ejbActivate() {    }    public void ejbPassivate() {    }    public void ejbCreate() {        System.out.println("ejbCreate() method called");        mapSQ = new HashMap();        mapSQ.put("INFY", new Double(44.85));        mapSQ.put("SUNW", new Double(4.99));        mapSQ.put("IBM", new Double(80.97));        mapSQ.put("MSFT", new Double(25.70));    }    public double getStockQuote(String symbol) {        double result = 0;        Object obj = mapSQ.get(symbol);        if(obj != null ) {            result = ((Double) obj).doubleValue();        }        return result;    }}
Home Interface has create to instantiate the bean in the container.
Remote Interface has call and follows RMI-IIOP rules ie Throws remote execption.
and input and return parameters must be serializable..

Session bean which implements SessionBean interface and implements lifecycle methods.

Must have 2 xml files
ejb-jar.xml: Required by all EJBs; has been standardized by the EJB specification.
xxx.xml: A vendor-specific deployment descriptor used to define the behavior of the container. This is not portable

ejb-jar file  has all there relevant description
<?xml version="1.0"?><!DOCTYPE ejb-jar PUBLIC'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'><ejb-jar><enterprise-beans>    <session>        <ejb-name>StockQuoteEJB</ejb-name>        <home>stockquote.StockQuoteHome</home>        <remote>stockquote.StockQuote</remote>        <ejb-class>stockquote.StockQuoteBean</ejb-class>        <session-type>Stateless</session-type>        <transaction-type>Container</transaction-type>    </session></enterprise-beans></ejb-jar>
and now deploy this ejb and call from client.

Client
import javax.rmi.PortableRemoteObject;import javax.naming.InitialContext;import stockquote.StockQuoteHome;import stockquote.StockQuote;public class StockQuoteClient {   public static void main(String arg[]) {      try {         InitialContext context = new InitialContext ();         Object objRef = context.lookup("StockQuoteHome");         StockQuoteHome homeObj = (StockQuoteHome)            PortableRemoteObject.narrow(objRef,               StockQuoteHome.class);         StockQuote sqObj = homeObj.create();         double result = sqObj.getStockQuote("INFY");         System.out.println("Stock Price of Symbol is:"            +result);         } catch (Exception err) {            System.out.println(err.getMessage());         }   }}

No comments:

Post a Comment