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

    Wildfly 10.1.0 Singleton doesn't work

    bfraga

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

        • 1. Re: Wildfly 10.1.0 Singleton doesn't work
          pferraro

          You need to configure your server-group in domain.xml to use either the "ha" or "full-ha" profile.

          • 2. Re: Wildfly 10.1.0 Singleton doesn't work
            bfraga


            The other-server-group it's already in full-ha.

            screenshot.191.png

            • 3. Re: Wildfly 10.1.0 Singleton doesn't work
              pferraro

              Are the servers on which your application is deployed associated with this "other-server-group"?

              • 4. Re: Wildfly 10.1.0 Singleton doesn't work
                bfraga

                Yes. My test app is deployed in the other-server-group.

                • 5. Re: Wildfly 10.1.0 Singleton doesn't work
                  pferraro

                  Can you attach your server logs?  If everything is configured correctly, you should see messages about cluster formation and the results of the singleton election.

                  • 6. Re: Wildfly 10.1.0 Singleton doesn't work
                    bfraga

                    I'm sending the server-three logs from master and slave.

                    It seems that both servers are elected to be the singleton provider.

                     

                    Master:

                    2016-10-25 08:20:11,469 INFO 

                    (notification-thread--p1-t1) WFLYCLSV0003: master:server-three elected as

                    the singleton provider of the

                    jboss.deployment.unit."TesteSingleton.war".FIRST_MODULE_USE service

                    2016-10-25 08:20:11,469 INFO 

                    (notification-thread--p1-t1) WFLYCLSV0001: This node will now operate as

                    the singleton provider of the

                    jboss.deployment.unit."TesteSingleton.war".FIRST_MODULE_USE service

                     

                    Slave:

                    2016-10-25 08:20:11,938 INFO 

                    (notification-thread--p1-t1) WFLYCLSV0003: slave:server-three elected as

                    the singleton provider of the

                    jboss.deployment.unit."TesteSingleton.war".FIRST_MODULE_USE service

                    2016-10-25 08:20:11,949 INFO 

                    (notification-thread--p1-t1) WFLYCLSV0001: This node will now operate as

                    the singleton provider of the

                    jboss.deployment.unit."TesteSingleton.war".FIRST_MODULE_USE service

                     

                    And then my app starts on both servers.

                    Master:

                    2016-10-25 08:20:14,012 INFO  (EJB default - 2) INFO  | 2016-10-25

                    08:20:14 | singleton.TesteAPP (TesteAPP.java:28) - Tarefa

                    executada com sucesso! Nome da máquina: RED-EST-DEV02 Endereço da máquina:

                    172.20.205.12

                     

                    Slave:

                    2016-10-25 08:20:15,002 INFO  (EJB default - 2) INFO  | 2016-10-25

                    08:20:15 | singleton.TesteAPP (TesteAPP.java:28) - Tarefa

                    executada com sucesso! Nome da máquina: RED-EST-DEV01 Endereço da máquina:

                    172.20.232.111

                    • 7. Re: Wildfly 10.1.0 Singleton doesn't work
                      pferraro

                      Are your servers really running on the same host?  According to your logs, your JGroups cluster is configured to use the loopback interface (i.e. 127.0.0.1), which means, unless your servers are all running on the same host, they won't form a cluster, and each server will operate as a cluster of 1.  That explains why each node elects itself as the primary provider of your singleton deployment.

                      Try specifying the "jboss.bind.address.private" system property to override the network interface used by JGroups for cluster formation.

                      • 8. Re: Wildfly 10.1.0 Singleton doesn't work
                        bfraga

                        My servers are running on diferent hosts! Master is 172.20.205.12, and

                        slave is  172.20.232.111. And httpd + mod_cluster is running on master.

                        Where and how do I configure JGroups?

                        • 9. Re: Wildfly 10.1.0 Singleton doesn't work
                          pferraro

                          As I mentioned earlier, you need to override the default value of the "private" interface.  You can do this per host, via host.xml.

                          Interfaces and ports - WildFly 10 - Project Documentation Editor

                          1 of 1 people found this helpful
                          • 10. Re: Wildfly 10.1.0 Singleton doesn't work
                            bfraga

                            It worked.

                            I just included the private interface in the interfaces section of host.xml

                            in each server.

                             

                             

                            • 11. Re: Wildfly 10.1.0 Singleton doesn't work
                              pferraro

                              Great! Please mark this thread as answered.

                              • 12. Re: Wildfly 10.1.0 Singleton doesn't work
                                bfraga

                                Just posting the modification in host.xml:

                                 

                                <interfaces>

                                        <interface name="management">

                                            <inet-address value="${jboss.bind.address.management:172.20.216.30}"/>

                                        </interface>

                                        <interface name="public">

                                            <inet-address value="${jboss.bind.address:172.20.216.30}"/>

                                        </interface>

                                <interface name="unsecured">      

                                <inet-address value="172.20.216.30"/>   

                                </interface>

                                        <interface name="private">

                                            <inet-address value="${jboss.bind.address.private:172.20.216.30}"/>

                                        </interface>

                                    </interfaces>

                                • 13. Re: Wildfly 10.1.0 Singleton doesn't work
                                  bfraga

                                  Two more questions:

                                  - All singletons are going to be load balanced across all servers? When this happens? Every time or when I deploy the application?

                                  - It's possible to move a singleton from one server to another manually, or it only moves when the server stop or fail?

                                  • 14. Re: Wildfly 10.1.0 Singleton doesn't work
                                    pferraro

                                    - All singletons are going to be load balanced across all servers? When this happens? Every time or when I deploy the application?

                                    I'm not sure I understand your question.  If your singleton deployment is a WAR, using a load balancer like mod_cluster will only direct requests to nodes on which the application is fully deployed (i.e. the primary singleton provider).

                                    - It's possible to move a singleton from one server to another manually, or it only moves when the server stop or fail?

                                    The selection of the target node can be manipulated by modifying election preferences.

                                    e.g.

                                    /subsystem=singleton/singleton-policy=default/election-policy=simple:write-attribute(name=name-preferences[node1])
                                    

                                     

                                    If node1 was not already the primary singleton provider of the deployment, this will restart any services/deployments using this policy, and force the election of node1.