Note that JBossAS-5.0.0 beta2 does not work with JDK 6 (see my previous post). So you will need to set environment variable JAVA_HOME to a JDK 5 location (e.g., set JAVA_HOME=C:\jdk5) for JBoss server. This also requires us to compile EJB classes with JDK 5 for the class file to be compatible with server JVM. However, it is perfectly OK to use JDK 6 for the client.
1. project directory structure:
C:\simple-ejb3> tree /A /F2. create java src files under
+---classes
| \---foo
\---src
\---foo
Client.java
FooBean.java
FooRemote.java
src\foo
:FooRemote.java:
FooBean.java:
package foo;
import javax.ejb.*;
@Remote
public interface FooRemote {
public String echo(String s);
}
Client.java
package foo;
import javax.ejb.*;
@Stateless
public class FooBean implements FooRemote {
public String echo(String s) {
return s;
}
}
3. Compile java src. An environment variable
package foo;
import javax.ejb.*;
import javax.naming.*;
public class Client {
public static void main(String[] args) throws Exception {
//JBoss' default remote jndi: <ejb-name>/remote
final String jndiName = "FooBean/remote";
Context ic = new InitialContext();
System.out.println("about to look up jndi name " + jndiName);
Object obj = ic.lookup(jndiName);
System.out.println("lookup returned " + obj);
FooRemote foo = (FooRemote) obj;
String s = foo.echo("Hello Foo on JBoss!");
System.out.println(foo + " echo returned " + s);
}
}
JBOSS_HOME
is set for convenience but it's not required.C:\simple-ejb3\classes> set JBOSS_HOME=C:\jboss4. start JBoss default config:
C:\simple-ejb3\classes> javac -d . -classpath %JBOSS_HOME%\client\jbossall-client.jar;. ..\src\foo\*.java
set JAVA_HOME=C:\jdk55. package and autodeploy ejb-jar. We can combine the 2 steps in 1 by using %JBOSS_HOME%\server\default\deploy as the destdir:
C:\simple-ejb3\classes> %JBOSS_HOME%\bin\run.bat
C:\simple-ejb3\classes>6. Prepare jndi.properties and log4j.properties somewhere in client classpath, e.g., simple-ejb3\classes directory:
jar cvf %JBOSS_HOME%\server\default\deploy\foo-ejb.jar
foo\FooBean.class foo\FooRemote.class
# jndi.properties:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
7. run the standalone java client. Note that I use JDK 6 on the client to use the wildcard (*) in classpath. JBoss client needs a sleuth of jars scattered in several JBoss server directories. With * in classpath I don't need to figure out which jars to include.
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n
C:\simple-ejb3\classes> java -cp %JBOSS_HOME%\lib\*;%JBOSS_HOME%\client\*;%JBOSS_HOME%\server\default\lib\*;. foo.ClientIf you have to run client with JDK 5, this is the detailed classpath:
about to look up jndi name FooBean/remote
lookup returned jboss.j2ee:jar=foo-ejb.jar,name=FooBean,service=EJB3
jboss.j2ee:jar=foo-ejb.jar,name=FooBean,service=EJB3 echo returned Hello Foo on JBoss!
%JBOSS_HOME%\lib\jboss-aop-jdk50.jar;This is the classpath necessary to run this simple client. Other more complex client may need additional jars. And who knows what will change in the next JBoss release? So it's always a good idea to use the wildcard classpath with JDK 6.
%JBOSS_HOME%\client\jbossall-client.jar;
%JBOSS_HOME%\server\default\lib\jbosssx.jar;.
8. undeploy the ejb module:
del %JBOSS_HOME%\server\default\deploy\foo-ejb.jar