Node initialization hooks

From JPPF Documentation

Jump to: navigation, search
Main Page > Customizing JPPF > Node initialization hooks

In the JPPF nodes, the lookup for a server to connect to relies essentially on each node's configuration. Thus, to implement a customized server lookup or failover mechanism, it is necessary to be able to modify the configuration, before the server lookup and connection is attempted. To this effect, JPPF provides a pluggable initialization hook which can be executed by the node before each connection attempt.

An initialization hook is a Java class that implements the interface InitializationHook, which is defined as follows:

 public interface InitializationHook {
    // Called each time the node is about to attempt to connect to a driver 
   void initializing(UnmodifiableTypedProperties initialConfiguration);
 }

Note that the initialConfiguration parameter reflects the exact same set of configuration properties that were loaded by the node at startup time. It is an instance of UnmodifiableTypedProperties, which is an extension of TypedProperties that does not permit the modification, insertion or removal of any property. To modify the node's configuration, you have to use JPPFConfiguration.getProperties(), which reflects the current configuration and can be modified.

Here is an example implementation:

 public class MyInitializationHook extends InitializationHook {
   // an alternate server address read from the configuration
   private String alternateServer = null;
   // determines which server address to use
   private boolean useAlternate = false;
 
   // This method toggles the JPPF server address between the value set in the
   // configuration file and an alternate server address
   public void initializing(UnmodifiableTypedProperties initialConfiguration) {
     // store the alternate server address
     if (alternateServer == null) {
       alternateServer = initialConfiguration.getString("alternate.server.host");
     }
     TypedProperties currentConfig = JPPFConfiguration.getProperties();
     // means the JPPF-configured value is to be used
     if (!useAlternate) {
       // reset the server address to its initially configured value
       String initialServer = initialConfiguration.getString("jppf.server.host");
       currentConfig.setProperty("jppf.server.host", initialServer);
       // toggle the server address to use for the next attempt
       useAlternate = true;
     } else {
       // connection to JPPF-configured server failed,
       // we will now try to connect to the alternate server
       currentConfig.setProperty("jppf.server.host", alternateServer);
       // toggle the server address to use for the next attempt
       useAlternate = false;
     }
   }
 }

Once the implementation is done, the initialization hook is plugged into JPPF using the service provider interface:

  • create a file in META-INF/services named org.jppf.node.initialization.InitializationHook
  • in this file, add the fully qualified class name of your implementation of the interface
  • copy the jar file or class folder containing your implementation and service file to the classpath of each node.

Related sample: Initialization Hook sample.

Main Page > Customizing JPPF > Node initialization hooks

Support This Project Powered by MediaWiki