A local JBoss EJB client

A local client can be a simple jsp:

   public void jspInit () {

   try {

     HelloBeanItf ejb = null;
     InitialContext ctx = new InitialContext();

     ejb = (HelloBeanItf)



   catch (Exception exc) { 


Remember that in EJB 3, the JNDI of the EJB by default is made up of : 
package name( ear/jar) / Bean Name / local or remote

If you fail to find your EJB in JNDI tree take a look at the JNDI tree from the JMX console and you'll solve the naming problem (There's an how to in this site here : JBoss howtos )

How can a remote client access a clustered EJB ?

When using remote client the first thing you think about is JNDI.

How does JNDI works in a cluster ? JBoss service maintains a cluster-wide context tree called HA-JNDI (High Availability JNDI). The cluster wide tree is always available as long as there is one node left in the cluster.

In the previous JSP , new InitialContext(), will be bound to a local-only, non-cluster-wide JNDI Context (this is actually basic JNDI). So, all EJB homes and such will not be bound to the cluster-wide JNDI Context, but rather, each home will be bound into the local JNDI.

When a remote client does a lookup through HA-JNDI, HA-JNDI will delegate to the local JNDI Context when it cannot find the object within the global cluster-wide Context.

How the remote client is aware of the HA-JNDI cluster ? You can pass a list of JNDI servers (i.e., the nodes in the HA-JNDI cluster) to the java.naming.provider.url JNDI setting in the jndi.properties file. Each server node is identified by its IP address and the JNDI port number. The server nodes are separated by commas



you can pass the list of JNDI servers to the InitialContext also like here:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

public class Client {

  public static void main(String[] args) throws Exception {

     Properties p = new Properties(); 
     p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces"); 
     p.put(Context.PROVIDER_URL, ",");

     InitialContext ctx = new InitialContext(p);
     HelloBeanItf ejb = 
       (HelloBeanItf) ctx.lookup("cluster/HelloBean/remote");
     for (int ii=0;ii<10;ii++) {

Before running the sample remeber to put the @Remote annotation in the Bean class, other wise you will try to contact an EJB local interface from remote, and this is not allowed!


Related articles available on mastertheboss.com

JBoss Clustering a Web Application

Please Note: This article cover JBoss AS 4/5/6 releases. If you w

JBoss monitoring HTTP Session replication

In this article we'll show how to monitor HTTPSession replication

How do I change multicast address of JBoss cluster ?

Since JBoss AS 4.0.3, the jboss.partition.udpGroup property can b

JBoss farming service

What is the farming service ? this article explains about it, als

WildFly HTTP Session in a cluster

This tutorial discusses in detail how to configure HTTP Session M

How do I know the Http Session replication size?

You need to add the <SIZE /> tag into your Cluster configur