1 Reply Latest reply on Aug 20, 2018 1:19 PM by pferraro

    Execute action in all nodes with jboss in domain mode

    pablin.aleman

      I'm running Jboss in domain mode and I have a startup singleton EJB that contain parameters, the parameters are readed from database and stored in a map in the EJB, aditionally in the EJB I have a scheduler that refresh the parameters every hour. Now I need that if some parameter is updated the new parameter should be updated in all nodes and don't wait almost one hour to refresh.

        • 1. Re: Execute action in all nodes with jboss in domain mode
          pferraro

          This should be trivial to implement using WF's clustering API.  Assuming you're using the "ha" or "full-ha" profile, the following should do the trick:

           

          public interface PropertyChangeListener {

             void propertyChanged(String name, Object value);

          }

           

          public class PropertyChangeCommand implements org.wildfly.clustering.dispatcher.Command<Void, PropertyChangeListener> {

              // N.B. Any properties of a command must be serializable

              private final String name;

              private final Object value;

           

              public PropertyChangeCommand(String name, Object value) {

                  this.name = name;

                  this.value = value;

              }

           

              @Override

              public Void execute(PropertyChangeListener listener) {

                  listener.propertyChanged(this.name, this.value);

                  return null;

              }

          }

           

          @Startup

          @Singleton

          public class MyBean implements PropertyChangeListener {

             @Resource(lookup = "java:jboss/clustering/dispatcher/default") // Inject a CommandDispatcherFactory using the default channel of the server

             private  org.wildfly.clustering.dispatcher.CommandDispatcherFactory factory;

             private  org.wildfly.clustering.dispatcher.CommandDispatcher<PropertyChangeListener> dispatcher;

           

             @PostConstruct

             public void init() {

                 this.dispatcher = this.factory.createCommandDispatcher(this.getClass().getName(), this);

             }

           

             @PreDestroy

             public void destroy() {

                 this.dispatcher.close();

             }

           

             public void update(String name, Object value) {

                 try {

                     Map<Node, CompletionStage<Void>> results = this.dispatcher.executeOnGroup(new PropertyChangeCommand(name, value));  // Execute on every member (including myself)

                     // Optionally wait for execution to complete on each node - make sure to ignore any CancellationExceptions

                 } catch (CommandDispatcherException e) {

                     // This means we failed to send the command to cluster, for whatever reason

                 }

             }

           

             @Override

             public void propertyChanged(String name, Object value) {

                // Update your state

             }

          }

           

          Any invocation of MyBean.update(...) will be propagated to the propertyChanged(...) method for all members of the cluster.

          For more details about the CommandDispatcher API, see the documentation here: wildfly/Clustering_API.adoc at master · wildfly/wildfly · GitHub