Friday, August 1, 2014

EJB 3.1 invocations by remote clients on JBOSS 7

How to write EJB 3.1 clients on JBOSS 7.1

EJB invocation from a remote client using JNDI

  1. Deploy the EJBs on the server
  2. Create a Initial Context instance by specifying the following properties
    1. name = java.naming.factory.url.pkgs
    2. value = org.jboss.ejb.client.naming
        See the below code:

public class JndiLookUp {
    private static Context initialContext;
   
    private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";

    public static Context getInitialContext() throws NamingException {
        if (initialContext == null) {
            Properties properties = new Properties();
            properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
           // properties.put("jboss.naming.client.ejb.context", true);

            initialContext = new InitialContext(properties);
        }
        return initialContext;
    }

}

3. Create a new file with name 'jboss-ejb-client.properties' and add it the ejb module class path. It is at same level as META-INF directory of ejbmodule
4. Add the following properties to the jboss-ejb-client.properties file

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=nitesh
remote.connection.default.password=nk

username and password properties are required from JBOSS 7.1 hence. These names can be create be add-user.bat utility at JBOSS_HOME/bin.

5. Add jboss-client.jar to the project class path. This jar can be found at:
JBOSS_HOME/bin/client/jboss-client-7.1.0.Final.jar

With the above steps we are ready to write the client code.

public class EjbClient {
           RemoteCounter counter =  (RemoteCounter) doLookup( SingletonCounterBean.class, RemoteCounter.class );
        
        System.out.println("count"+ counter.getTheValue() );
         System.out.println( counter.increment() );        
    }
   
    private static Object doLookup(  Class<?> beanClz, Class<?> interfaceClz ) {
        Context context = null;
        Object bean = null;
        try {
            // 1. Obtaining Context
            context = JndiLookUp.getInitialContext();
            // 2. Generate JNDI Lookup name
            String lookupName = getLookupName(beanClz, interfaceClz );
            // 3. Lookup and cast
            bean = context.lookup(lookupName);

        } catch (NamingException e) {
            e.printStackTrace();
        }
        return bean;
    }
   
    private static String getLookupName( Class<?> beanClz, Class<?> interfaceClz) {
        /*The app name is the EAR name of the deployed EJB without .ear
        suffix. Since we haven't deployed the application as a .ear, the app
        name for us will be an empty string */
        String appName = "ejbprojear";

        /* The module name is the JAR name of the deployed EJB without the
        .jar suffix.*/
        String moduleName = "ejbproj1";

        /* AS7 allows each deployment to have an (optional) distinct name.
        This can be an empty string if distinct name is not specified.*/
        String distinctName = "";

        // The EJB bean implementation class name
        String beanName = beanClz.getSimpleName();
       
        System.out.println("Simple name =" + beanName );

        // Fully qualified remote interface name
        final String interfaceName = interfaceClz.getName();

        // Create a look up string name
        String name = "ejb:" + appName + "/" + moduleName + "/" +
                distinctName    + "/" + beanName + "!" + interfaceName;
        return name;
    }
}

Some useful links:

  1. EJB invocation from a remote client via JNDI
  2. Remote naming  

EJB invocation from a Remote server instance

Below tutorial explains how to invoke on EJBs deployed on one AS7 instance from another AS7 instance.

Some useful links.

  1. EJB invocation from a remote server instance