May 30

EJB 3: Stateless Bean

No need to maintain the state of a bean between calls. //CalculatorBean.java@Statelesspublic class CalculatorBean implements CalculatorRemote, CalculatorLocal{   public int add(int x, int y)   {      return x + y;   }   public int subtract(int x, int y)   {      return x - y;   }}//CalculatorRemote.javaimport javax.ejb.Remote;@Remotepublic interface CalculatorRemote extends Calculator{}//CalculatorLocal.java import javax.ejb.Local;@Localpublic interface CalculatorLocal extends Calculator{}//Client.javaimport tutorial.stateless.bean.Calculator;import tutorial.stateless.bean.CalculatorRemote;import javax.naming.InitialContext;public class Client{   public static void main(String[] args) throws Exception   {      InitialContext ctx = new InitialContext();      Calculator calculator = (Calculator) ctx.lookup("CalculatorBean/remote");      System.out.println("1 + 1 = " + calculator.add(1, 1));      System.out.println("1 - 1 = " + calculator.subtract(1, 1));   }}

Skip to toolbar