Jboss deployment of HelloWorld EJB example
- Deployment under jboss is relatively simple:
- pack classes and deployment descriptors in jar
- create ear
- copy ear into deploy directory of jboss
Then to run the client:
- create client java file that calls util class of HelloWorld ejb example
- create client jar with proper jndi.properties
- call client
To create the jar/ear we need to add the following to our build.xml file:
<!-- =================================================================== -->
<!-- Deployment -->
<!-- =================================================================== -->
<target name="jar" depends="compile">
<mkdir dir="${samples.jar.dir}" />
<jar jarfile="${samples.jar.dir}/ejb-hello.jar">
<fileset dir="${samples.classes.dir}" includes="**" excludes="**/client/**" />
<metainf dir="${samples.meta-inf.dir}"
includes="**"
excludes="application.xml" />
</jar>
</target>
<target name="ear" depends="jar">
<mkdir dir="${samples.ear.dir}" />
<copy file="etc/application.xml" todir="${samples.meta-inf.dir}" />
<jar jarfile="${samples.ear.dir}/helloworld.ear">
<fileset dir="${samples.jar.dir}" includes="**/*" excludes="**/client*.jar" />
<metainf dir="${samples.meta-inf.dir}" includes="application.xml" />
</jar>
</target>
<target name="deploy-jboss" depends="ear">
<copy todir="${jboss4.deploy.dir}">
<fileset dir="${samples.ear.dir}" includes="helloworld.ear" />
</copy>
</target>
You also need to add some properties to your build-dist.properties file:
jboss4.deploy.dir=/usr/local/jboss-4.0.2/server/default/deploy
jboss4.client.j2ee.jar=/usr/local/jboss-4.0.2/client/jbossall-client.jar
The ear target also uses an application.xml file. This is a general file, and not propiety for jboss. It contains:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN' 'http://java.sun.com/j2ee/dtds/application_1_2.dtd'>
<application>
<display-name>HelloWorld</display-name>
<description>J2EE application to helloworld</description>
<module>
<ejb>ejb-hello.jar</ejb>
</module>
</application>
Ears could also have propiety deployment descriptors, for example when you want to deploy mbeans. However, my example is simple, and does not need it.
Now when you start jboss (jboss 4), and run ant deploy-jboss, you should see a successful deployment.
To make use of the newly deployed EJB, you will need to write a client file:
/**
* $Id$
*/
package com.jankester.tryout.client;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import com.jankester.tryout.ejb.session.HelloWorld;
import com.jankester.tryout.ejb.session.HelloWorldHome;
import com.jankester.tryout.ejb.session.HelloWorldUtil;
/**
* @author jkester
*
*/
public class HelloWorldEjb
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("calling client of HelloWorld");
try
{
HelloWorldHome helloHome = HelloWorldUtil.getHome();
System.out.println("Got HelloWorld home");
HelloWorld hw = helloHome.create();
String answer1 = hw.helloWorld();
String answer2 = hw.helloWorld("It's me again ..");
System.out.println("answer1=" + answer1 + " answer2=" + answer2);
}
catch (NamingException ne)
{
System.out.println("Failed getting home: " + ne.getMessage());
System.out.println("exception=" + ne);
}
catch (RemoteException re)
{
System.out.println("Failed calling getHome: " + re.getMessage());
}
catch (CreateException ce)
{
System.out.println("Failed creating HelloWorld proxy: " + ce.getMessage());
}
}
}
The client file makes use of the interfaces that have been generated by xdoclet. We will pack all classes into a client-hello.jar.
Further we need to add a jndi.properties file for jboss. It contains:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
This file will also be packed into the client-hello.jar. We need to add the following targets to our build.xml:
<target name="client-jar-jboss" depends="compile">
<mkdir dir="${samples.jar.dir}" />
<jar jarfile="${samples.jar.dir}/client-hello.jar">
<fileset dir="${samples.classes.dir}">
<include name="**/client/**" />
<include name="**/ejb/**" />
<exclude name="**/ejb/**/*Local*" />
<exclude name="**/ejb/**/*Bean*" />
<exclude name="**/ejb/**/*Session*" />
</fileset>
<fileset dir="etc/jboss" includes="*.properties" />
</jar>
</target>
<target name="helloworld-jboss" depends="client-jar-jboss">
<java classname="com.jankester.tryout.client.HelloWorldEjb"
failonerror="yes"
fork="yes"
spawn="no">
<classpath>
<pathelement location="${samples.jar.dir}/client-hello.jar" />
<pathelement location="${jboss4.client.j2ee.jar}" />
</classpath>
</java>
</target>
Now you can call the client file with “ant helloworld-jboss”. You should see output like this:
[java] calling client of HelloWorld
[java] Got HelloWorld home
[java] answer1=Hello world from HelloWorldBean! answer2=Hello It's me again ..!
Comments