1 Reply Latest reply on Oct 26, 2015 5:03 AM by jaysensharma

    EJB Remote Client - reconnect - how to



      Given is a Stateless Session Bean running on server.

      A client connects remotely.


      Properties jndiProps = new Properties();
              jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
              jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8180");
              // username
              jndiProps.put(Context.SECURITY_PRINCIPAL, "calc");
              // password
              jndiProps.put(Context.SECURITY_CREDENTIALS, "secret");
              // This is an important property to set if you want to do EJB
              // invocations via the remote-naming project
              jndiProps.put("jboss.naming.client.ejb.context", true);
              ctx = new InitialContext(jndiProps);
              // lookup the bean CalcService
              beanRemoteInterface = (ICalculator) ctx


      using this bean (beanRemoteInterface) in a loop to calculate some numbers.

      for (int i = 0; i <= 300; i++) {
          int x = beanRemoteInterface.sumRemote(10, i);


      Now if the server restarts (redeployment or some network issue, or something elsehow to or is down for some seconds, i will get errors that the remote side is not responding. OK

      But if the server comes up again the error is still there.

      I need somehow to reconnect or lookup the remotebean again. Is there some already built in feature for that or does anyone have a code snippet showing me how this could be fixed.


      Thank you


        • 1. Re: EJB Remote Client - reconnect - how to

          Hello Niki,


             You are using remoting based approach to lookup EJBs which will not work in case of reconnect You should rather try using the EJB client API approach.   Please try the following file in your EJB Client jar










          package client;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import java.util.Properties;
          import ejb.one.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) {
                    CallerRemote remote=(CallerRemote)context.lookup("ejb:TestEAR/remoteEjbOne/CallerBean!ejb.one.CallerRemote");
                    for (int i = 0; i <= 300; i++) {
                       try {
                             System.out.println("\n\t remote.testMethod(\"MiddlewareMagic!!!\") = "+remote.testMethod("MiddlewareMagic!!!"));
                        } catch(Exception e) {                  
                              System.out.println("\n\tEXCEPTION: " + e.getMessage());
                              try{ Thread.sleep(500); } catch(InterruptedException ee) {  ee.printStackTrace(); }



          It (client) should automatically reconnect to the WildFly instance as soon as the WildFly becomes available.


          NOTE:   Restrictions for EJB's

          If the remote-naming is used there are some restrictions as there is no full support of the ejb-client features.

          • No loadbalancing, if the URL conatains multiple "remote://" servers there is no loadbalancing, 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.
          • 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
          • No client side interceptor. The EJBContext.getCurrent() can not be used and it is not possible to add a client interceptor
          • No UserTransaction support
          • All proxies become invalid if .close() for the related Initalcontext is invoked, or the InitialContext is not longer referenced and gets garbage-collected. In this case the underlying EJBContext is destroyed and the conections are closed.
          • It is not possible to use remote-naming if the client is an application deployed on another JBoss instance