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

    EJB Remote Client - reconnect - how to

    Niki Walz Newbie

      Hi

      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
                      .lookup("thecore/CalcService!com.nikirocks.calculatorcore.api.ICalculator");
      

       

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

      beanRemoteInterface.init();
      for (int i = 0; i <= 300; i++) {
          int x = beanRemoteInterface.sumRemote(10, i);
          System.out.println(x);
          Thread.sleep(500);
      }
      beanRemoteInterface.destroy();
      

       

      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

      Niki

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

          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

           

           

          jboss-ejb-client.properties

          ====================

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

           

           

          Client.java

          ========

          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) {
                          e.printStackTrace();
                    }
          
          
                    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!!!"));
                             Thread.sleep(500);
                        } catch(Exception e) {                  
                              System.out.println("\n\tEXCEPTION: " + e.getMessage());
                              e.printStackTrace();
                              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

           

          Labels: