INTRODUCTION

A J2EE application client is packaged in a client JAR file. Eventually, the client JAR file is packaged within a EAR file. It is generally a good practice to package the client JAR file in a different EAR than the EAR that contains the EJB JAR file. The client EAR can be distributed widely without the risk of exposing the implementation business logic included in the EJB.

In this tutorial, we will develop a client application for the square root calculator EJB developed in the WASKB017tutorial.

CREATE THE CLIENT PROJECTS

Make sure that you have completed the tutorial in WASKB017 and the application developed there is fully functional.

Launch RAD v6. Switch to the J2EE perspective. In the Project Explorer view, right click on Application Client Projects. Select New->Application Client Project. As the name of the project enter SquareRootClient. Click on the Show Advanced button. By default, system sets the EAR file name to SquareRootClientEAR. That’s good enough. System also offers to create a main class. When the client EAR is launched, the main method of the main class gets executed. Uncheck this check box. We will manually configure the main class so that we know what is required to setup a main class.

Click on Finish.

ADD EJB REFERENCES

We need to build two types of references to the EJB.

  1. We need reference to the home and remote interface class. This is required for the client code to compile and execute.
  2. EJB JNDI reference.

In Project Explorer expand Enterprise Applications->SquareRootClientEAR. Double click on Deployment Descriptor. Click on the Module tab. Under Project Utility JARs section, click on Add. Select the Test EJBClient project and click on Finish. Save and close the deployment descriptor editor. Recall, in the previous tutorial, the “Test EJBClient” was created to hold home, remote and stub classes for the EJBs in the “Test EJB” project.

Right click on the SquareRootClient project and select Properties. Select the Java JAR Dependencies property. Make sure that the Use EJB Client JARs option is selected. Check the checkbox next to Test_EJBClient.jar.

Click on OK.

Now, we will add a JNDI reference. Expand SquareRootClient and double click on Deployment Descriptor. Click on the References tab. Click on Add. Select EJB reference. Click on Next. Select the SimpleSession EJB as shown below.

System chooses the refernce name to ejb/SimpleSession (that means, in our code, we will have to look up “java:comp/env/ejb/SimpleSession”). We will leave it like that. Click on Finish. Notice the WebSphere Bindingssection of the deployment descriptor. System has automatically filled in the JNDI name of the EJB.

Save and close the deployment descriptor.

DEVELOP THE CLIENT CODE

Right click on the SquareRootClient project and select New->Class. Set the package name to com.webage.clients. Set the class name to TestClient. Click on Finish.

Add these import statements to the class.

import javax.naming.*; 
import javax.rmi.PortableRemoteObject; 
import com.webage.ejbs.*;

Write a main method in the class as shown below.

public static void main(String[] args) {
 try {
 Context ctx = new InitialContext();
 Object o = ctx.lookup("java:comp/env/ejb/SimpleSession");
 SimpleSessionHome sh = (SimpleSessionHome) 
 PortableRemoteObject.narrow(o, SimpleSessionHome.class);
 SimpleSession s = sh.create();

 System.out.println("Test result is: " + 
 s.getSuqareRoot(100.00).getResultValue());
 } catch (Exception e) {
 e.printStackTrace();
 }
}

Save changes.

CONFIGURE THE MAIN CLASS

Double click on the Deployment Descriptor of the SquareRootClient project. Click on the Overview tab. Under the Main Class section click on Edit. System opens the JAR Dependency Editor. Under the Main Class section, click on Browse and select TestClient. Save and close the JAR Dependency Editor. Back in the Client Deployment Descriptor editor, click on the Refresh button in the Main Class section. Close the editor.

TEST CLIENT

The client needs the EJB to be running in the server. Start the server if it is not running. Wait for the server to start.

Right click on the SquareRootClient project and select Run->Run. From the Configurations list, select WebSphere v6.0 Application Client. Click on New. Change the name to Square root Client.

Click on Apply. Click on Run. In the Console view, you should see the output.

To run the client again, you can simply use the run toolbar button.

CONCLUSION

In this tutorial, we built a standalone client application that has minimal dependency on the actual EJB project. The client can be deployed in any machine where WebSphere Application Server v6.0 or WebSphere Client v6.0 runtime is installed.

RESOURCES

  1. WA1283 J2EE Programming Using Rational Application Developer (RAD) v6