0 Replies Latest reply on Jun 15, 2016 2:37 PM by Sumanth Bandi

    invoking stateless bean method in wildfly instance from another wildfly instance

    Sumanth Bandi Newbie

      My application (.ear) is deployed in a 2-node cluster (two wildfly 9 instances). This application is a servlet invoking a stateless bean method on both nodes in a cluster. For example, servlet in nodeA has to invoke bean method on nodeA as well as on nodeB. All the required jars (jboss-client.jar etc.) are added to the classpath. Remoting is enabled on wildfly instances as well as an application user is added to support remote invocation. But bean invocation is always happening on the local node.


      Application packaging

      WildflyRemoteEJB.ear

      |---- jboss-as-ejb-remote-app.jar

      |         |---- CalculatorBean.class

      |         |---- RemoteCalculator.class

      |---- ejbclient.war

      |         |---- WEB-INF

      |                |---- web.xml

      |                |---- classes

      |                        |---- RemoteEJBClientServlet.class

      |---- META-INF    

              |---- application.xml


      Remote Interface

      public interface RemoteCalculator {

             int add(int a, int b);

      }

       

      Stateless Bean

      @Stateless

      @Remote(RemoteCalculator.class)

      public class CalculatorBean implements RemoteCalculator {

             final Logger log = Logger.getLogger(this.getClass().getName());

             @Override

             public int add(int a, int b) {

                  return a+b;

             }

      }

       

      @WebServlet("/RemoteEJBClient")

      public class RemoteEJBClient extends HttpServlet {

             private static final long serialVersionUID = 1L;

             final Logger log = Logger.getLogger(this.getClass().getName());

             public RemoteEJBClient() {

                   super();

             }

             protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                  invokeBean();

             }

             protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                  invokeBean();

             }

             private void invokeBean() {

                  RemoteCalculator statelessRemoteCalculator1;

                  RemoteCalculator statelessRemoteCalculator2;

                  try {

                       statelessRemoteCalculator1 = lookupRemoteStatelessCalculator1();

                       log.debug("Obtained a remote stateless calculator for invocation");

                       int a = 204; int b = 340;

                       log.debug("Adding " + a + " and " + b + " via the remote stateless calculator deployed on the server");

                       int sum = statelessRemoteCalculator1.add(a, b);

                       log.debug("Remote calculator returned sum = " + sum);

       

                       statelessRemoteCalculator2 = lookupRemoteStatelessCalculator2();

                       log.debug("Obtained a remote stateless calculator for invocation");

                       log.debug("Adding " + a + " and " + b + " via the remote stateless calculator deployed on the server");

                       sum = statelessRemoteCalculator2.add(a, b);

                       log.debug("Remote calculator returned sum = " + sum);

                  } catch (NamingException e) {

                       e.printStackTrace();

                  }

           }

           private RemoteCalculator lookupRemoteStatelessCalculator1() throws NamingException {

                   final Properties jndiProperties = new Properties();

                   jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

                   jndiProperties.put(Context.PROVIDER_URL,"http-remoting://localhost:8080");

                   jndiProperties.put(Context.SECURITY_PRINCIPAL, "ejbuser");

                   jndiProperties.put(Context.SECURITY_CREDENTIALS, "ejbuser");

                   final Context context = new InitialContext(jndiProperties);

                   log.debug(context.getEnvironment());

       

                   final String appName = "WildflyRemoteEJB";

                   final String moduleName = "jboss-as-ejb-remote-app";

                   final String beanName = CalculatorBean.class.getSimpleName();

                   final String viewClassName = RemoteCalculator.class.getName();

                   return (RemoteCalculator) context.lookup(appName+"/"+moduleName+"/"+beanName+"!"+viewClassName);

            }

           private RemoteCalculator lookupRemoteStatelessCalculator2() throws NamingException {

                   final Properties jndiProperties = new Properties();

                   jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

                   jndiProperties.put(Context.PROVIDER_URL,"http-remoting://localhost:8180");

                   jndiProperties.put(Context.SECURITY_PRINCIPAL, "ejbuser");

                   jndiProperties.put(Context.SECURITY_CREDENTIALS, "ejbuser");

                   final Context context = new InitialContext(jndiProperties);

                   log.debug(context.getEnvironment());

       

                   final String appName = "WildflyRemoteEJB";

                   final String moduleName = "jboss-as-ejb-remote-app";

                   final String beanName = CalculatorBean.class.getSimpleName();

                   final String viewClassName = RemoteCalculator.class.getName();

                   return (RemoteCalculator) context.lookup(appName+"/"+moduleName+"/"+beanName+"!"+viewClassName);

            }

      }

       

      I tried by giving remoting urls for 2 wildfly servers, but bean method is always executed from local server only. Can you please suggest?