This sample demonstrates the use of a node initialization hook to implement a failover mechanism for the connection to the server. The hook reads a list of JPPF server addresses from the node configuration, and uses these addresses in the configured order.
The failover is performed as follows:
- At node startup time, read the list of servers and store it in memory
- Use the first server in the list and attempt to connect
- When the connection fails, the recovery mechanism will attempt to reconnect to the current server
- If the recovery fails, the current server is put at the end of the list and we get back to step 2
For information on how to set up a node and server, please refer to the JPPF documentation.
For convenience, this sample provides 2 configurations for the servers, which you will find in InitializationHook/config/driver1 and InitializationHook/config/driver2.
Additionally, a node configuration is provided in InitializationHook/config/node.
Once you have installed the 2 servers and at least one node, perform the following steps:
- Open a command prompt in JPPF-x.y-samples-pack/InitializationHook
- Build the sample: type "ant jar"; this will create a file named InitializationHook.jar
- Copy InitializationHook.jar in the "lib" folder of the JPPF node installation, to add it to the node's classpath.
- Replace the node's configuration with the one provided in this sample
- Replace the servers configurations with those provided in this sample
- Start the two drivers
- Start the node. Upon startup you should see the following messages in the node's console:
*** found 3 servers *** registered server localhost:11111 registered server localhost:11121 registered server localhost:11131
- Kill the first driver (the one listening to port 11111), the node console will display the following:
Attempting connection to the node server at localhost:11111 SocketInitializer.initializeSocket(): Could not reconnect to the remote server Attempting connection to the class server at localhost:11121 Reconnected to the class server JPPF Node management initialized Attempting connection to the node server at localhost:11121 Reconnected to the node server Node successfully initialized
The first two lines show the node trying to reconnect to the same driver, and failing to do so. This is the built-in connection recovery mechanism. The next lines show the node connecting to the second driver (port 11121), this is our failover mechanism taking place. - Now, start the first driver again, and kill the second driver (port 11121), and you should see the following:
Attempting connection to the node server at localhost:11121 SocketInitializer.initializeSocket(): Could not reconnect to the remote server Attempting connection to the class server at localhost:11131 Attempting connection to the class server at localhost:11111 Reconnected to the class server JPPF Node management initialized Attempting connection to the node server at localhost:11111 Reconnected to the node server Node successfully initialized
In the same way as previously, we first see the node attempting to recover the broken connection. Then our failover mechanism attempts to connect to a driver listening to port 11131. Since we never started this driver, the connection attempt fails. The last lines show the node connecting to the first driver again. The failover mechanism browses the configured servers as if they were in a ring.
- DiscoveryHook.java : this is the implementation of our connection failover mechanism, via a node initialization hook.
- driver1/jppf-driver.properties : the configuration of the first driver
- driver2/jppf-driver.properties : the configuration of the second driver
- node/jppf-node.properties : the configuration of the node
There are 2 privileged places you can go to: