gpt4 book ai didi

java - Wildfly 10.1.0 单例不起作用

转载 作者:行者123 更新时间:2023-12-02 03:16:24 24 4
gpt4 key购买 nike

我在一个域中配置了两个 Wildfly 服务器,并且我需要创建一个使用 HA 运行的单例。我需要它只在一台服务器上运行,如果该服务器发生故障,它应该在从属服务器中启动。我使用默认配置,并且只在 WAR 中创建了“/META-INF/singleton-deployment.xml”。当我部署 WAR 时,它会在两台服务器中启动!不仅在一处。缺什么?我需要在domain.xml 中编辑某些内容吗?

我的单例仅将文本写入日志文件和控制台,仅用于测试:

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Singleton
public class TesteAPP {

final Logger logger = LogManager.getLogger(TesteAPP.class);

@Schedule(second = "*/1", minute = "*", hour = "*", persistent = false)
public void executaTarefa() {
try {
logger.log(Level.INFO, "Tarefa executada com sucesso! Nome da máquina: {} Endereço da máquina: {}",
InetAddress.getLocalHost().getHostName(),
InetAddress.getLocalHost().getHostAddress());
System.out.println(String.format("Tarefa executada com sucesso! Nome da máquina: %s Endereço da máquina: %s",
InetAddress.getLocalHost().getHostName(),
InetAddress.getLocalHost().getHostAddress()));
} catch (UnknownHostException e) {
logger.log(Level.ALL, "Tarefa executada com sucesso!");
System.out.println(String.format("Tarefa executada com sucesso!"));
}
}


}

最佳答案

已在 https://stackoverflow.com/a/27956003/653069 中得到答复

根据 EJB 3.1 规范,@Singleton 仅适用于每个 JVM,而不适用于每个集群。

In cases where the container is distributed over many virtual machines, each application will have one bean instance of the Singleton for each JVM.

尽管如此,您可以使用 JBoss 特定的方式。看看cluster-ha-singleton quickstart - EAP 7.0 之一,也应该适用于 WildFly 10.x。

关于java - Wildfly 10.1.0 单例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com