Tag: jboss-ejb-client.properties

Why and How to use WildFly based EJB Client API for Client auto reconnect logic ?

Hi,

In JBossAS7 or in WildFly there are different approaches which we can use in order to access EJBs. Either using “EJB client API” or using “remote naming” approach. EJB Client API is a WildFly-specific API that allows invocation on remote EJBs. This client API isn’t based on JNDI. So remote clients need not rely on JNDI API to invoke on EJBs.

Using EJB Client API based approach for EJB lookup is the recommended approach. If the remote-naming is used there are some restrictions as there is no full support of the ejb-client features. Following are some points which encourages us to use the EJB Client API based approach over the remote-naming based approach for EJB invocations.

1. No load balancing, if the URL contains multiple “remote://” servers there is no load balancing, the first available server will be used and only in case it is not longer available there will be a failover to the next available one.

2. No cluster support. As a cluster needs to be defined in the “jboss-ejb-client.properties” this feature can not be used and there is no cluster node added

3. No client side interceptor. The EJBContext.getCurrent() can not be used and it is not possible to add a client interceptor

4. No UserTransaction support

5. All proxies become invalid if .close() for the related javax.naming.InitalContext is invoked, or the InitialContext is not longer referenced and gets garbage-collected. In this case the underlying EJBContext is destroyed and the connections are closed.

6. It is not possible to use remote-naming if the client is an application deployed on another JBoss instance

What this demo is about ?

This example demonstrates why to use the EJB Client API and not the remote naming based API. As part of this sample we will see how the EJB Client can automatically gets reconnected to WildFly automatically if the wildly instance goes down and rebooted again.

So lets start developing a very simple EJB3 application and then deploy it on WildFly 10 (WildFly 8/9 also can be used).

Developing and deploying EJB3 in WildFly

Step-1). Open a terminal and then move inside the WildFly 10 “bin” directory and then create “ejbuser” (with password “ejbuser@1”) in the ApplicationRealm as following:

   $ cd /PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/bin

   $ ./add-user.sh -a ejbuser ejbuser@1
      Updated user 'ejbuser' to file ‘/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/standalone/configuration/application-users.properties'
      Updated user 'ejbuser' to file ‘/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/domain/configuration/application-users.properties'

Step-2). Start the WildFly instance normally as following:

   $ cd /PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/bin
   $ ./standalone.sh 

Step-3). Lets now create a directory where we will place our EJB remote interface, EJB Bean and the clients.

   $ mkdir  WildFly_EJB_Client_Reconnect
   $ mkdir -p WildFly_EJB_Client_Reconnect/src
   $ cd /PATH/TO/WildFly_EJB_Client_Reconnect

Developing EJB Project

Step-4). First of all we will write EJB remote interface “CallerRemote.java” inside the “WildFly_EJB_Client_Reconnect/src” directory as following:

package ejb3;
public interface CallerRemote {
     public String testMethod(String name);
}

Step-5). Now we will write EJB Stateless Bean “CallerBean.java” inside the “WildFly_EJB_Client_Reconnect/src” directory as following:

package ejb3;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
@Stateless  
@Remote(CallerRemote.class)
public class CallerBean implements CallerRemote {
     public String testMethod(String name) {
          String result="";
		  System.out.println("\n\n\t[CallerBean] Bean's testMethod(String name) called...."); 
		  return "[CallerBean] returned Welcome, "+name;
	 }
 }

Step-6). As we want to package our above mentioned EJBs inside an EAR file so lets create an “application.xml” file which will be our EAR deployment descriptor. So lets create the following kind of application.xml file inside the “WildFly_EJB_Client_Reconnect/src”

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns="http://java.sun.com/xml/ns/javaee" 
             xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" 
             id="Application_ID" version="5">
  <module>
    <ejb>remoteEjbOne.jar</ejb>
  </module>
</application>

Writing EJB Client API approach based Client

Step-7). As we are going to use the EJB Client API based approach hence lets first create the “jboss-ejb-client.properties” as following inside the “WildFly_EJB_Client_Reconnect/src”, In this file we will define our WildFly server details where our EJB is deployed. While running this code we will need to make sure that this file is present in client CLASSPATH along with the “$JBOSS_HOME/bin.client/jboss-client.jar” file.

remote.connections=one
remote.connection.one.host=localhost
remote.connection.one.port=8080
remote.connection.one.username=ejbuser
remote.connection.one.password=ejbuser@1
remote.connection.one.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

Step-8). Now the most important of writing the EJB Client based code. So lets write the “Client.java” as following inside the “WildFly_EJB_Client_Reconnect/src”:

package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
import ejb3.CallerRemote;
public class Client {
     public static void main(String ar[]) throws Exception {
          Context context=null;
          try {
                Properties props = new Properties();
                props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
                context = new InitialContext(props);	
	            System.out.println("\n\tGot initial Context: "+context);		
          } catch (Exception e) {
                e.printStackTrace();
          }

          //   When the invocation starts you try stopping the WildFly and then restart it back to find out if client reconnection works or not.
          CallerRemote remote=(CallerRemote)context.lookup("ejb:TestEAR/remoteEjbOne/CallerBean!ejb3.CallerRemote");
          for (int i = 0; i <= 300; i++) {  
             try {
                   System.out.println("\n\t remote.testMethod(\"MiddlewareMagic!!!\") = "+remote.testMethod("MiddlewareMagic!!!"));
                   Thread.sleep(500);
              } catch(Exception e) {                    
                    System.out.println("\n\tEXCEPTION: " + e.getMessage());
                    e.printStackTrace();
                    try{ Thread.sleep(500); } catch(InterruptedException ee) {  ee.printStackTrace(); } 
              }  
          }  
     }
  }

Writing ANT script to build/deploy/run

Step-9). Now we will write the following kind of ANT build script which will build The EAR containing the EJBs, deploy it on WildFly as well as will run the client. So lets create the “build.xml” file inside the “WildFly_EJB_Client_Reconnect” directory as following:

<project name="SingletonStartupService" default="all">
<property name="jboss.home" value="/Users/jsensharma/NotBackedUp/Installed/wildfly-10.0.0.CR3-SNAPSHOT" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="src.dir" value="src" />
<property name="output.dir" value="test" />
<property name="application.dir" value="build" />
<property name="ear.name" value="TestEAR.ear" />
<property name="ejb.one.jar" value="remoteEjbOne.jar" />
<property name="client.jar.name" value="remoteEJBClient.jar" />

        <path id="jboss.classpath">
           <fileset dir="${jboss.module.dir}/system/layers/base/javax/ejb/api/main">
               <include name="jboss-ejb-api*.jar"/>
           </fileset>  
        </path>

        <!-- Client Needs the following Jar to be present in the CLASSPATH including -->
        <path id="jboss.new.client.classpath">
           <fileset dir="${jboss.home}/bin/client">
               <include name="jboss-client.jar" />
           </fileset>  
        </path>
	 
        <target name="all" depends="buildEAR,message" />

        <target name="buildEAR" >
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}/META-INF" />
           <mkdir dir="${application.dir}" />
           <javac srcdir="${src.dir}" destdir="${tmp.dir}"  includes="Caller*.java" classpathref="jboss.classpath"/>
           <copy file="${src.dir}/CallerBean.java" todir="${tmp.dir}/ejb/one"/>
           <copy file="${src.dir}/CallerRemote.java" todir="${tmp.dir}/ejb/one"/>
           <jar jarfile="${output.dir}/${ejb.one.jar}" basedir="${tmp.dir}" compress="true" />

           <mkdir dir="${output.dir}/META-INF"/>
           <copy todir="${output.dir}/META-INF">
                <fileset dir="${src.dir}/">
                  <include name="application.xml"/> 
                </fileset>
           </copy>           
           <jar jarfile="${application.dir}/${ear.name}" basedir="${output.dir}" compress="true" />
           <delete includeEmptyDirs="true">
              <fileset dir="${output.dir}"/>
           </delete> 
           <delete dir="${tmp.dir}" />
        </target>

        <target name="message" depends="buildEAR">
            <echo message="*******************  ******************* *********************" />  
            <copy file="${application.dir}/${ear.name}" tofile="${jboss.home}/standalone/deployments/${ear.name}"/>
            <echo message="********** ${application.dir}/${ear.name} Build and Deployed Successfully **********" />  
            <echo message="*******************  ******************* *********************" />  
        </target>

        <target name="run">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}" destdir="${tmp.dir}"  includes="CallerRemote.java,Client.java" classpathref="jboss.classpath"/> 
           <copy file="${src.dir}/CallerRemote.java" todir="${tmp.dir}/ejb3"/>
           <copy file="${src.dir}/Client.java" todir="${tmp.dir}/client"/>    
           <copy file="${src.dir}/jboss-ejb-client.properties" todir="${tmp.dir}"/>     
           <jar jarfile="${application.dir}/${client.jar.name}" basedir="${tmp.dir}" compress="true" />
           <delete dir="${tmp.dir}"/>

           <java classname="client.Client" fork="true">
               <classpath>
                  <pathelement location="${application.dir}/${client.jar.name}"/>
                  <path refid="jboss.new.client.classpath"/>
               </classpath>
           </java>
        </target>        
    
</project>

Step-10). We can run the above ANT based EJB client project simply by setting the ANT_HOME, JAVA_HOME and PATH, So open a command prompt and then run the following commands as following:

For Unix Based OS:

$ export ANT_HOME=/PATH/TO/apache_ant_1.9.2
$ export JAVA_HOME=/PATH/TO/jdk1.8.0_60
$ export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
$ cd /PATH/TO/WildFly10_JMS_Client


<strong>For Windows Based OS</strong>
$ set ANT_HOME=C:\PATH\TO\apache_ant_1.9.2
$ set JAVA_HOME=C:\PATH\TO\jdk1.8.0_60
$ set PATH=%JAVA_HOME%\bin;%ANT_HOME%\bin;%PATH%
$ cd C:\WildFly10_JMS_Client

Step-11). Lets deploy the application by running the ANT:

$ ant
Buildfile: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/build.xml

buildEAR:
    [mkdir] Created dir: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp/META-INF
    [javac] /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/build.xml:32: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 2 source files to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp/ejb/one
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp/ejb/one
      [jar] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/test/remoteEjbOne.jar
    [mkdir] Created dir: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/test/META-INF
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/test/META-INF
      [jar] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/build/TestEAR.ear
   [delete] Deleting directory /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp

message:
     [echo] *******************  ******************* *********************
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/Installed/wildfly-10.0.0.CR3-SNAPSHOT/standalone/deployments
     [echo] ********** build/TestEAR.ear Build and Deployed Successfully **********
     [echo] *******************  ******************* *********************

all:

BUILD SUCCESSFUL
Total time: 0 seconds

Step-12). Now Run the client using the same Client.

 ant run
Buildfile: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/build.xml

run:
    [mkdir] Created dir: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp
    [javac] /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/build.xml:60: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 2 source files to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp/ejb3
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp/client
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp
      [jar] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/build/remoteEJBClient.jar
   [delete] Deleting directory /Users/jsensharma/NotBackedUp/MM_Tests/Forums/WildFly/WildFly_EJB_Client_Reconnect/tmp
      
      	Got initial Context: javax.naming.InitialContext@ae3865e
      Nov 08, 2015 9:13:51 PM org.jboss.ejb.client.EJBClient <clinit>
      INFO: JBoss EJB Client version 2.1.2.Final
      Nov 08, 2015 9:13:51 PM org.xnio.Xnio <clinit>
      INFO: XNIO version 3.3.2.Final
      Nov 08, 2015 9:13:51 PM org.xnio.nio.NioXnio <clinit>
      INFO: XNIO NIO Implementation Version 3.3.2.Final
      Nov 08, 2015 9:13:51 PM org.jboss.remoting3.EndpointImpl <clinit>
      INFO: JBoss Remoting version 4.0.14.Final
      Nov 08, 2015 9:13:52 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
      INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      Nov 08, 2015 9:13:52 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
      INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@6f526c5f, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection@c490a12,channel=jboss.ejb,nodename=banl13bca644a-3]} on channel Channel ID b29f24ca (outbound) of Remoting connection 3af686da to localhost/127.0.0.1:8080
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!

EJB Client reconnection test

Now in order to see if the EJB Client is able to reconnect to the restarted WildFly automatically or not, Let the above EJB client keep running and in the mean time restart the WildFly, We will notice that the EJB Client gets the following exception for sometime until the WildFly is restarted and as soon as the WildFly is completely restarted then the EJB Client API approach based EJB Client automatically reconnects to it.

      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      	EXCEPTION: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@b10b9b2
      java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@3d600a0c
      	at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)
      	at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116)
      	at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
      	at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)
      	at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)
      	at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
      	at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
      	at com.sun.proxy.$Proxy0.testMethod(Unknown Source)
      	at client.Client.main(Unknown Source)
      
      	EXCEPTION: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@3d600a0c
      Nov 08, 2015 9:17:39 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
      INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
      Nov 08, 2015 9:17:39 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
      INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@f342a0b, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection@1a8ed249,channel=jboss.ejb,nodename=banl13bca644a-3]} on channel Channel ID fe6d7219 (outbound) of Remoting connection 511533be to localhost/127.0.0.1:8080
      java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@5f51da55
      	at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)
      	at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116)
      	at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
      	at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)
      	at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)
      	at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
      	at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
      	at com.sun.proxy.$Proxy0.testMethod(Unknown Source)
      	at client.Client.main(Unknown Source)
      
      	EXCEPTION: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@5f51da55
      java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@271d900d
      	at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)
      	at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116)
      	at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
      	at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)
      	at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)
      	at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
      	at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
      	at com.sun.proxy.$Proxy0.testMethod(Unknown Source)
      	at client.Client.main(Unknown Source)
      
      	EXCEPTION: EJBCLIENT000025: No EJB receiver available for handling [appName:TestEAR, moduleName:remoteEjbOne, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@271d900d
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!
      
      	 remote.testMethod("MiddlewareMagic!!!") = [CallerBean] returned Welcome, MiddlewareMagic!!!

.
.
The Source Code for this demo can be found in:
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WildFly/EJB/WildFly_EJB_Client_Reconnect
.
.
Regards
Jay SenSharma


Invoking EJBs from One JBossAS7 to another JBossAS7

Hi,

In our previous examples we have seen how to invoke an EJB which is deployed on JBoss AS7 server from a standalone java client (http://middlewaremagic.com/jboss/?p=1466). However in this example we will see how to invoke an EJB from another EJB when both the EJBs are deployed on different JBoss instances. This is different from invoking the EJBs from a remote standalone client.

Reference Document:
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+server+instance

Points we will discuss here:

Point-1) . In JBossAS7 how to create/delete Server-Groups, JBoss Servers, Outbound-Connections and Security realm using Batch Mode of CLI.

Point-2) . We will see how to configure a Server-Identity for the JBoss to JBoss EJB communication purpose.

Point-3) . We will see how to invoke an EJB from a standalone java client.

Point-4) . We will see how One EJB deployed on the Client Server (JBossServerOne) can invoke an EJB which is deployed on the destination server (JBossServerTwo)

Point-5) . The source code of this demo along with the “domain.xml” & “host.xml” including the Example source is present in the github repository.
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/EJB_Server_to_Server

Point-6) . We will see how to make use of “$EAR/META-INF/jboss-ejb-client.xml” file. Like following:

<jboss-ejb-client xmlns="urn:jboss:ejb-client:1.0">
    <client-context>
        <ejb-receivers>
            <remoting-ejb-receiver outbound-connection-ref="OutboundEJB_ConnectionOne"/>
        </ejb-receivers>
    </client-context>
</jboss-ejb-client>
EJB_to_EJB_Call_Between_JBossAS7_Servers

EJB_to_EJB_Call_Between_JBossAS7_Servers

Creating a User which will be available in Destination Server.

Step-1) Create a User with username “ejbUserOne” and password as “ejbPasswordOne” in the client Server.

[userone@localhost bin]$ ./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) : ApplicationRealm
Username : ejbUserOne
Password : ejbPasswordOne
Re-enter Password : ejbPasswordOne
What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: ejbRole
About to add user 'ejbUserOne' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'ejbUserOne' to file '/home/userone/jboss-as-7.1.2.Final/standalone/configuration/application-users.properties'
Added user 'ejbUserOne' to file '/home/userone/jboss-as-7.1.2.Final/domain/configuration/application-users.properties'
Added user 'ejbUserOne' with roles ejbRole to file '/home/userone/jboss-as-7.1.2.Final/standalone/configuration/application-roles.properties'
Added user 'ejbUserOne' with roles ejbRole to file '/home/userone/jboss-as-7.1.2.Final/domain/configuration/application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS process e.g. slave domain controller?
yes/no? yes

