Andre Broers’ personal blog

November 9, 2007

Stateless EJB and client using Glassfish

Filed under: ejb, glassfish, j2ee, java — broersa @ 7:36 am

Remote interface to the Bean:
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat Hello.java

 package com.bekijkhet;
 public interface Hello {
   public String sayHello();
   public String sayHelloRemote();
 }

Local interface to the Bean:
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloLocal.java

 package com.bekijkhet;
 public interface HelloLocal {
   public String sayHello();
   public String sayHelloLocal();
 }

Bean himself :
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloBean.java

 package com.bekijkhet;
 import javax.ejb.Stateless;
 import javax.ejb.Remote;
 import javax.ejb.Local;
 @Stateless
 @Remote(Hello.class)
 @Local(HelloLocal.class)
 public class HelloBean implements Hello,HelloLocal {
   public String sayHello() {
     return "Hello World!!!!";
   }
   public String sayHelloLocal() {
     return "Hello Local World!!!!";
   }
   public String sayHelloRemote() {
     return "Hello Remote World!!!!";
   }
 }

Build.xml script:
broersa@debian1:~/work/HelloApp/HelloEJB$ cat build.xml

<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/HelloBean-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Do a asant dist to create the EJB jar file and use asadmin deploy <jarfile> to deploy the EJB to Glassfish.
As an example I wil create a simple stand alone client that will call the Remote interface of the EJB. Local isn’t possible because I create a stand alone client which does not reside inside the container.Client code:
broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloClient.java

package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.Hello;

public class HelloClient {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      // Piece of code to list all remote JNDI resources
      //NamingEnumeration e = ctx.list("");
      //while (e.hasMore()) {
      //  System.out.println(e.next());
      //}

      Hello h = (Hello)ctx.lookup("com.bekijkhet.Hello");
      System.out.println(h.sayHello());
      System.out.println(h.sayHelloRemote());
      // Will fail because we call the Remote Business interface
      // System.out.println(h.sayHelloLocal());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Use the next steps to compile and run the code:broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ javac -cp $GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar:. -d . HelloClient.java

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ java -cp $GLASSFISH_HOME/lib/javaee.jar:$GLASSFISH_HOME/lib/appserv-rt.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-20071108.jar:. com.bekijkhet.helloclient.HelloClient

Hello World!!!!
Hello Remote World!!!!

This is a simple Hello world EJB sample. Have fun.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.