java - 在 2 节点 wildfly 集群中调用远程 ejb

标签 java jakarta-ee remote-access ejb-3.1 wildfly-9

我试图在具有节点 node1 和 node2 的集群的每个节点上调用远程 ejb,但我总是得到 node1。在两个节点中将 EJB 和客户端代码部署为 EAR 文件。应用程序正在 Wildfly 9 ApplicationServer 上运行。从 node1 调用客户端代码。

EJB 代码:

@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);
       }
    }
}

客户端代码:

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);
           }
       }
   }
}

在日志中,

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

请推荐

最佳答案

为了使用集群 EJB,wild fly 需要配置集群,据我所知:

  1. Wildfly 提供有状态 EJB 的集群。
  2. Wild fly 文档提供了集群故障转移场景的示例。 (客户端尝试联系服务器 #1 上的 ejb,如果不可用,则客户端联系服务器 #2 上的 ejb。)
  3. 集群 Ejb 需要根据需要进行配置并正确注释。
import org.jboss.ejb3.annotation.Clustered;
import javax.ejb.Stateful;

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

文档的这一页给出了示例,其中详细描述了必须完成的操作。 https://docs.jboss.org/author/display/WFLY8/EJB+Services

如果您应用此配置,则来自集群所有节点的 EJB 都可以为客户端提供服务。

但是请注意,客户端通常应该完全不知道集群。客户端需要调用 ejb,这应该由集群来决定哪个实例为客户端服务。

关于java - 在 2 节点 wildfly 集群中调用远程 ejb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069328/

相关文章:

java - 我如何才能将 ArrayList<String> 插入到接受 List<Integer> 的构造函数中?

java - 在 Java 中填充 boolean 数组

java - Sun Application Server 中的 EJB 依赖项版本冲突

.net - 想要远程访问 sharepoint 以使用 sharepoint api

postgresql - Heroku - PostgreSQL - 如何在 Windows 上远程连接

java - XMLUnit比较忽略元素的顺序

java - 如何重构方法并添加带有 On 和 Off 值的枚举

java - 你如何使@Resource 成为可选的?

eclipse - 如何在 Java EE @ServerEndpoint 中获取 session.getRequestURI() 的端口

Java psexec 交互式远程命令行