To represent the user add the following to the server-identities definition <secret value="ZWpiUGFzc3dvcmRPbmU=" />

NOTE: The “ejbUserOne” which we created in destination Server “JBossServerTwo” has generated a secret value “ZWpiUGFzc3dvcmRPbmU=” which we need to add in the “host.xml” of the Client JBoss where we will deploy the CallerBean.

In our case for our simplicity we will create the same user “ejbUserOne” in the clientServer “JBossServerOne” as well sothat the Standalone client also can use the same username & password in order to invoke the CallerBean deployed on Client Server “JBossServerOne”

Step-2) Create a directory somewhere in your file system like “/home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/” and place the CLI Batch file “ConfigureResources.cli” inside the above directory which we will use oin order to configure various resources on the JBoss quickly.

NOTE: In the following script you will need to replace the value secret key value (line-43) “ZWpiUGFzc3dvcmRPbmU=” which is generated in Step-1 (if you want to create a different username)

#################################################################
# Removing all Unnecessary & Default Configurations from Server
#################################################################
batch
# Stopping all the default servers, and removing default groups & servers
/host=master/server-config=server-one:stop(blocking=true)
/host=master/server-config=server-two:stop(blocking=true)

/host=master/server-config=server-one:remove
/host=master/server-config=server-two:remove
/host=master/server-config=server-three:remove
/server-group=main-server-group:remove
/server-group=other-server-group:remove

#################################################################
# Disabling the "$local" authentication from the Configuration
#################################################################
/host=master/core-service=management/security-realm=ApplicationRealm/authentication=local:remove


#################################################################
# EJB server to server call related configuration
#################################################################
/socket-binding-group=full-sockets/remote-destination-outbound-socket-binding=OutboundEJBOne:add(host=localhost, port=4647)

# adding the outbound connections to the remoting subsystem of the full-profile
/profile=full/subsystem=remoting/remote-outbound-connection=OutboundEJB_ConnectionOne:add(outbound-socket-binding-ref=remote-ejb, security-realm=ejbRealmOne, username=ejbUserOne)
/profile=full/subsystem=remoting/remote-outbound-connection=OutboundEJB_ConnectionOne/property=SASL_POLICY_NOANONYMOUS:add(value=false)
/profile=full/subsystem=remoting/remote-outbound-connection=OutboundEJB_ConnectionOne/property=SSL_ENABLED:add(value=false)

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

#################################################################
# Security-Realm configuration
#################################################################
#### using    add-user.sh   create a user with name "ejbUserOne"  and password  as "ejbPasswordOne" on the Client Server
#### Press "yes" When the user creation script asks:
#### Is this new user going to be used for one AS process to connect to another AS process e.g. slave domain controller?
####  it will generate an encrypted password., Use it in the below section.

/host=master/core-service=management/security-realm=ejbRealmOne:add()
/host=master/core-service=management/security-realm=ejbRealmOne/server-identity=secret:add(value=ZWpiUGFzc3dvcmRPbmU=)


#################################################################
# Configuring New Servers & Server Groups
#################################################################
/server-group=GroupOne:add(profile=full,socket-binding-group=full-sockets)
/server-group=GroupOne/jvm=default:add(permgen-size=64m, max-permgen-size=128m)
/host=master/server-config=JBossServerOne:add(auto-start=true, group=GroupOne, socket-binding-port-offset=100)


/server-group=GroupTwo:add(profile=full,socket-binding-group=full-sockets)
/server-group=GroupTwo/jvm=default:add(permgen-size=64m, max-permgen-size=128m)
/host=master/server-config=JBossServerTwo:add(auto-start=true, group=GroupTwo, socket-binding-port-offset=200)
run-batch

Step-3) Now start your JBoss Domain controller using the “domain.sh” script.

Step-4) Open a terminal and the move inside the “$JBOSS_HOME/bin” directory from where we will execute the above CLI Batch file in order to configure various resources on our JBoss.

[userone@localhost bin]$ ./jboss-cli.sh -c --controller=localhost:9999 --file=/home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/ConfigureResources.cli

Output will be something like following:

#1 /host=master/server-config=server-one:stop(blocking=true)
#2 /host=master/server-config=server-two:stop(blocking=true)

#3 /host=master/server-config=server-one:remove
#4 /host=master/server-config=server-two:remove
#5 /host=master/server-config=server-three:remove

#6 /server-group=main-server-group:remove
#7 /server-group=other-server-group:remove

#8 /host=master/core-service=management/security-realm=ApplicationRealm/authentication=local:remove

#9 /socket-binding-group=full-sockets/remote-destination-outbound-socket-binding=OutboundEJBOne:add(host=localhost, port=4647)

#10 /profile=full/subsystem=remoting/remote-outbound-connection=OutboundEJB_ConnectionOne:add(outbound-socket-binding-ref=remote-ejb, security-realm=ejbRealmOne, username=ejbUserOne)
#11 /profile=full/subsystem=remoting/remote-outbound-connection=OutboundEJB_ConnectionOne/property=SASL_POLICY_NOANONYMOUS:add(value=false)
#12 /profile=full/subsystem=remoting/remote-outbound-connection=OutboundEJB_ConnectionOne/property=SSL_ENABLED:add(value=false)

