Sunday, February 6, 2011

EJB 3.0


Business Interface - @Remote - accessed by remote Client
                             @Local - accessed by local Client
Home Interface used to initialize the bean used only in ejb2.x

The business interfaces are annotated using @Remote and @Local annotations. Note that the @Remote annotation lets the container know that the bean will be accessed by remote clients and the @Local annotation lets the container know that the bean will be accessed by local clients

// Business Interface in EJB 3.0 - StockQuote.javapackage stockquote;import javax.ejb.Remote;@Remotepublic interface StockQuote {    public double getStockQuote(String Symbol);}

// Bean Implementation Class in EJB 3.0 - StockQuoteBean.javapackage stockquote;import javax.ejb.Stateless;@Statelesspublic class StockQuoteBean implements stockquote.StockQuote {private HashMap mapSQ = null;    public StockQuoteBean() {        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;    }}
eg of multiple implementation
@Local ({StockQuoteLocal.java})@Remote ({StockQuote.java})public class StockQuoteBean implements    StockQuoteLocal, StockQuote {.............................................}
Client
//EJB 3.0-Session EJB Client - StockQuoteClient.javapackage client;import javax.ejb.EJB;import stockquote.StockQuote;public class StockQuoteClient {   @EJB   private static StockQuote stockquote;   public static void main(String[] args) {      try {         double result = stockquote.getStockQuote("INFY");         System.out.println("Stock Price of Symbol is:            "+result);      } catch (Exception ex) {         System.err.println("Caught an exception!");         ex.printStackTrace();      }   }}
--
Regards
+91-9890633732
 Reply Forward

No comments:

Post a Comment