Wednesday, August 13, 2014

EJB3.1 - EJB on Websphere & Client on Remote PC with J2SE

Purpose:  I was looking for a simple hello world ejb (ejb3.1) with remote interface,  that i can deploy on websphere application server and use it from the remote j2se client.
I was not able to find  this kind of sample readily available.

So here is the HelloWorld EJB(3.1) and its client.

1) Create HelloWorld EJB

1.1) Using RAD (/eclipse)  Create a new JavaEE > Enterprise Application Project.

Project Name = TestEjb, Under java ee module dependency, check the check box for ejb module.
The project Structure should look like below.






Code for TestEJBRemote.java
---------------------------------------------------------------------------------------------------------------------
package ejb31.test;
import javax.ejb.Remote;

@Remote
public interface TestEJBRemote {   
    public String sayHello(String value);
}

--------------------------------------------------------------------------------------------------------------------- 

Code for TestEJBRemote.java
 --------------------------------------------------------------------------------------------------------------------- 
package ejb31.test;

import javax.ejb.Stateless;

@Stateless
public class TestEJB implements TestEJBRemote {

    public TestEJB() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String sayHello(String yourValue) {
        System.out.println("got your " + yourValue);
        return "hello " + System.currentTimeMillis() + " - " + yourValue;
    }

}

---------------------------------------------------------------------------------------------------------------------

Code for ejb-jar.xml
 --------------------------------------------------------------------------------------------------------------------- 
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
    <module-name>TestEjbEJB</module-name>
    <display-name>TestEjbEJB</display-name>
   
</ejb-jar>

---------------------------------------------------------------------------------------------------------------------

Code for ibm-ejb-jar-bnd.xml
 ---------------------------------------------------------------------------------------------------------------------  <?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd
        xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_2.xsd"
        version="1.2">
      
        <session name="TestEJB"></session>

</ejb-jar-bnd>

---------------------------------------------------------------------------------------------------------------------

Code for ibm-ejb-jar-ext.xml
 ---------------------------------------------------------------------------------------------------------------------
 <?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-ext
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-ext_1_1.xsd"
    version="1.1">

</ejb-jar-ext>

  ---------------------------------------------------------------------------------------------------------------------


Start the Application server (WAS  8.5 / 8) and deploy to the server.


2) Create the EJB Client.

2.1) Right click on TestEjbEJB and click on Export. Export it as EJB JAR File.






The jar file get exported at location c:/temp.

2.2) Now run the IBM createEJBStubs tool located under  AppServer/bin directory.
 

 (run from c:/temp)
C:\IBM\WebSphere85\AppServer_1\bin\createEJBStubs.bat TestEjbEJB.jar -newfile TestEjbEJBClient.jar -quiet

You can check the TestEjbEJBClient.jar - there would be a stub produced by the name of _TestEJBRemote_Stub.class


3) Create the Java Client Project.
Dependency needed





3.1) Create new Java Project TestEJBClient, as below.





Code for TestMain.java
 ---------------------------------------------------------------------------------------------------------------------
package test;

import java.util.Hashtable;

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

import ejb31.test.TestEJBRemote;
  
public class TestMain {
    Context ctx = null;
    public static void main(String args[]) {
        TestMain t = new TestMain();
        t.test();
    }
  
    public void test() {
       
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
        env.put(Context.PROVIDER_URL,"iiop://localhost:2809");
                      
        try {
            ctx = new InitialContext(env);
           

            String jndiURL = "ejb31.test.TestEJBRemote";
          
            TestEJBRemote remote = (TestEJBRemote) ctx.lookup(jndiURL);
           
          
            System.out.println(remote.sayHello("did you got me"));
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}


  ---------------------------------------------------------------------------------------------------------------------



No comments:

Post a Comment