#13 /socket-binding-group=full-sockets/remote-destination-outbound-socket-binding=remote-ejb:add(host=localhost, port=4647)

#14 /host=master/core-service=management/security-realm=ejbRealmOne:add()
#15 /host=master/core-service=management/security-realm=ejbRealmOne/server-identity=secret:add(value=ZWpiUGFzc3dvcmRPbmU=)

#16 /server-group=GroupOne:add(profile=full,socket-binding-group=full-sockets)
#17 /server-group=GroupOne/jvm=default:add(permgen-size=64m, max-permgen-size=128m)

#18 /host=master/server-config=JBossServerOne:add(auto-start=true, group=GroupOne, socket-binding-port-offset=100)
#19 /server-group=GroupTwo:add(profile=full,socket-binding-group=full-sockets)
#20 /server-group=GroupTwo/jvm=default:add(permgen-size=64m, max-permgen-size=128m)
#21 /host=master/server-config=JBossServerTwo:add(auto-start=true, group=GroupTwo, socket-binding-port-offset=200)

The batch executed successfully.

Deploying and running the code:

You can download the TestCase from the following github repository. https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/EJB_Server_to_Server

Place the “EJB_Server_to_Server” directory somewhere in your file system thich contains the following:

├── domain_Configurations
│   ├── application-roles.properties
│   ├── application-users.properties
│   ├── domain.xml
│   ├── host.xml
│   └── mgmt-users.properties


├── EJB_2_EJB_Call_Between_Servers.JPG


└── Server_To_Server_EJB3_Remote_Lookup
    ├── build
    │   ├── ejbOneEAR.ear
    │   ├── ejbTwoEAR.ear
    │   └── remoteEJBClient.jar
    ├── build.xml
    ├── ConfigureResources.cli
    └── src
        ├── client
        │   ├── CallerRemote.java
        │   ├── Client.java
        │   └── jboss-ejb-client.properties
        ├── ejbOneEAR
        │   ├── application.xml
        │   ├── CallerBean.java
        │   ├── CallerRemote.java
        │   ├── jboss-ejb-client.xml
        │   └── TestRemote.java
        └── ejbTwoEAR
            ├── application.xml
            ├── TestBean.java
            └── TestRemote.java

Code snippet of “CallerBean.java”

package ejb.one;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import ejb.two.TestRemote;
@Stateless  
@Remote(CallerRemote.class)
public class CallerBean implements CallerRemote
 {
     public String testMethod(String name)
	    {
                   String result="";
		   System.out.println("nnt[CallerBean] Bean's testMethod(String name) called....");
                   System.out.println("nnt[CallerBean] Is Now calling the TestBean deployed on another JBoss Instance.");
                   try {
                         Hashtable<String,String> props = new Hashtable<String,String>();
                         props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

                         /******* FOLLOWING  CODE IS NOT NEEDED  ********/
                         //props.put(Context.PROVIDER_URL, "remote://localhost:4647"); 
                         //props.put(Context.SECURITY_PRINCIPAL, "ejbUserOne");
                         //props.put(Context.SECURITY_CREDENTIALS, "ejbPasswordOne");

                         Context context = new javax.naming.InitialContext(props);
                         String jndiName="ejb:" + "ejbTwoEAR/remoteEjbTwo/TestBean!ejb.two.TestRemote";
                         TestRemote remote = (TestRemote) context.lookup(jndiName);

                         result = remote.helloWorld("TestMessage For TestBean: MiddlewareMagic");
                         System.out.println("[CallerBean] Received result from TestBean: " + result);
                        } 
                    catch (Exception e) 
                        {
                         throw new RuntimeException(e);
                        }
		   return "[CallerBean] returned Hello "+name+"tTestBean: " + result;
	    }
 }

Now open a Terminal where we need to set the PATH like following sothat ANT is available in the path and then compile and run the program:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.8.0/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.8.0/bin;%PATH%

Compile the program as following:


[userone@localhost Server_To_Server_EJB3_Remote_Lookup]$ cd /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup
[userone@localhost Server_To_Server_EJB3_Remote_Lookup]$ ant
Buildfile: build.xml

build_ear_one:
    [mkdir] Created dir: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
    [javac] Compiling 3 source files to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
      [jar] Building jar: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/remoteEjbOne.jar
    [mkdir] Created dir: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/META-INF
     [copy] Copying 2 files to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/META-INF
      [jar] Building jar: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/ejbOneEAR.ear
   [delete] Deleting: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/remoteEjbOne.jar
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/build
   [delete] Deleting: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/ejbOneEAR.ear

build_ear_two:
   [delete] Deleting directory /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
    [mkdir] Created dir: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
    [javac] Compiling 2 source files to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
      [jar] Building jar: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/remoteEjbTwo.jar
    [mkdir] Created dir: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/META-INF
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/META-INF
      [jar] Building jar: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/ejbTwoEAR.ear
   [delete] Deleting: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/remoteEjbTwo.jar
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/build
   [delete] Deleting: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp/ejbTwoEAR.ear

message:
     [echo] *******************  ******************* *********************
     [echo] ********** build/ejbOneEAR.ear Build Successfully **********
     [echo] ********** build/ejbTwoEAR.ear Build Successfully **********
     [echo] Deploy the above EARs on your desired servers using CLI script or using Manaagement Console
     [echo] *******************  ******************* *********************

all:

BUILD SUCCESSFUL
Total time: 2 seconds

Deploy & Run
Now deploy the “ejbOneEAR.ear” on “JBossServerOne”
And
deploy the “ejbTwoEAR.ear” on “JBossServerTwo”
And
Then Run the program as following:

[jsenshar@localhost Server_To_Server_EJB3_Remote_Lookup]$ ant run
Buildfile: build.xml

run:
   [delete] Deleting directory /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
    [mkdir] Created dir: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
    [javac] Compiling 2 source files to /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
      [jar] Building jar: /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/build/remoteEJBClient.jar
   [delete] Deleting directory /home/userone/jboss-as-7.1.2.Final/domain/EJB_Server_to_Server/Server_To_Server_EJB3_Remote_Lookup/tmp
      Sep 10, 2012 2:20:25 AM org.xnio.Xnio <clinit>
      INFO: XNIO Version 3.0.3.GA
      Sep 10, 2012 2:20:25 AM org.xnio.nio.NioXnio <clinit>
      INFO: XNIO NIO Implementation Version 3.0.3.GA
      Sep 10, 2012 2:20:25 AM org.jboss.remoting3.EndpointImpl <clinit>
      INFO: JBoss Remoting version 3.2.7.GA
      
      	Got initial Context: javax.naming.InitialContext@671ff436
      Sep 10, 2012 2:20:26 AM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
      INFO: EJBCLIENT000017: Received server version 1 and marshalling strategies [river]
      Sep 10, 2012 2:20:26 AM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
      INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@62ebcdbb, receiver=Remoting connection EJB receiver [connection=Remoting connection <65efb4be>,channel=jboss.ejb,nodename=master:JBossServerOne]} on channel Channel ID 9a4598f0 (outbound) of Remoting connection 5d2aea3e to localhost/127.0.0.1:4547
      Sep 10, 2012 2:20:26 AM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
      WARN: Unsupported message received with header 0xffffffff
      
      	 remote.testMethod("MiddlewareMagic") = [CallerBean] returned Hello MiddlewareMagic	TestBean: [TestBean] returned Hello TestMessage For TestBean: MiddlewareMagic

BUILD SUCCESSFUL
Total time: 3 seconds

.
.
Thanks 🙂
MiddlewareMagic Team


Simplified way of EJB Remote Lookup in JBoss AS7.1.0.Final

Hi,

Until “JBoss AS7.1.0 CR1b” We saw a very combersome way of looking up an EJB from a remote client. But based on the demand of the community guys Now the “JBoss AS 7.1.0.Final” provides a very easy way of invoking the EJBs remotely from the client side.

We discussed the previous option of looking up EJB remotely till “JBoss AS7.1.0 CR1b” as described in the following link: “Minimum Jars Required for Client app to remotely Invoke EJBs in JBoss AS7”

.
.

What’s New in JBoss AS7.1.0.Final?

In this article we are going to see an Easiest way to lookup the EJBs remotely in >”JBoss AS7.1.0.Final”

Point-1). JBoss AS7.1.0.Final provides a new Jar file for the Client side in order to perform remote lookup’s of JMS and EJB components “jboss-as-7.1.0.Final/bin/client/jboss-client-7.1.0.Final.jar”. This jar should be used with standalone clients only, not with deployments are that deployed to an AS7 instance.

Point-2). What will be the syntax of the JNDI name while invoking a Stateless SessionBean?
For Stateless EJBs Beans JNDI Naming will be in following format:

                 .
<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
                 .

Example: “TestRemoteEJBEAR/remoteEJB//CallerBean!remote.CallerRemote”

Point-3). On the client side we will need to use a new Protocul “remote://” as following:

.

  String JBOSS_CONTEXT="org.jboss.naming.remote.client.InitialContextFactory";;
  Properties props = new Properties();
  props.put(Context.INITIAL_CONTEXT_FACTORY, JBOSS_CONTEXT);
  props.put(Context.PROVIDER_URL, "remote://localhost:4447"); 
  props.put(Context.SECURITY_PRINCIPAL, "testuser");
  props.put(Context.SECURITY_CREDENTIALS, "testpassword");
  context = new InitialContext(props);	
.

Point-4). A properties file need to be placed in the Clients CLASSPATH with name “jboss-ejb-client.properties”

Point-5): [ IMPORTANT ] Security is the main focus now so before running “JBoss AS7.1.0.Final” we will need to create a new Application User by running “${JBOSS_HOME}/bin/add-user.sh” script as following :

[userone@localhost bin]$ ./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) :  ApplicationRealm
Username : testuser
Password : testpassword
Re-enter Password : testpassword

What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none) : testrole
About to add user 'testuser' for realm 'ApplicationRealm'

Is this correct yes/no? yes

Added user 'testuser' to file '/home/userone/jboss-as-7.1.0.Final/standalone/configuration/application-users.properties'
Added user 'testuser' to file '/home/userone/jboss-as-7.1.0.Final/domain/configuration/application-users.properties'
Added user 'testuser' with roles testrole to file '/home/userone/jboss-as-7.1.0.Final/standalone/configuration/application-roles.properties'
Added user 'testuser' with roles testrole to file '/home/userone/jboss-as-7.1.0.Final/domain/configuration/application-roles.properties'
.
.

.
.

Source Code (Git Repo) Source Code for this Demo Can be downloaded from GIT Repository:
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/New_EJB3_Remote_Lookup

Once you have gone through the above mentioned 4- Points we can now begin writing our EJB Based application.

Developing Simple EJB3 Application

Step-1). Create a directory somewhere in your file system like “/home/userone/New_EJB3_Remote_Lookup” and then create another directory with name “src” inside the “/home/userone/New_EJB3_Remote_Lookup” directory.

