Skip to main content

skip to main content

developerWorks  >  WebSphere  >

Using JNDI with the WebSphere Application Server J2EE Thin Application Client

developerWorks
Document options

Document options requiring JavaScript are not displayed

Sample code


Rate this page

Help us improve this content


Level: Introductory

Kulvir Bhogal (kbhogal@us.ibm.com), Consultant, IBM Software Services for WebSphere, IBM Austin
Kwang Kang (kwang@us.ibm.com), Consultant, IBM Software Services for WebSphere, IBM Charlotte

08 Oct 2003

The IBM Java Application thin client provides a lightweight environment that can access J2EE resources externally via Java Naming and Directory Interface (JNDI), thus avoiding the overhead of a J2EE platform on the client machine. This article shows you how to use the thin client to locate a remote object via a simple Java application.

Introduction

Naming services manage a set of namespaces, resolving name bindings to data objects. The JavaTMNaming and Directory Interface (JNDI) is an API that provides access to naming and directory services. Many Java applications use JNDI to locate resources such as datasources and Enterprise JavaBeans (EJBs) registered in the naming service of a Java 2 Enterprise Edition (J2EE) server.

The IBM®Java thin application client provides a lightweight environment capable of accessing J2EE resources externally via JNDI, enabling you to avoid the overhead of maintaining the J2EE platform on the client machine.



Back to top


Creating a resource to find

In this section you will create a simple stateless session bean, implement a method, and deploy the EJB to WebSphere®Application Server. The remote thin application client that we will build later in this paper will resolve this EJB via JNDI and invoke the exposed method.

WebSphere Studio Application Developer V5.0 (hereafter called Application Developer) will be used for this exercise. The method of locating and invoking a remote object described in this article is valid only with the Java Runtime Environment included with WebSphere Application Server V5.

Creating a J2EE enterprise application project

Start Application Developer and create a J2EE enterprise application project:

  1. Select File => New Enterprise Application Project .
  2. From the Enterprise Application Project Creation window, select Create J2EE 1.3 Enterprise Application project
    Figure 1. Create a J2EE 1.3 Enterprise Application project
    Create a J2EE 1.3 Enterprise Application project
  3. Click Next .
  4. From the Enterprise Application Project Creation window, name the project Simple . For additional module projects, select EJB Module and name the module SimpleEJB
    Figure 2. Create an EJB module for the Enterprise Application project.
    Create an EJB Module
  5. Click Finish .

Creating an EJB module

From the Application Developer J2EE Hierarchy view, expand the EJB Modules folder to reveal the newly created module. Associate a new stateless session bean to this module:

  1. Right-click on EJB Module => SimpleEJBModule and select New => Enterprise Bean .
  2. From the Enterprise Bean Creation window, confirm that SimpleEJB project is selected:
    Figure 4. Create an Enterprise Bean associated to the SimpleEJBModule
    Associate the bean to the proper module
  3. Click Next .
  4. From the Create a 2.0 Enterprise Bean window, specify Session bean as the bean type. Also specify the bean name as SimpleEJB and the default package as com.ibm.simple
    Figure 5. Specify the EJB 2.0 type, bean name, and default package.
    Basic bean properties
  5. Click Next.
  6. From the Enterprise Bean Details window, do not modify any of the default settings. However, notice the EJB binding name, which will be discussed in more detail below:
    Figure 6. Create the remote/home interfaces for the bean.
    Bean's Remote and Home Interfaces
  7. Click Finish.

Implementing an EJB method

