我在域中配置了两个Wildfly服务器,我需要创建一个与HA一起运行的单例。我需要它只在一台服务器上运行,如果该服务器出现故障,它应该在从服务器中启动。 我正在使用defult配置,我只在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!"));
}
}
}
答案 0 :(得分:2)
在https://stackoverflow.com/a/27956003/653069
中得到了解答根据EJB 3.1规范,@Singleton
只是每个JVM而不是每个群集。
如果容器分布在许多虚拟机上,则每个应用程序将为每个JVM配备一个Singleton的bean实例。
尽管如此,您可以使用JBoss特定的方式。看一下cluster-ha-singleton quickstart - EAP 7.0,它也适用于WildFly 10.x.