Implement EJB clients in Java

Application client projects

Application client projects contain the resources needed for application client modules. An application client module is used to contain a full-function client Java application (non Web-based) that connects to and uses the J2EE resources defined in your server. When you place the client code in an application client module instead of a simple JAR file, the application client benefits from the server's resources (it does not need to re-specify the class path to J2EE and server JAR files) as well as from easier JNDI lookup (the client container fills in the initial context and other parameters). The application client project allows you to work as if you are creating a standalone Java application in a Java project.

An application client project enables you to do the following things:

Like Java projects, application client projects contain the resources needed for application clients, including Java class files. When you create a new application client project, the environment is set up for Java development. A Java builder is associated with the project so the Java source can be incrementally compiled as it is updated. The application client project contains information about the type hierarchy and Java elements. This information is kept current as changes are made, and the Java builder will incrementally compile the resources within these projects as the resources are updated.

In the workbench, application client projects are always referenced by enterprise application (EAR) projects. When you create an application client project, you specify the enterprise application project to which the application client project belongs. A module element is automatically added to the application.xml deployment descriptor for the EAR project.

An application client project is deployed as a JAR file. This application client JAR file contains the necessary resources for the application, including Java class files, and deployment descriptor information and any meta-data extensions and bindings files.

Application client projects are typically run on networked client systems connected to J2EE (EJB) servers. The point of entry for the application client is a Java main-class, which is simply a Java class that contains a static main method. The class is declared in the manifest file of the client module.

A J2EE application client container provides access to the J2EE service (JNDI naming services, deployment services, transaction services, and security services) and communications APIs (internet protocols, Remote Method Invocation protocols, Object Management Group protocols, Messaging protocols, and data formats).

By default, application client projects contain one folder named appClientModule, which contains both Java source code and compiled .class files, along with all the meta-data files in the META-INF subfolder.

Creating an application client project

You can use a wizard to create a new application client project and add it to a new or existing enterprise application project.

Application client projects contain the resources needed for application client modules. Application client projects contain programs that run on networked client systems. An application client project is deployed as a JAR file.

To create a J2EE application client project:

  1. In the J2EE perspective, select File > New > Application Client Project.

  2. In the Name field, type a name for the application client project - Chapter2Client.

  3. To change the default Project location, click the Browse button to select a new location. If you specify a non-default project location that is already being used by another project, the project creation will fail.

  4. Click Show Advanced.

  5. In the J2EE version drop-down list, select the J2EE version to which you want your application client project to adhere (1.4).

  6. In the Target server drop-down list, select the application server that you want to target for your development. This selection affects the run time settings by modifying the class path entries for the project.

  7. Specify whether you want to add the new module to an enterprise application (EAR) project.

  8. In the EAR project combination box, type a new project name or select an existing enterprise application project from the drop-down list (Chapter2EAR). Or, click the New button to launch the New Enterprise Application Project wizard.

    Note: If you type a new EAR project name, the EAR project will be created in the default location with the lowest compatible J2EE version based on the version of the project being created. If you want to specify a different version or a different location for the enterprise application, you must use the New Enterprise Application Project wizard.

  9. Select Create a default Main class if you want the wizard to generate a Java class to be the application entry point.

    Application Client Project

  10. If you are adding the new module to an existing enterprise application, click Next to specify module and JAR file dependencies on the next page of the wizard. This updates the manifest class-path and Java project build path with the appropriate JAR files. If you are creating a new enterprise application project, or if you have no dependencies to specify, click Finish.

    Application Client modules, EJB modules, and Web modules can all have dependencies on EJB modules or utility JAR files. Modules cannot depend on WAR or Application Client JAR files.


  11. Click Finish.

Inserting Java code to call a session bean method

Use the Snippets view to insert Java code that calls a particular method on a session bean.

The inserted Java code calls the create method on the session bean, then invokes the method that you specify in the wizard.

Example snippet insertion for calling an session bean service method.

Before snippet insertion:

public class Main {
    public static void main(String[] args) {
        Main main = new Main();

    public String getGreeting(String name) {
        return null;

After snippet insertion, with the method selected on the session bean:

import java.rmi.RemoteException;

import by.iba.rad257.ejb.SayHello;
import by.iba.rad257.ejb.SayHelloHome;


public class Main {
    private final static String STATIC_SayHelloHome_REF_NAME = "ejb/SayHello";

    private final static Class STATIC_SayHelloHome_CLASS = SayHelloHome.class;

    public static void main(String[] args) {
        Main main = new Main();

    public String getGreeting(String name) {
        String aString = "";
        SayHello aSayHello = createSayHello();
        try {
            aString = aSayHello.greet(name);
        } catch (RemoteException ex) {
        return aString;

    protected SayHello createSayHello() {
        SayHelloHome aSayHelloHome = (SayHelloHome) ServiceLocatorManager.getRemoteHome(STATIC_SayHelloHome_REF_NAME, STATIC_SayHelloHome_CLASS);
        try {
            if (aSayHelloHome != null)
                return aSayHelloHome.create();
        } catch (javax.ejb.CreateException ce) {
        } catch (RemoteException re) {
        return null;

To insert Java code for calling a session bean service method:

  1. In the J2EE perspective, open in the Java editor the Java file where you want to add the code snippet.

  2. Place your cursor in the point of the Java file where you want to insert the code.

  3. In the Snippets view, expand the EJB drawer and double-click Call a Session bean service method. The Insert Session Bean Service wizard opens.

    Insert Session Bean Service

  4. Select the EJB reference for the session bean that you want to call. If you have not defined an EJB reference for the session bean, you MUST add one before you can complete this wizard. Click New EJB Reference to open the Add EJB Reference wizard.

  5. Click Next.

  6. If the client and enterprise bean are not in the same application server container, you must enter the Provider URL and Name Service Type to locate the referenced enterprise bean. Otherwise, you can select Use default context properties for doing a lookup on this reference.

  7. Click Next.

  8. Select the method in the session bean that you want to call.

    Method name

    Parameter values

  9. Click Finish.

NOTE: A serviceLocatorMgr.jar file is added as a utility JAR file to each enterprise application that the Java class you edited belongs to. This serviceLocator.jar file includes a ServiceLocatorManager class that is used within the inserted snippets of Java code. This class optimizes the lookups of the home interfaces and InitialContexts, and ensures that they are only looked up once for the entire application. Because the utility JAR file is added, a Java JAR dependency for the serviceLocator.jar file is added for the module or Java utility project that the Java file belongs to.

The ServiceLocatorManager class has a static method called setErrorHandler(ServiceLocatorErrorHandler handler) that you can use to specify a specific error handler for error conditions that occur when looking up the home interface. The default handler simply calls printStackTrace() on the exception that is handled.

BOOT.BY - Tech Industry News         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     Free SCWCD 1.4 Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free SCBCD 5.0 Study Guide