Hi,

In this article we can see how to develop and test a simple Stateless Session Bean in JBoss and what all JARs are needed in the CLASSPATH of a standalone client to invoke an EJB. In this demonstration we will not use any IDE to create the EJBs we will just notepad or gedit of editors to develop the testcase on our own, this is just to see how to manually set the classpath and path and many other things.

Step1). Create a directory somewhere in your filesystem like : “/home/testuser/Demos/MyEJB3.jar” Here “MyEJB3.jar” is a Directory & Not a file.

Step2). Now create a remote Interface for your Stateless Bean inside “/home/testuser/Demos/MyEJB3.jar” with name “TestSLSB_RemoteIntf.java” like following:

package ejb3;
import javax.ejb.Remote;
@Remote
public interface TestSLSB_RemoteIntf {
  public String sayHello(String name);
}

Step3). Now we need to write the Stateless Bean class, which implements the above interface as following “TestSLSBean.java” :

package ejb3;
import javax.ejb.*;
@Stateless
public class TestSLSBean implements  TestSLSB_RemoteIntf
{
  public TestSLSBean() {
      System.out.println("nt TestSLSBean-->  TestSLSBean() invoked");
  }

   public String sayHello(String name){
      System.out.println("nt TestSLSBean --->  public String sayHello(String name)  invoked");
      return "Hello Mr. "+name;
   }
}

Step4). Now we need to set the CLASSPATH to compile the above two classes. In order to set the classpath please open a shell prompt and then run the following commands:

 export PATH=/home/testuser/MyJdks/jdk1.6.0_21/bin:$PATH:
 export CLASSPATH=`/home/testuser/jboss-AS6/jboss-as/bin/classpath.sh -s`

Step5). Now compile the EJBs like following:

javac    -d   .   TestSLSBean.java
javac    -d    .   TestSLSB_RemoteIntf.java

Step6). Now Copy the “MyEJB3.jar” and then paste it inside your “$PROFILE/deploy” directory so that it will be deployed on the JBoss instance. As soon as you will deploy it on JBoss you will find following kind of STDOUT entry in the server’s console:

18:06:47,564 INFO  [JBossASKernel] Created KernelDeployment for: MyEJB3.jar
18:06:47,565 INFO  [JBossASKernel] installing bean: jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3
18:06:47,565 INFO  [JBossASKernel]   with dependencies:
18:06:47,565 INFO  [JBossASKernel]   and demands:
18:06:47,565 INFO  [JBossASKernel] 	jboss.ejb:service=EJBTimerService
18:06:47,565 INFO  [JBossASKernel]   and supplies:
18:06:47,565 INFO  [JBossASKernel] 	Class:ejb3.TestSLSB_RemoteIntf
18:06:47,565 INFO  [JBossASKernel] 	jndi:TestSLSBean/remote
18:06:47,565 INFO  [JBossASKernel] 	jndi:TestSLSBean/remote-ejb3.TestSLSB_RemoteIntf
18:06:47,565 INFO  [JBossASKernel] Added bean(jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3) to KernelDeployment of: MyEJB3.jar
18:06:47,626 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3
18:06:47,632 INFO  [EJBContainer] STARTED EJB: ejb3.TestSLSBean ejbName: TestSLSBean
18:06:47,692 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

	TestSLSBean/remote - EJB3.x Default Remote Business Interface
	TestSLSBean/remote-ejb3.TestSLSB_RemoteIntf - EJB3.x Remote Business Interface

Step7). Now we need to write the EJB Client to invoke the Stateless Session Bean which is deployed on the Server. So we need to write “Client.java” program as following inside “/home/testuser/Demos” directory.

import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import javax.rmi.PortableRemoteObject;
public class Client
{
   public static void main(String[] args) throws Exception
    {
             //JBoss' default remote jndi: <ejb-name>/remote
             final String jndiName = "TestSLSBean/remote";
             Hashtable ht=new Hashtable();
             ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
             ht.put(Context.PROVIDER_URL,"localhost:1099");

             Context ic = new InitialContext(ht);
             System.out.println("ntabout to look up jndi name " + jndiName);
             Object obj = ic.lookup(jndiName);
             System.out.println("ntlookup returned " + obj);

             ejb3.TestSLSB_RemoteIntf remote=(ejb3.TestSLSB_RemoteIntf)PortableRemoteObject.narrow(obj,ejb3.TestSLSB_RemoteIntf.class);
             System.out.println("ntCalling Remote Method:sayHello(String): "+remote.sayHello("JBossUser"));
    }
}

Step8). Now again we can open a separate Shell Prompt to set the Client CLASSPATH like following:

 export PATH=/home/testuser/MyJdks/jdk1.6.0_21/bin:$PATH:
 export CLASSPATH=`/home/testuser/jboss-AS6/jboss-as/bin/classpath.sh -c`

NOTE: “$JBOSS_HOME/bin/classpath.sh -c” here “-c” represents Clients Classpath same we can use “-s” flag with this script to display the required Server Classpath Jars/Classpath setting.

Step9). Now compile and then run the client in the same shell prompt as following:

 java Client

	about to look up jndi name TestSLSBean/remote
log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.

	lookup returned Proxy to jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3 implementing [interface ejb3.TestSLSB_RemoteIntf]

	Calling Remote Method:sayHello(String): Hello Mr. JBossUser

.
.
Thanks
Middleware Magic

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.