以下のコードを確認してください。GridGain 6.1.8 を使用してテストしました。
import org.gridgain.grid.*;
import org.gridgain.grid.cache.GridCache;
import org.gridgain.grid.cache.GridCacheConfiguration;
import org.gridgain.grid.cache.GridCacheMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class GridGainMain {
public static void main(String[] args) throws GridException {
GridConfiguration config = new GridConfiguration();
// Give a name to your grid.
config.setGridName("MyCoolGrid");
// Configure the cache that will be used by the leader election algorithm.
GridCacheConfiguration leaderConf = new GridCacheConfiguration();
leaderConf.setName("leader");
leaderConf.setCacheMode(GridCacheMode.REPLICATED);
config.setCacheConfiguration(leaderConf);
// Start the grid!
try (Grid grid = GridGain.start(config)) {
// Get the local node.
final GridNode localNode = grid.localNode();
// Get the leader cache.
final GridCache<String, String> leaderCache = grid.cache("leader");
// Elect this member as the leader, if no other node was elected yet.
putIfAbsent("leader", localNode.id().toString(), leaderCache);
// ================================================
// Schedule the leader election algorithm.
// The leader election algorithm will elect the oldest grid node as the leader.
// ================================================
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Get the self ID.
final String selfId = localNode.id().toString();
// Get the cached leader ID.
final String cachedLeaderId = get("leader", leaderCache);
// Get all nodes.
List<GridNode> list = new ArrayList<>(grid.nodes());
// Sort all nodes by natural order.
list.sort((o1, o2) -> (int) (o1.order() - o2.order()));
// Get the ID of the oldest node, which is the leader ID.
final String leaderId = list.get(0).id().toString();
// If the leader ID is not equals to the cached leader ID,
if (!leaderId.equals(cachedLeaderId)) {
// Put the leader ID into cache.
put("leader", leaderId, leaderCache);
}
// If this node is the leader,
if (selfId.equals(leaderId)) {
// =====================================
// Do something! Only this grid node will execute this code.
// =====================================
}
System.out.println("### Self ID: " + selfId
+ ", Order: " + localNode.order()
+ ", Leader ID: " + leaderId);
}
},
// Schedule now.
0L,
// Run the algorithm once every five seconds.
TimeUnit.SECONDS.toMillis(5));
// Remove this in production.
sleep(1, TimeUnit.DAYS);
}
}
private static <T> T get(String key, GridCache<String, T> cache) {
try {
return cache.get(key);
} catch (GridException e) {
return null;
}
}
private static <T> T putIfAbsent(String key, T value, GridCache<String, T> cache) {
try {
return cache.putIfAbsent(key, value);
} catch (GridException e) {
return null;
}
}
private static <T> T put(String key, T value, GridCache<String, T> cache) {
try {
return cache.put(key, value);
} catch (GridException e) {
return null;
}
}
public static void sleep(long duration, TimeUnit unit) {
try {
unit.sleep(duration);
} catch (InterruptedException e) {
// Ignore.
}
}
}