Skip to content

Instantly share code, notes, and snippets.

@jackeylu
Last active July 1, 2016 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackeylu/f6f30d7b3240ecd545068cee18c0f639 to your computer and use it in GitHub Desktop.
Save jackeylu/f6f30d7b3240ecd545068cee18c0f639 to your computer and use it in GitHub Desktop.
An Ignite client doing cache put with event listener.
public class IgniteClientCacheEventPut {
public static void main(String[] args) {
Ignition.setClientMode(true);
try (Ignite ignite = Ignition.start(args[0])){
String cache_name = "ignite-cache";
IgniteCache cache = ignite.getOrCreateCache(cache_name);
UUID uuid = addEventListener(ignite, cache_name);
new CacheOperations().putOperations(ignite, cache_name, Integer.valueOf(args[1]));
removeEventListener(ignite, uuid);
System.out.println("Cache size: "+cache.size());
ignite.destroyCache(cache_name);
}
}
private static UUID addEventListener(Ignite ignite, final String cache_name) {
IgniteBiPredicate<UUID, CacheEvent> localLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent event) {
return true;
}
};
IgnitePredicate<CacheEvent> rmtFilter = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent event) {
return event.cacheName().equals(cache_name);
}
};
UUID uuid = ignite.events(ignite.cluster().forServers()).remoteListen(localLsnr, rmtFilter,
EventType.EVT_CACHE_OBJECT_PUT, EventType.EVT_CACHE_OBJECT_REMOVED);
return uuid;
}
private static void removeEventListener(Ignite ignite, UUID uuid) {
System.out.println("Stopping remote listeners with uuid " + uuid);
ignite.events().stopRemoteListen(uuid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment