Previous Topic: Java Agent Auto-Response GenerationNext Topic: Developing Against the Java Agent


Example: Auto-Response Generation for EJB Calls

The following Java method creates a JNDI context object, calls a server that is able to run EJBs, and invokes APIs.

public void doEJBCall() throws Exception
    {
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        props.put(Context.PROVIDER_URL, "jnp://localhost:1099");

        /* 1st (optionally) remote call */
        Context ctx = new InitialContext(props);

        /* 2nd remote call */
        EJB3UserControlBeanRemote remote = (EJB3UserControlBeanRemote) ctx.lookup("EJB3UserControlBean/remote");

        /* 3rd remote call */
        User u = remote.getUser("lisa_simpson");

        /* Local calls... */
        System.out.println(u.getEmail());
    }

Suppose that the server is not available. If you run this method, the code does not succeed because it cannot obtain a connection.

You can use the following respond directives to force the code to succeed. The first directive intercepts the creation of the InitialContext object. The second directive intercepts the lookup() method of the Context object. The third directive intercepts any method of the EJB3UserControlBeanRemote object.

<respond class="javax.naming.InitialContext" method="&lt;init&gt;" args="1099"/>

<respond class="javax.naming.Context" method="lookup" args="EJB3UserControlBean/remote"/>

<respond class="com.itko.examples.ejb3.EJB3UserControlBeanRemote"/>