Writing A Standalone Java Test Client for Stateful EJB

  1. In the Package Explorer View make sure the Calculator project is selected.

  2. Create a new Java class. Open menu File > New > Other... and choose Java > Class.

  3. The package will be by.iba.client and the class name StatefulCalculatorClient.

    Leave the default options selected and be sure that public static void main(...) method is checked.

    Click the Finish button.

    StatefulCalculatorClient

  4. Add the following code to the public static void main method body:

    try {
    	Context jndiContext = new InitialContext();
    	Object ref = jndiContext.lookup("StatefulCalculatorBean/remote");
    	StatefulCalculator calc = (StatefulCalculator) PortableRemoteObject
    			.narrow(ref, StatefulCalculator.class);
    			
    	calc.clean();
    	double register = calc.add(4);
    	System.out.println(register + " + 3 = " + (register = calc.add(3)));
    	System.out.println(register + " - 3 = " + (register = calc.subtract(3)));
    	System.out.println(register + " * 3 = " + (register = calc.multiply(3)));
    	System.out.println(register + " / 3 = " + (register = calc.divide(3)));
    			
    	calc.turnOff();
    			
    	System.out.println("Trying to access removed bean...");
    	calc.clean();
    			
    } catch (Exception e) {
    	System.out.println(e.getMessage());
    }
    								

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

  5. Update client's CLASSPATH with required libraries.

    Right click the Calculator project in the Package Explorer View and select the Properties context menu.

    Select the Java Build Path option, go to Libraries tab:

    Packaging Configurations

    Click the Add External JARs... button and add the C:\jboss4\client\jboss-client.jar file to the CLASSPATH:

    jboss-client.jar

    Click the OK button.

  6. Run the Java class by selecting menu Run > Run As > Java Application.

    NOTE: Make sure the StatefulCalculatorClient.java is selected in Package Explorer View.

    You should see the following result in the Java client's Console View:

    4.0 + 3 = 7.0
    7.0 - 3 = 4.0
    4.0 * 3 = 12.0
    12.0 / 3 = 4.0
    Trying to access removed bean...
    Could not find Stateful bean: 93u4t67-2seebc-eu6srhdl-1-eu70q7f5-k								
    								

    If you switch to the Servers's Console View (use Display Selected Console button), you will see the server's output:

    Display Selected Console Button

    17:17:33,329 INFO  [STDOUT] [StatefulCalculatorBean] PostConstruct callback !
    17:17:33,391 INFO  [STDOUT] [StatefulCalculatorBean] Good bye ! I gotta split !
    17:17:33,407 INFO  [STDOUT] [StatefulCalculatorBean] PreDestroy callback !
    								

Congratulations ! You have succesfully tested EJB 3.0 application with Stateful Session Bean !!!

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