Step-2). Write the EJB Stateless Bean class “CallerBean.java” as following inside the directory “/home/userone/New_EJB3_Remote_Lookup/src”:

package remote;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;

@Stateless  
@Remote(CallerRemote.class)
public class CallerBean implements CallerRemote
 {
     public String testMethod(String name)
	    {
		   System.out.println("nnt Bean's testMethod(String name) called....");
		   return "[CallerBean] returned Hello "+name;
	    }
 }

Step-3). Write the EJB Remote Interface “CallerRemote.java” as following inside the directory “/home/userone/New_EJB3_Remote_Lookup/src”:

package remote;
public interface CallerRemote
{
     public String testMethod(String name);
}

Step-4). As we are going to create an EAR file containing the above Stateless Session Bean so We will write the “application.xml” as following inside the directory “/home/userone/New_EJB3_Remote_Lookup/src”:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" id="Application_ID" version="5">
  <module>
    <ejb>remoteEJB.jar</ejb>
  </module>
</application>

Developing EJB3 Remote Client

Step-5). Now we will write the Client side code to access the Above mentioned EJB Remotely so create a class “TestRemoteClientA.java” as following inside the directory “/home/userone/New_EJB3_Remote_Lookup/src”:

package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
import remote.CallerRemote;
public class TestRemoteClientA
  {
      public static void main(String ar[]) throws Exception 
       {
          Context context=null;
       try
         {
                Properties props = new Properties();
                props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
                props.put(Context.PROVIDER_URL, "remote://localhost:4447"); 
                props.put(Context.SECURITY_PRINCIPAL, "testuser");
                props.put(Context.SECURITY_CREDENTIALS, "testpassword");
                context = new InitialContext(props);	
	        System.out.println("ntGot initial Context: "+context);		
         }
       catch (Exception e)
         {
                e.printStackTrace();
          }

         // Lookup Format will be 
         // <app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
   
         CallerRemote remote=(CallerRemote)context.lookup("TestRemoteEJBEAR/remoteEJB//CallerBean!remote.CallerRemote");
         System.out.println("nt remote.testMethod("MiddlewareMagic") = "+remote.testMethod("MiddlewareMagic"));
       }
  }

Step-6). Now we will need to create a properties file “jboss-ejb-client.properties” as following inside the directory “/home/userone/New_EJB3_Remote_Lookup/src”:


remote.connections=default
endpoint.name=client-endpoint
remote.connection.default.port = 4447
remote.connection.default.host=localhost
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

NOTE: The above properties file need to be present inside the client’s classpath. In our case we are going to put this file inside a JAR file “remoteEJBClient.jar” and then we will add this JAR inside the client’s classpath.

Step-7). Now Start your JBoss Profile as following from inside the directory “/home/userone/jboss-as-7.1.0.Final/bin”:

.
 ./standalone.sh -c standalone-full.xml
.

Step-8). Now the most important part of building / deploying and running the application so in order to achieve that we will create a simple ant build script, So Create a “build.xml” file inside the “/home/userone/New_EJB3_Remote_Lookup” directory as following:

<project name="SingletonStartupService" default="all">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.Final" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="src.dir" value="src" />
<property name="output.dir" value="build" />
<property name="ear.name" value="TestRemoteEJBEAR.ear" />
<property name="ejb.jar" value="remoteEJB.jar" />
<property name="client.jar.name" value="remoteEJBClient.jar" />

        <path id="jboss.classpath">
           <fileset dir="${jboss.module.dir}">
               <include name="**/*.jar"/>
           </fileset>  
        </path>

        <!-- Client Needs the following Jar to be present in the CLASSPATH including -->
        <path id="jboss.new.client.classpath">
           <fileset dir="${jboss.home}/bin/client">
               <include name="jboss-client-7.1.0.Final.jar" />
           </fileset>  
        </path>
	 
        <target name="all" depends="deploy" />

        <target name="build_ear">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}" destdir="${tmp.dir}"  includes="*.java" classpathref="jboss.classpath"/>

           <jar jarfile="${tmp.dir}/${ejb.jar}" basedir="${tmp.dir}" compress="true" />

           <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}/remote"/>
           </delete> 
           <mkdir dir="${tmp.dir}/META-INF"/>
           <copy todir="${tmp.dir}/META-INF">
                <fileset dir="${src.dir}/">
                  <include name="application.xml"/> 
                </fileset>
           </copy>           
           <jar jarfile="${tmp.dir}/${ear.name}" basedir="${tmp.dir}" compress="true" />
           <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}/META-INF"/>
           </delete> 
           <delete file="${tmp.dir}/${ejb.jar}"/>

           <copy file="${tmp.dir}/${ear.name}" tofile="${output.dir}/${ear.name}"/>
           <delete file="${tmp.dir}/${ear.name}"/>
        </target>


        <target name="deploy" depends="build_ear">
            <echo message="*******************  Deploying the EAR file ${ear.name} *********************" />  
            <echo message="********** ${output.dir}/${ear.name} to ${jboss.home}/standalone/deployments **********" />  
            <copy todir="${jboss.home}/standalone/deployments/">
                <fileset dir="${output.dir}/">
                  <include name="${ear.name}"/> 
                </fileset>
            </copy>
            <echo message="*******************  Deployed Successfully   *********************" />  
        </target>


        <target name="run">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <copy file="${src.dir}/jboss-ejb-client.properties" toDir="${tmp.dir}"/> 

           <javac srcdir="${src.dir}" destdir="${tmp.dir}"  includes="CallerRemote.java,TestRemoteClientA.java" classpathref="jboss.classpath"/>          
           <jar jarfile="${output.dir}/${client.jar.name}" basedir="${tmp.dir}" compress="true" />
           <delete dir="${tmp.dir}"/>

           <java classname="client.TestRemoteClientA" fork="true">
               <classpath>
                  <pathelement location="${output.dir}/${client.jar.name}"/>
                  <path refid="jboss.new.client.classpath"/>
               </classpath>
           </java>
        </target>        
    
