How to call EJBs from another JBoss AS 7 instance

In this tutorial we will show how you can configure two JBoss AS 7 server instances to call each other via EJB.

Calling EJB which are located on a remote EJB server is done differentely depending if the remote client is a standalone client or if the client is located on another instance of JBoss AS 7.

J2SE Standalone clients will use a file name jboss-ejb-client.properties which defines the host name where EJBs are deployed along with other settings such as application username and password. You can read more on this tutorial which describes step by step how to configure your standalone clients.

Java EE clients located on another application server will need another procedure and another file named jboss-ejb-client.xml which is used by the Remote clients. In order to complete this procedure you will need to configure an application user on the JBoss AS 7 "server" (which exposes the EJBs) and configure an outbound remote connection on the JBoss AS 7 "client" which points to the "server" machine.

jboss as 7 remote ejb client

JBoss AS 7 "server" configuration:

So at first thing we will do is creating an application user on the JBoss AS 7 server:

./add-user.sh
What type of user do you wish to add?
a) Management User (mgmt-users.properties)
b) Application User (application-users.properties)
(a): b
Enter the details of the new user to add.
Realm (ApplicationRealm) :
Username : user1234
Password : password1234
Re-enter Password : password1234
What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank
for none) :
About to add user 'user1234' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'ejb' to file '/jboss-as-7.1.1.Final/standalone/configuration/application-users.properties'
Added user 'ejb' to file '/jboss-as-7.1.1.Final/domain/configuration/application-users.properties'
Added user 'ejb' with roles to file '/jboss-as-7.1.1.Final/standalone/configuration/application-roles.properties'
Added user 'ejb' with roles to file '/jboss-as-7.1.1.Final/domain/configuration/application-roles.properties'

Next step, you need to generate a base64 encoded string ("secret") for your password. If you are using a JBoss AS 7.1.2 release (e.g. 7.1.2 Alpha) you get it printed on the console automatically. For the 7.1.1 release you can use an online utility such as http://www.base64encode.org/.
In our example the base64 encoded password is cGFzc3dvcmQxMjM0ed

JBoss AS 7 "client" configuration:

Create a security realm which contains the password generated into the secret element:

<management>
            . . . . . .
            <security-realm name="ejb-security-realm">
                            <server-identities>
                            <secret value="cGFzc3dvcmQxMjM0"/>
                            </server-identities>
            </security-realm>
</management>

Now configure the outbound socket connection toward the "remote" JBoss AS 7 server. This will be done in two points of your configuration:

1) Into the remoting subsystem:

<subsystem xmlns="urn:jboss:domain:remoting:1.1">
    <connector name="remoting-connector" socket-binding="remoting" security-realm="ApplicationRealm"/>
        <outbound-connections>
              <remote-outbound-connection name="remote-ejb-connection" outbound-socket-binding-ref="remote-ejb"
                  username="user1234" security-realm="ejb-security-realm">
                   <properties>
                     <property name="SASL_POLICY_NOANONYMOUS" value="false"/>
                     <property name="SSL_ENABLED" value="false"/>
                   </properties>
              </remote-outbound-connection>
        </outbound-connections>
</subsystem>

2) Into the socketbinding group, where you need to specify the host name and port where you are going to connect (Replace 192.168.0.1 with the IP Address of the JBoss AS 7 "server")
<socket-binding-group name="standard-sockets" default-interface="public">
. . . .
     <outbound-socket-binding name="remote-ejb">
          <remote-destination host="192.168.0.1" port="4447"/>
     </outbound-socket-binding>
</socket-binding-group>


Application changes:
In terms of application code, you need to include the file jboss-ejb-client.xml into your client application which describes the outbound-connection to use for the EJB calls. Please note that this file needs to be placed into the META-INF folder of your EAR file or the WEB-INF of your Web application.

Example:

webclient.war
             WEB-INF/jboss-ejb-client.xml
<jboss-ejb-client xmlns="urn:jboss:ejb-client:1.2"> 
 
  <client-context >
     <ejb-receivers exclude-local-receiver="true">
         <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection" >
       </remoting-ejb-receiver>
     </ejb-receivers>
     </client-context>
</jboss-ejb-client>


That's all.

Please notice that if you are starting both JBoss AS 7 servers on the same machine you need to specify a port offset for one of the two servers and a jboss.node.name unique name
Ex.
Server1

standalone.bat -Djboss.node.name=nodeA

Server2

standalone.bat -Djboss.socket.binding.port-offset=150 -Djboss.node.name=nodeB

If you need an example of EJB to EJB communication in a clustered environment, we suggest having a look at this tutorial: EJB to EJB communication in a cluster (JBoss AS 7 - WildFly)

Related articles available on mastertheboss.com

JBoss AS 7 introduction

This is an introduction tutorial to the newest JBoss AS 7 which a

Develop Java EE applications with JBoss AS 7

In this tutorial we will learn how to create and deploy a Java EE

Adding users with JBoss AS 7

If you are planning to add new users to your management interface

Using JBoss AS 7 management API programmatically

In this tutorial we will show how to use detyped management API t

JNDI view in JBoss AS 7

JNDI can be checked on the naming subsystem of the application se

How to install a module on WildFly / JBoss AS 7

Installing a module on WildFly / JBoss AS 7 requires creating a p

Follow us on Twitter