Sun Java System Application Server 

Samples Index

The EJB 3.1 Embeddable API Sample Application - Test Code

This is an example of the Java client that uses an embeddable EJB container to test EJBs in an environment that supports for the same basic services that would exist within a Java EE runtime: injection, access to a component environment, container-managed transactions, Java Persistence API, etc

Used EJB 3.1 Embeddable API

The following bootstrapping API that are defined on the class javax.ejb.embeddable.EJBContainer are used to create the embeddable EJB container and to lookup the Stateless Session bean in the JNDI:

EJB module called ejb-embedded.jar is located in the JVM classpath.


Standalone Java Client

public class TestClient {

    private static String appName;

    public static void main(String[] s) throws Exception {
        TestClient t = new TestClient();
        t.test();

    }

    private void test() throws Exception {

        EJBContainer c = null;
        try {
            c = EJBContainer.createEJBContainer();
            Context ic = c.getContext();
            System.out.println("Looking up EJB...");
            SimpleEjb ejb = (SimpleEjb) ic.lookup("java:global/ejb-embedded/SimpleEjb");
            System.out.println("Invoking EJB...");
            System.out.println("Inserting entities...");
            ejb.insert(5);
            System.out.println("JPA count returned: " + ejb.verify());
            System.out.println("Done calling EJB");
        } finally {
            if (c != null) {
                System.out.println("Closing container ...");
                c.close();
                System.out.println("Done Closing container");
            }
        }

        System.out.println("..........FINISHED Embedded test");
    }
}

Building, Deploying, and Running the Java Client

Following are the instructions for building, deploying, and running the Java Client.

  1. Setup your build environment and Configure the application server with which the build system has to work by following the common build instructions.
  2. test_dir is the sample application test directory: samples_install_dir/javaee6/ejb/ejb-embedded/ejb-embedded-client
  3. Change directory to test_dir.
  4. Make sure the JavaDB database is started. If not, refer to common build instructions.
  5. Build, and Run the test using the target  runtest
  6. app_dir> ant runtest

  7. The runtest target depends on ejb-embedded.jar so make sure that the ejb code is built prior to running this target.
  8. In build.xml, the property 'classpath' is redefined to have only the ejb and the client classes on which this test code depends, and the special glassfish-embedded-static-shell.jar that is located in the application server's lib/embedded directory. The property 'runjavaclient.class.name' points to the Java Client class name which is invoked as a result of running ant runtest.
  9. Use the target clean to remove the temporary directories like build and dist.

    app_dir> ant clean

Troubleshooting

If you have problems when running the application, refer to troubleshooting document.

 

Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.