How to Use Remote EJBs in JBoss Pt. 2

It's easy to setup a Remote EJB connection in JBoss. In this section, we learn how to do that.

How to Use Remote EJBs in JBoss Pt. 2

Remote EJBs make it easy to access business components when they run on a different server. In Part 1, we saw how to create and deploy a Remote EJB.

And now, we'll access that Remote EJB from a different server.

How Do We Use Remote EJBs?

Our final architecture

  • 1 server running a web app that can access our Remote EJB (Web Tier)
  • 1 server running a Remote EJB (Business Tier)

What You'll Need

  • A basic understanding of Maven (read this),
  • A basic understanding of EJBs & Servlets (read this)
  • Completion of Part 1 (Business Tier). Steps here

Part 2: The Web Tier

Create Web Application

We'll use a previous example as our starting point. It contains a web servlet that connects to a Local EJB. And with a few updates, it will connect to our Remote EJB.

Step 1: Download simple-remoteejb-project

  • Or, follow the steps here

Step 2: Import the project into JBoss Developer Studio

Step 3: Add a Maven Dependency: ejb-server-side

  • Under properties, change the type to ejb-client

Step 4: Create JBoss EJB client descriptor

  • In /src/main/webapp/WEB-INF, create a file called jboss-ejb-client.xml
<jboss-ejb-client xmlns="urn:jboss:ejb-client:1.0"> 
            <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection"/> 

Step 5: Write EJB client code

  • In, add a method called getRemoteEJB
public RemoteEJB getRemoteEJB(){
    final Hashtable props = new Hashtable();
    props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
	Context context;
	RemoteEJB ejb = null;
	try {
	    context = new InitialContext(props); 
	    ejb = (RemoteEJB) context.lookup("ejb:/ejb-server-side//MyEJB!" + RemoteEJB.class.getName());

	} catch (NamingException e) {
	return ejb;
  • You'll need to import javax.naming.Context & javax.naming.InitialContext

Step 6: Remove the Local EJB

  • Delete the lines below and
MyEJB ejb;

Step 7: Declare our Remote EJB

  • At the top of our doGet method, add the following
RemoteEJB ejb = getRemoteEJB();

Step 8: Update Maven

  • Right click your project -> Maven -> Update Project

Step 9: Build the project

  • Right click your project -> Run as -> Maven Build.
Goals: clean install

Step 10: Find the build artifact

  • Right click your project -> Show In -> System Explorer
  • Under the target folder, there should be a .war file
  • Save the location of this file, we'll need it soon!

Create Web Tier Server

Step 1: Duplicate your $JBOSS_HOME directory

  • If you don't know where that is, see Part 1 under "Add a New User"
  • Once you find it, make a copy of it. Name the new directory jboss-eap-6.4-2.
  • We'll refer to this directory as $JBOSS_HOME2

Step 2: Start your server

  • In a terminal, start JBoss (for Windows, standalone.bat)
$ ./$JBOSS_HOME2/bin/

Step 3: Start the JBoss CLI (Command Line Interface)

  • In a separate terminal, run the CLI script (for Windows, jboss-cli.bat)
$ ./$JBOSS_HOME2/bin/ --connect

Step 4: Create a security realm

  • Add a new security realm called ejb-security-realm
  • Add your secret key from Part 1

Step 5: Create an outbound socket binding

/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=remote-socket-ejb:add(host=localhost, port=4447)

Step 6: Create a remote outbound connection

  • Add an outbound connection with our socket, security realm, and username (from Part 1)
  • Configure security settings


Step 7: Shutdown your server

  • In the CLI, type shutdown

Connect to Our Remote EJB

Step 1: Start Server 1 - Business Tier

  • In a terminal, navigate to your $JBOSS_HOME/bin
$ ./

Step 2: Start Server 2 - Web Tier

  • In a separate terminal, navigate to your $JBOSS_HOME2/bin
$ ./ -Djboss.socket.binding.port-offset=100

Step 3: Delete old deployments

  • Delete everything in $JBOSS_HOME2/standalone/deployments

Step 4: Deploy our Web Application

  • Copy the simple-remoteejb.war file (from earlier) to $JBOSS_HOME2/standalone/deployments
  • You should see a .deployed file created

Step 5: Connect to our remote EJB


Create Web Application

  • We added our Remote EJB (ejb-server-side) as a dependency, since we reference it in our servlet
  • We created a EJB client descriptor to configure settings for a Remote EJB connection. In the next section, we actually create the connection using the CLI
  • We wrote client code to lookup our remote EJB
  • InitialContext - an environment we'll use to connect to our EJB
  • URL_PKG_PREFIXES & org.jboss.ejb.client.naming - tells the environment to use our descriptor file to create our remote connection
  • Once we connect to our remote server, we need to lookup our EJB
  • JNDI (Java Naming Directory Interface) ¬†allows us to lookup Java objects
  • Syntax to lookup an EJB. Read more here

Create Web Tier Server

  • We created a second server to host our web application
  • We used the CLI to add a remote outbound connection to our business tier server
  • The CLI (Command Line Interface) is an easy way to modify your JBoss server
  • In JBoss, port 4447 is used for remote EJB calls
  • Remote outbound connections include
  • Remote socket - the hostname & port of our remote server.
  • Username - our credentials from Part 1
  • Security Realm - our secret key for authentication

Connect to Our Remote EJB

  • We started our business tier server that is running our Remote EJB
  • We started our web server and deployed our web application
  • Since we're running two servers on one machine, we had to start the web server with a port-offset.
  • Otherwise, both servers will try to use the same ports & crash
  • Port offsets shift all server ports a certain amount
  • We connected to our web app and it called our remote EJB


  1. We use an EJB client descriptor to configure a remote connection
  2. We wrote client code to lookup our EJB by name (JNDI)
  3. We used the CLI to create a remote connection on our server
  1. Project Source Code
  2. EJB Invocations from a remote server
  3. The JBoss CLI

Ok, that's all for now!

Happy Coding,