14 Replies Latest reply on Oct 27, 2016 3:37 PM by Paul Ferraro

    Wildfly 10.1.0 Singleton doesn't work

    Bruno Fraga Newbie

      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:

      package br.com.etaure.singleton;

       

      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!"));

              }

          }

         

       

      }

       

      package br.com.etaure.singleton;
      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)@Singletonpublic 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!"));        }    }    }