Created
November 14, 2017 13:00
-
-
Save Romeh/efb5dc0b9b653ae442800c976b386535 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.ignite.*; | |
import org.apache.ignite.events.DiscoveryEvent; | |
import org.apache.ignite.events.EventType; | |
import javax.cache.expiry.CreatedExpiryPolicy; | |
import javax.cache.expiry.Duration; | |
public class NodeApp { | |
public static void main(String[] args) throws Exception { | |
// just for demo and test purpose , you should design more generic bootstrap logic to start your node | |
Ignite ignite = Ignition.start("config/igniteFailOver.xml"); | |
try { | |
IgniteCache<String, Job> cache = ignite.cache(CacheNames.ICEP_JOBS.name()); | |
// enable that ONLY for one node and after you start see the system outs , you can kill that node to see the fail over logic in the second node | |
// in production quality use logging instead of system out for sure, just i am sing it for demo limited needs | |
System.out.println("start of jobs creation"); | |
/* for (int i = 0; i <= 25; i++) { | |
String key = i + "Key"; | |
// start creating jobs by inserting them into the | |
cache.put(key | |
, Job.builder().nodeId(ignite.cluster().localNode().id().toString()). | |
request(Request.builder().requestID(key).modifiedTimestamp(System.currentTimeMillis()).build()). | |
build()); | |
}*/ | |
// listen globally for all nodes failed or removed events | |
ignite.events().localListen(event -> { | |
DiscoveryEvent discoveryEvent = (DiscoveryEvent) event; | |
System.out.println("Received Node event [evt=" + discoveryEvent.name() + | |
", nodeID=" + discoveryEvent.eventNode() + ']'); | |
ignite.compute().runAsync(() -> { | |
IgniteCache<String, String> nodes = ignite.cache(CacheNames.ICEP_NODES.name()); | |
String failedNodeId = discoveryEvent.eventNode().id().toString(); | |
// only one NODE will manage to insert successfully as it it is an atomic operation and thread safe | |
nodes.withExpiryPolicy(new CreatedExpiryPolicy(Duration.ONE_HOUR)).putIfAbsent(failedNodeId, failedNodeId); | |
}); | |
return true; | |
}, EventType.EVT_NODE_LEFT, EventType.EVT_NODE_FAILED); | |
} catch (Exception e) { | |
// just for test , do not do that in production code | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment