Reputation: 1
I've two Wildfly server configured in a domain, and I need to make a singleton that runs with HA. I need it to run only in one server, and if that server fails it should be started in the slave server. I'm using the defult configuration and I only created the “/META-INF/singleton-deployment.xml” in my WAR. When I deploy the WAR it starts in both servers! Not only in one. What is missing? Do I need to edit something in domain.xml?
My singleton only writes a text to the log file and console, just for testing:
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!"));
}
}
}
Upvotes: 0
Views: 1075
Reputation: 3164
Was answered in https://stackoverflow.com/a/27956003/653069
As per EJB 3.1 spec, the @Singleton
is only per-JVM and not per-cluster.
In cases where the container is distributed over many virtual machines, each application will have one bean instance of the Singleton for each JVM.
Nevertheless, you can use JBoss specific way. Take a look into the cluster-ha-singleton quickstart - EAP 7.0 one, which should work on WildFly 10.x too.
Upvotes: 2