In these few lines I describe the process to develop standalone client for ejb3 session bean hosted on JBoss -4.x application server.
Dependencies
- JBoss Client libraries : You have to import some JBoss libraries $JBOSS_HOME/client directory to the stand-alone application, these libraries are :
- jbossall-client.jar
- jboss-ejb3-client.jar
- EJB module
Add the reference of the ejb module to the application
Getting the ejb instance
To get the ejb instance in the client module there are two methods:
- EJB injection
JBoss-4.x does not support EJB injection in servlets or application clients which means the annotation @EJB will not work. It’s supported on JBoss-5 version.
- JNDI lookup
For any type of ejb client (either stand-alone or web servlet) if it’s not deployed in the same ear archive of the ejb ,you must setting up the environment required by a JNDI application .
JNDI settings are to initialize InitialContext object, for this we have the below options :
1. using Application Resource Files
- Create file named and jndi.properties save it to application class path.
- Add the following lines :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=JBOSS_IP:1099
Where JBOSS_IP is the ejb hosting JBoss server IP address and 1099 is the JBoss server JNDI port.
2. using System Properties
- Navigate to Project Properties.
- From categories select Run
- In VM Options box type the below lines :
-Dava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
-Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
-Djava.naming.provider.url=JBOSS_IP:1099
How it works
1. In the IDE(here i am using netbeans) create new project of type java application .
2. Add the dependences mentioned above to the project libraries.
3. Configure the JNDI setup using any of the two options mentioned above , here I am using jndi.properties which is saved to c: drive root
4. Use the Properties class load the JNDI properties.
Properties props = new Properties();
props.load(new FileInputStream("c://jndi.properties"));
5. Initialize the InitialContext instance passing the Properties class instance to the constructor.
InitialContext ctx = new InitialContext (props);
6. Use the InitialContext instance lookup method to looks for the remote object inside the EJB module and returns its instance.
for this we pass the JNDI name of the remote or local interface of the session bean to lookup method , for JBoss the JNDI name pattern takes the following format
EAR name / The EJB Implementation Component / remove or local
If the ejb deployed into JAR archive rather than EAR achieve, then the JNDI name pattern is:
The EJB Implementation Component / remove or local
SalaryCalc a;
a = (SalaryCalc ) ctx.lookup("SalaryCalc Bean/remote");
4. the code will be as follows:
<p><code>try {<br /><br />Properties props = new Properties();<br /><br /><p>props.load(new FileInputStream("c://jndi.properties")); </p><br /><br /><p>InitialContext ctx = new InitialContext(props); </p><br /><br /><p>SalaryCalc a = (SalaryCalc) ctx.lookup("SalaryCalc Bean/remote"); </p><br /><br /><p>a.doaction();</p><br /><br /><p>} catch (IOException ex) { </p><br /><br /><p>System.out.println("IOException : " + ex.getMessage()); </p><br /><br /><p>} catch (NamingException ex) { </p><br /><br /><p>System.out.println("NamingException : " + ex.getMessage()); </p><br /><br />}


Integration Specialist at Axiom Mobile Solutions a division of Axiom Telecom