Invoking remote ejb in a 2-node wildfly cluster
Asked Answered
M

1

8

I am trying to invoke remote ejb on each node of a cluster with nodes node1 & node2, but i am always getting node1. Deployed EJB & client code as EAR file in both nodes. Application is running on Wildfly 9 ApplicationServer. Invoked client code from node1.

EJB code:

@Remote
public interface SLSBRemote {
    public void test();
}

@Stateless(mappedName="SLSBEJB")
public class SLSBEJB implements SLSBRemote {
    @Override
    public void test() 
    {
       try {
          String nodeName = System.getProperty("jboss.node.name");
          if(nodeName == null) {
             nodeName = InetAddress.getLocalHost().getHostName();
          }
          log.debug("nodename: "+nodeName);
       } catch (Exception e) {
          log.error("Error",e);
       }
    }
}

client code:

public class testEjb
{
    //invoking this method from other class. Able to pass node address
    public void testBean(List<String> nodes)
    {
       Properties properties = new Properties();
       properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
       Context context;
       for (String node : nodes) {
           properties.put(Context.PROVIDER_URL,"http-remoting://" + node);
           try {
             context = new InitialContext(properties);                  
             SLSBRemote slsbRemote=(SLSBRemote)context.lookup(getLookupName());
             log.debug("node:"+node+"slsbejbobject:"+slsbRemote.hashCode());
             slsbRemote.test();
           } catch (NamingException e) {
             log.error("Error ", e);
           }
       }
   }
}

In the logs,

node: "node1", binddbejb object: 1276422461
node: "node2", binddbejb object: 1276422461
nodename: "node1_address"
nodename: "node1_address" (instead of node2_address)

Please suggest

Malynda answered 28/1, 2016 at 18:34 Comment(4)
Probably your cluster only has one JNDI service.Nirvana
Gabriel, i didn't understand. How can i make sure each node in the cluster has separate JNDI service?Malynda
can you check your list nodes and make sure they are different at create time before executing the testBean(list<?>).Potvaliant
What part of the log is from clients and nodes? The code says: log.debug("node:"+node+"slsbejbobject:"+slsbRemote.hashCode()); the log part of your post says: node: "node1", binddbejb object: 1276422461 It seems as they are different versions.Xanthophyll
X
2

In order to use a clustered EJB wild fly needs to be configured for clustering and as far as I have found:

  1. Wildfly provides clustering of stateful EJBs.
  2. Wild fly documentation provides an example of a failover scenario for clustering. (Client tries to contact ejb on server #1 , if it is unavailable then client contacts the ejb on server #2.)
  3. Clustered Ejbs need to be configured as needed and annotated properly.
import org.jboss.ejb3.annotation.Clustered;
import javax.ejb.Stateful;

@Stateful
@Clustered
public class MyStatefulBean {
...
}

Examples are given from this page of the documentation, which describes what has to be done in detail. https://docs.jboss.org/author/display/WFLY8/EJB+Services

If you apply this configuration EJBs from all nodes of the cluster could serve the client.

However do note that a client should normally be completely unaware of the clustering. A clients needs to call the ejb and it should be up to the cluster to decide which instance serves the client.

Xanthophyll answered 2/2, 2016 at 13:22 Comment(2)
This can confuse the people: "Clustering EJBs is provided for statefull ejbs only.". A cluster works in Wildfly for stateless EJBs providing failover and load-balance.Lundell
I updated the text. You are right, I have improved the wording thanks to your observation.Xanthophyll

© 2022 - 2024 — McMap. All rights reserved.