Expand the newly created EJB to reveal the EJB and its remote/home interfaces. Now add a method to the EJB:

  1. Double-click on SimpleEJBBean to edit the EJB code (see figure 7). After the automatically generated EJB methods, add the following method:
    public String echoMe(String whatToEcho)
    	{
    		return("I am echoing: " + whatToEcho);
    	}


    Figure 7. Edit the SimpleEJBBean.
    SimpleEJBBean
  2. From the Outline view, right-click on the echoMe method, select Enterprise Bean => Promote to Remote Interface
    Figure 8. Promote the method to the Remote Interface.
    Promote to Remote Interface
  3. From the J2EE Hierarchy view, right-click on SimpleEJBModule and select Generate => Deploy and RMIC code
    Figure 9. Generate the Deployment and RMIC Code.
    Generate Deploy and RMIC
  4. From the J2EE Navigator view, expand the com.ibm.simple package and select the following files: _SimpleEJB_Stub.java, _SimpleEJBHome_Stub.java, SimpleEJB.java, SimpleEJBHome.java . Right-click and select Export
    Figure 10. Export the generated stubs and EJB Interfaces.
    Export the EJB Interfaces and Stubs
  5. From the Export window, select JAR file and click Next .
  6. From the JAR Export window, enter the full path and file name of the JAR:
    Figure 12. Define the resources to package into the JAR.
    Define Resources for Jar

Deploying a J2EE enterprise application

The enterprise application will now be deployed to the WebSphere Application Server Single Server Edition embedded in Application Developer. Follow the instructions below to create and configure the server environment:

  1. From the J2EE Hierarchy view, right-click on Servers and select New => Server and Server Configuration .
  2. From the Create a New Server and Server Configuration window, specify the server name as WAS5TestServer and the server type as WebSphere version 5.0 Test Environment:
    Figure 14. Create a WebSphere version 5.0 Test Environment.
    Create a WebSphere 5.0 Test Environment
  3. Click Finish.

In the J2EE Hierarchy View, expand the Server Configuration folder to reveal the WebSphere Application Server V5 Test Environment Server that you just created. Deploy the enterprise application:

  1. Right-click on the WAS5TestServer server configuration and select Add => Simple .
  2. Expand the EJB Modules folder, right-click on SimpleEJBModule , and select Run on Server .


Back to top


Preparing the Environment

Now we need to prepare the external environment by installing the WebSphere Application Server Application Client V5.0, which is available on the WebSphere Application Server V5.0 install CDs.

After installation, go to the bin directory where the Application Client was installed (the default location is C:\Program Files\WebSphere\AppClient\bin ). In this directory, you'll notice the setupclient.bat file, which prepares our thin application client environment. Let#x2019s examine this file more closely:

  1. Open the setupClient.bat file in your favorite editor. Specify the following parameter values (see figure 17):
    1. WebSphere allows for several different client application types including the pluggable client, J2EE client, and the J2EE Thin client. This exercise illustrates the J2EE Thin client. Ensure that the following parameter value exists:
      SET CLIENT_TYPE=J2EETHIN
    2. The default server name and port number are required. The server name is the accessible hostname or IP address of the server housing the enterprise application. The server port number is the server#x2019s ORB port number. This exercise houses both the server and client on the same machine (your local machine).
      SET DEFAULTSERVERNAME=localhost
      SET SERVERPORTNUMBER=2809
  2. Save the file.
    Figure 17. Modify setting parameters in setupClient.bat
    Parameters in setupClient.bat
  3. Open a command window and run the setupclient batch file:
    Figure 18. From the command line, run the setupclient batch file.
    Run the setupclient.bat



Back to top


Calling the object externally

We are now ready to externally interact with our EJB. The client application code is shown below and is also available in the archive file associated with this article.

				import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.ibm.simple.*;
import com.ibm.simple.SimpleEJBHome;

public class ExternalCaller
{
   public static void main(String[] args)
   {
      try
      {
         Context initialContext = new InitialContext();
         System.out.println("Calling EJB Externally");
         String lookupString =
            "cell/nodes/localhost/servers/server1/ejb/com/ibm/simple/SimpleEJBHome";
         Object obj =
            initialContext.lookup(lookupString);
         SimpleEJBHome ejbHome =
            (SimpleEJBHome) javax.rmi.PortableRemoteObject.narrow(obj,SimpleEJBHome.class);
         SimpleEJB simpleEJB = ejbHome.create();
         System.out.println(simpleEJB.echoMe("I worked!"));
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
}			

The code for locating and invoking our remote object is straightforward. Notice the JNDI name: cell/nodes/localhost/servers/server1/ejb/com/ibm/simple/SimpleEJBHome . There are two ways to obtain this string. The first is to run the dumpnamespace batch file provided by the WebSphere Application Server Application Client:

  1. From the command line, run dumpnamespace.bat . Locate the JNDI name associated with the SimpleEJBHome
    Figure 19. The dumpnamespace displays all objects in the context.
    Dumpnamespace utility

The alternate method is to concatenate the prefix string ( cell/nodes/localhost/servers/server1/ ) located in the scope field in the Server Configuration (see figure 20) with the EJB Binding name ( ejb/com/ibm/simple/SimpleEJBHome). The JNDI name can also be found in the Universal Test Client. One approach would be to seperate the lookupString into two parts, PREFIX and OBJECT, where PREFIX would be cell/nodes/localhost/servers/server1/ and OBJECT would be ejb/com/ibm/simple/SimpleEJBHome.


Figure 20. The scope can be found in the WebSphere Server Configuration.
WebSphere Server Configuration Scope Field

Save the file as ExternalCaller.java and execute the following command to compile the Java class:

Javac -classpath <location of SimpleEJBStubs.jar>;
	C:\progra~1\websphere\applclient\lib\j2ee.jar ExternalCaller.java


Figure 21. Compile the ExternalCaller.java application.
Compile the Java Application


Back to top


Running the thin application client

To execute our external Java application, make sure your environment is prepared and use a Java command of the following form to invoke the client application:

%JAVA_HOME%/bin/java -Xbootclasspath/p:%WAS_BOOTCLASSPATH%
-classpath <list of the referenced jars, classes, and resource directories>
-Djava.ext.dirs=%WAS_EXT_DIRS%
-Djava.naming.provider.url=iiop://<server:orb port>
-Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
%SERVER_ROOT% %CLIENTSAS% <fully qualified main class of the application client>;

For this exercise, the following command was used. We suggest using a batch file to execute the process -- a batch file called go.bat is included in the project ZIP file.

%JAVA_HOME%/bin/java -Xbootclasspath/p:%WAS_BOOTCLASSPATH%
-classpath %WAS_CLASSPATH%;c:\jars\SimpleEJBStubs.jar;C:\articles
-Djava.ext.dirs=%WAS_EXT_DIRS%
-Djava.naming.provider.url=iiop://localhost:2809
-Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
%SERVER_ROOT% %CLIENTSAS% ExternalCaller


Figure 22. Run the Thin Application Client.
Run the Thin Application Client


Back to top


Conclusion

External application clients can access J2EE resources more easily by taking advantage of the IBM Java thin application client. Advantages include environment preparation and minimal footprint on the client machine.

As this article has shown, locating a remote object via a simple Java application in WebSphere Application Server V5 can be a challenge. While our example used an EJB as an example, you can use this approach to locate and retrieve other objects, such as datasources.



Back to top


Acknowledgements

Thanks to Troy Bishop, WebSphere Studio Application Developer Support Analyst of the Toronto IBM labs for his help in making this article possible.

Top of page




Back to top


Download

NameSizeDownload method
ThinClientSample.zip59 KBFTP|HTTP
Information about download methods


Resources



About the authors

Photo of Kulvir Bhogal

Kulvir Singh Bhogal works as an IBM consultant, devising and implementing Java-centric solutions at customer sites across the nation. You can reach Kulvir at kbhogal@us.ibm.com


Photo of Kwang Sik Kang

Kwang Sik Kang is a consultant for IBM Software Services for WebSphere. You can reach Kwang at kkwang@us.ibm.com.




Rate this page


Please take a moment to complete this form to help us better serve you.



 


 


Not
useful
Extremely
useful
 


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!



Back to top