Click here to Skip to main content
Click here to Skip to main content

How to create JBoss ejb3 session bean standalone client

, 16 Sep 2009
Rate this:
Please Sign up or sign in to vote.
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 :

In these few lines I describe the process to develop standalone client for ejb3 session bean hosted on JBoss -4.x application server.

Dependencies

  1. 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
  2. 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:

  1. 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.

  2. 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 {

Properties props = new Properties();

<p>props.load(new FileInputStream("c://jndi.properties")); </p>

<p>InitialContext ctx = new InitialContext(props); </p>

<p>SalaryCalc a = (SalaryCalc) ctx.lookup("SalaryCalc Bean/remote"); </p>

<p>a.doaction();</p>

<p>} catch (IOException ex) { </p>

<p>System.out.println("IOException : " + ex.getMessage()); </p>

<p>} catch (NamingException ex) { </p>

<p>System.out.println("NamingException : " + ex.getMessage()); </p>

}
<script type="text/javascript">var dzone_url = 'http://m-shaheen.blogspot.com/2009/07/how-to-create-jboss-ejb3-session-bean.html';</script><script type="text/javascript">var dzone_title = 'How to create JBoss ejb3 session bean standalone client';</script><script type="text/javascript">var dzone_blurb = 'How to create JBoss ejb3 session bean standalone client';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohamed M. Shaheen
Other Axiom Telecom
United Arab Emirates United Arab Emirates
Integration Specialist at Axiom Mobile Solutions a division of Axiom Telecom

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Sep 2009
Article Copyright 2009 by Mohamed M. Shaheen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid