Exercise 4.2: JNDI Binding with Annotations




Exercise 4.2: JNDI Binding with Annotations

This exercise is the same code as in Exercise 4.1, except it uses a JBoss-specific annotation to override the remote JNDI binding that is used by clients to look up the TravelAgent EJB.

Start Up JBoss

If you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in Workbook 1.

Initialize the Database

If you have problems running this example, shut down JBoss and run the clean.db Ant task.

Build and Deploy the Example Programs

Perform the following steps:

  1. Open a command prompt or shell terminal and change to the ex04_2 directory created by the extraction process.

  2. Set the JAVA_HOME and JBOSS_HOME environment variables to point to where your JDK and JBoss 4.0 are installed. Examples:


    Windows:

    C:\workbook\ex04_2> set JAVA_HOME=C:\jdk1.5.0
    C:\workbook\ex04_2> set JBOSS_HOME=C:\jboss-4.0.x
    


    Unix:

    $ export JAVA_HOME=/usr/local/jdk1.5.0
    $ export JBOSS_HOME=/usr/local/jboss-4.0
    

  3. Add ant to your execution path. Ant is the build utility.


    Windows:

    C:\workbook\ex04_2> set PATH=..\ant\bin;%PATH%
    


    Unix:

    $ export PATH=../ant/bin:$PATH
    

  4. Perform the build by typing ant.

As in the last exercise, titan.jar is rebuilt, copied to the JBoss deploy directory, and redeployed by the application server.

Using @RemoteBinding

If you do not like the default remote JNDI binding for the TravelAgent EJB discussed in Exercise 4.1, you can override it with the @org.jboss.annotation.ejb.RemoteBinding annotation:

package org.jboss.annotation.ejb.RemoteBinding

@Target({TYPE, ANNOTATION_TYPE}) @Retention(RUNTIME)
public @interface RemoteBinding
{
   String jndiBinding( ) default "";
   String interceptorStack( ) default "";
   String clientBindUrl( ) default "socket://0.0.0.0:3873";
   Class factory( ) default org.jboss.ejb3.remoting.RemoteProxyFactory.class;
}

The jndiBinding( ) attribute is used to specify the JNDI binding override. To learn about the other attributes, check out the JBoss EJB 3.0 documentation.

TravelAgentBean.java
package com.titan.travelagent;

import org.jboss.annotation.ejb.RemoteBinding;

@Stateless
@RemoteBinding 
(jndiBinding="TravelAgentRemote")
public class TravelAgentBean implements TravelAgentRemote
{
...
}

With the @RemoteBinding annotation in place, the client program can be modified to use it.

Client.java
package com.titan.clients;

import com.titan.travelagent.TravelAgentRemote;
import com.titan.domain.Cabin;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

public class Client
{
    public static void main(String [] args)
    {
        try
        {
            Context jndiContext = getInitialContext( );
            Object ref = jndiContext.lookup("TravelAgentRemote");
            TravelAgentRemote dao = (TravelAgentRemote)
                PortableRemoteObject.narrow(ref,TravelAgentRemote.class);
...
    }

    public static Context getInitialContext( )
        throws javax.naming.NamingException
    {
        return new javax.naming.InitialContext( );
    }
}

Run the client

Run the Client application by invoking ant run.client at the command prompt. Remember to set your JBOSS_HOME and PATH environment variables.