2.2. Running Your First EJB Application

Several example programs are included with the JOnAS distribution. They are located in the $JONAS_ROOT/examples/src directory. The one we will use as your first EJB application is the Session Bean (SB) example that resides in $JONAS_ROOT/examples/src/sb.

The SB example involves a Java client that accesses a Stateful Session Bean and twice invokes the buy method of the bean transaction. The client communicates with JOnAS using RMI and the configuration files should be already set so that the RMI registry will be automatically started and embedded in the same JVM as JOnAS. The figure below gives an overview of this application.

Figure 2-1. EB example overview

2.2.1. Building the Examples

The simplest way to compile all examples is to start in the $JONAS_ROOT/examples/src directory and enter the following command line as user jonas:

ant -find build.xml install

This command compiles all examples in the $JONAS_ROOT/examples/src directory.

NoteNote
 

All examples assume that the current directory is $JONAS_ROOT/examples/src/ followed by the directory that contains the example. In other words, the commands entered for the SB example assume the current folder is $JONAS_ROOT/examples/src/sb, whereas the commands entered for the EB example assume the current folder is $JONAS_ROOT/examples/src/eb.

To login as user jonas, first login as root, then su to user jonas, specifying /bin/bash as the shell:

su -s /bin/bash jonas

2.2.2. Quick Start to the SB Example

NoteNote
 

This example is described in more detail in Section 5.3 Running the SB Example.

To run this example, you will have to first start the Red Hat Application Server server and then run the Java client. Finally, at the end of the execution you should stop the Red Hat Application Server server.

NoteNote
 

The following example assumes that the current directory is $JONAS_ROOT/examples/src/sb.

Here is how to proceed:

  1. As root, start the Red Hat Application Server server:

    /sbin/service jonas start
  2. Deploy the JAR file:

    • To deploy the JAR file from the command line:

      jonas admin -a sb.jar
    • To deploy the JAR file using the web interface:

      1. Log into the JOnAS Administrator.

        Figure 2-2. JOnAS Administration Login

        Log in with User Name jonas and Password jonas.

        Figure 2-3. JOnAS Administration Web Interface

      2. Click Deployments > EJB Modules (JAR).

        Figure 2-4. Deploy the sb.jar Example

      3. On the sb.jar in the Deployable list box on the left side, Click Deploy, then click Apply.

      4. A Confirm dialog appears; click Confirm.

        Figure 2-5. Confirm the sb.jar Deployment

      5. A Result dialog appears, and the newly deployed jar is in the list.

        Figure 2-6. Results of the sb.jar Deployment

      6. If you click Deployments > EJB Modules (JAR) again, sb.jar will appear in the right list box, which lists the deployed jar files.

  3. Start the Java Client:

    jclient sb.ClientOp 

    A successful run should output:

    Create a bean
    Start a first transaction
    First request on the new bean
    Second request on the bean
    Commit the transaction
    Start a second transaction
    Rollback the transaction
    Request outside any transaction
    ClientOp OK. Exiting.

    Congratulations! You have succeeded running your first EJB application with JOnAS!

  4. As root, stop the Red Hat Application Server server with the following command:

    /sbin/service jonas stop