</project>

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script to point to your own JBoss AS7 directory.

Step-9). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step-10). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/userone/New_EJB3_Remote_Lookup” and then run the ant to build and deploy the EJB based EAR applicationon your JBoss Standalone full profile, by running the command “ant deploy”

[userone@localhost New_EJB3_Remote_Lookup]$ ant deploy
ant deploy
Buildfile: build.xml

build_ear:
    [mkdir] Created dir: /home/userone/New_EJB3_Remote_Lookup/tmp
    [javac] Compiling 3 source files to /home/userone/New_EJB3_Remote_Lookup/tmp
      [jar] Building jar: /home/userone/New_EJB3_Remote_Lookup/tmp/remoteEJB.jar
    [mkdir] Created dir: /home/userone/New_EJB3_Remote_Lookup/tmp/META-INF
     [copy] Copying 1 file to /home/userone/New_EJB3_Remote_Lookup/tmp/META-INF
      [jar] Building jar: /home/userone/New_EJB3_Remote_Lookup/tmp/TestRemoteEJBEAR.ear
   [delete] Deleting: /home/userone/New_EJB3_Remote_Lookup/tmp/remoteEJB.jar
     [copy] Copying 1 file to /home/userone/New_EJB3_Remote_Lookup/build
   [delete] Deleting: /home/userone/New_EJB3_Remote_Lookup/tmp/TestRemoteEJBEAR.ear

deploy:
     [echo] *******************  Deploying the EAR file TestRemoteEJBEAR.ear *********************
     [echo] ********** build/TestRemoteEJBEAR.ear to /home/userone/jboss-as-7.1.0.Final/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.0.Final/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

BUILD SUCCESSFUL
Total time: 1 second

As soon as the EJB will be deployed on the JBoss AS7.1.0.Final you will see the following kind of output on your JBoss Console (STDOUT):

22:44:32,587 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "TestRemoteEJBEAR.ear"
22:44:32,595 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "remoteEJB.jar"
22:44:32,626 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named CallerBean in deployment unit subdeployment "remoteEJB.jar" of deployment "TestRemoteEJBEAR.ear" are as follows:

	java:global/TestRemoteEJBEAR/remoteEJB/CallerBean!remote.CallerRemote
	java:app/remoteEJB/CallerBean!remote.CallerRemote
	java:module/CallerBean!remote.CallerRemote
	java:jboss/exported/TestRemoteEJBEAR/remoteEJB/CallerBean!remote.CallerRemote
	java:global/TestRemoteEJBEAR/remoteEJB/CallerBean
	java:app/remoteEJB/CallerBean
	java:module/CallerBean

22:44:32,654 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018565: Replaced deployment "TestRemoteEJBEAR.ear" with deployment "TestRemoteEJBEAR.ear"

Step-11). Now we will try to compile and run the EJB3 remote Client application by running the command “ant run”

[userone@localhost New_EJB3_Remote_Lookup]$ ant run
Buildfile: build.xml

run:
   [delete] Deleting directory /home/userone/New_EJB3_Remote_Lookup/tmp
    [mkdir] Created dir: /home/userone/New_EJB3_Remote_Lookup/tmp
     [copy] Copying 1 file to /home/userone/New_EJB3_Remote_Lookup/tmp
    [javac] Compiling 2 source files to /home/userone/New_EJB3_Remote_Lookup/tmp
      [jar] Building jar: /home/userone/New_EJB3_Remote_Lookup/build/remoteEJBClient.jar
   [delete] Deleting directory /home/userone/New_EJB3_Remote_Lookup/tmp
      Feb 18, 2012 10:45:52 PM org.xnio.Xnio <clinit>
      INFO: XNIO Version 3.0.3.GA
      Feb 18, 2012 10:45:52 PM org.xnio.nio.NioXnio <clinit>
      INFO: XNIO NIO Implementation Version 3.0.3.GA
      Feb 18, 2012 10:45:52 PM org.jboss.remoting3.EndpointImpl <clinit>
      INFO: JBoss Remoting version 3.2.2.GA
      
      	Got initial Context: javax.naming.InitialContext@1ebcda2d
      Feb 18, 2012 10:45:53 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
      INFO: Received server version 1 and marshalling strategies [river]
      Feb 18, 2012 10:45:53 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
      INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@1309de5b, receiver=Remoting connection EJB receiver [connection=Remoting connection <30ff94b1>,channel=jboss.ejb,nodename=localhost]} on channel Channel ID 8359e77c (outbound) of Remoting connection 1fe903d5 to localhost/127.0.0.1:4447
      Feb 18, 2012 10:45:53 PM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
      WARN: Unsupported message received with header 0xffffffff
      
      	 remote.testMethod("MiddlewareMagic") = [CallerBean] returned Hello MiddlewareMagic

BUILD SUCCESSFUL
Total time: 2 seconds

References

[1] EJB invocations from a remote client using JNDI.
[2] Remote EJB invocations via JNDI – EJB client API or remote-naming project.
[3] EJB invocations from a remote server instance.
.
.
Thanks 🙂
Middleware Magic Team


Copyright © 2010-2012 Middleware Magic. All rights reserved. |