Friday, March 21, 2014

How to develop EJB client applications for EJB deployed in different ejb containers?

i am going to develop a standalone java application to access the EJB has been deployed. first of all forget about the underlying EJB container impletation and focus only on developing the client.

we are going to develop a EJB client for the EJB deployed with http://chathurangat.blogspot.com/2014/03/create-simple-stateless-session-bean.html post.


1. Add the deployed EJB jar file to the classpath of the client being developed. this is because, the client program needs the access for the EJB business interface and methods.  i have added it as a maven dependency and it will be downloaded with my local maven repo.
   
       

            com.chathurangaonline.ejb.samples

            ejb-business-interface

            1.0

        

The JNDI and other configuration properties will differ between EJB containers. therefore we need to change those properties based on the EJB container that is we are going to access.

pom.xml

 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chathurangaonline.samples</groupId>
    <artifactId>ejb-client-example</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>ejb-client-example</name>
    <url>http://maven.apache.org</url>
    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.chathurangaonline.ejb.samples</groupId>
            <artifactId>ejb-business-interface</artifactId>
            <version>1.0</version>
        </dependency>

         <!--this dependency is required if the client is going to access the EJB deployed in the glassfish server-->
        <dependency>
            <groupId>org.glassfish.main.appclient.client</groupId>
            <artifactId>gf-client</artifactId>
            <version>3.1.2</version>
            <scope>system</scope>
            <systemPath>/home/chathuranga/Softwares/glassfish3/glassfish/lib/gf-client.jar</systemPath>
        </dependency>

    </dependencies>
</project>



EJB client for Jboss server

 
package com.chathurangaonline.ejb.samples;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class EJBClientJboss {

    public static void main(String[] args) {

        final String jndiName = "ejb-business-interface-1.0/SalaryCalculatorImpl!com.chathurangaonline.ejb.samples.SalaryCalculator";

        //configurations for accessing the EJB deployed in the glassfish server
        Properties jndiProps = new Properties();
        jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
        jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
        jndiProps.put(Context.SECURITY_PRINCIPAL, "appuser");
        jndiProps.put(Context.SECURITY_CREDENTIALS, "appuser123");
        jndiProps.put("jboss.naming.client.ejb.context", true);

        try{
            Context ctx = new InitialContext(jndiProps);

            Object obj = ctx.lookup(jndiName);
            System.out.println("lookup returned " + obj);

            SalaryCalculator salaryCalculator = (SalaryCalculator) obj;
            Double salaryIncrement =  salaryCalculator.getAnnualIncrementOnBasicSalary(55000.00);
            System.out.println(" salary increment is ["+salaryIncrement+"]");

        }
        catch (NamingException ex){
            System.out.println(" message ["+ex.getMessage()+"]");
            System.out.println(" ex ["+ex+"]");
        }
    }
}




EJB Client for glassfish server 

 
package com.chathurangaonline.ejb.samples;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class EJBClientGlassfish {

    public static void main(String[] args) {

        final String jndiName = "java:global/ejb-business-interface-1.0/SalaryCalculatorImpl!com.chathurangaonline.ejb.samples.SalaryCalculator";

        //configurations for accessing the EJB deployed in the glassfish server
        Properties jndiProps = new Properties();
        jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
        jndiProps.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        // glassfish default port value will be 3700
        jndiProps.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

        try{
            Context ctx = new InitialContext(jndiProps);

            Object obj = ctx.lookup(jndiName);
            System.out.println("lookup returned " + obj);

            SalaryCalculator salaryCalculator = (SalaryCalculator) obj;
            Double salaryIncrement =  salaryCalculator.getAnnualIncrementOnBasicSalary(55000.00);
            System.out.println(" salary increment is ["+salaryIncrement+"]");

        }
        catch (NamingException ex){
            System.out.println(" message ["+ex.getMessage()+"]");
            System.out.println(" ex ["+ex+"]");
        }
    }
}



You will notice that we have change only the properties based on the EJB container/server.


Thanks
Chathuranga Tennakoon


No comments:

Post a Comment