Defining A Stateful Session Bean

  1. Add business method signatures to the StatefulCalculator interface:

    public double add(double a);
    public double subtract(double a);
    public double multiply(double a);
    public double divide(double a);
    public void clean();
    
    public void turnOff();								
    								

  2. In the Package Explorer View, right click on the StatelessCalculatorBean class and select Source > Override/Implement Methods....

    Implement Methods

  3. Accept the default selection and press the OK button:

    Implement Methods - Default

  4. Open the StatefulCalculatorBean.java file (if not already open), add instance variables and modify the newly created methods as shown below:

    @EJB private StatelessCalculator calculator;
    	
    double register = 0;
    
    public double add(double a) {		
    	register = calculator.add(register, a); 
    	return register;
    }
    
    public double subtract(double a) {
    	register = calculator.subtract(register, a);
    	return register;
    }
    	
    public double multiply(double a) {
    	register = calculator.multiply(register, a);
    	return register;
    }
    
    public double divide(double a) {		
    	register = calculator.divide(register, a);
    	return register;
    }
    
    public void clean() {
    	register = 0;		
    }
    
    @Remove
    public void turnOff() {
    	System.out.println("[StatefulCalculatorBean] Good bye ! I gotta split !");
    }
    	
    @SuppressWarnings("unused")
    @PostConstruct
    private void afterCreated() {
    	System.out.println("[StatefulCalculatorBean] PostConstruct callback !");		
    }
    
    @SuppressWarnings("unused")
    @PreDestroy
    private void beforeRemoved() {
    	System.out.println("[StatefulCalculatorBean] PreDestroy callback !");		
    }
    								

    NOTE: To resolve imports problems press Shift + Ctrl + O. Press Ctrl + S to save the Java class.

  5. The Statful Session EJB is complete. With EJB 3, there is no need to code deployment descriptors or the home interface. The next step is to package the EJB in the EJB JAR file and deploy on the server.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine