Thursday, November 8, 2007

Stateless EJB and client using Glassfish

Thursday, November 08, 2007 Posted by Andre Broers 2 comments
Remote interface to the Bean:
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat Hello.java
[sourcecode language='java']
package com.bekijkhet;
public interface Hello {
public String sayHello();
public String sayHelloRemote();
}[/sourcecode]
Local interface to the Bean:
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloLocal.java
[sourcecode language='java']
package com.bekijkhet;
public interface HelloLocal {
public String sayHello();
public String sayHelloLocal();
}[/sourcecode]
Bean himself :
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloBean.java
[sourcecode language='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!!!!";
}
}[/sourcecode]
Build.xml script:
broersa@debian1:~/work/HelloApp/HelloEJB$ cat build.xml
[sourcecode language='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>[/sourcecode]
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
[sourcecode language='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();
}
}
}[/sourcecode]
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.

2 comments: