/** * Runs basic cache example. * * @param args Command line arguments, none required. * @throws Exception If example execution failed. */ public static void main(String[] args) throws Exception { final Grid g = args.length == 0 ? G.start("examples/config/spring-cache.xml") : G.start(args[0]); try { // Subscribe to events on every node, so we can visualize what's // happening in remote caches. g.run( BROADCAST, new CA() { @Override public void apply() { GridLocalEventListener lsnr = new GridLocalEventListener() { @Override public void onEvent(GridEvent event) { switch (event.type()) { case EVT_CACHE_OBJECT_PUT: case EVT_CACHE_OBJECT_READ: case EVT_CACHE_OBJECT_REMOVED: { GridCacheEvent e = (GridCacheEvent) event; X.println("Cache event [name=" + e.name() + ", key=" + e.key() + ']'); } } } }; GridNodeLocal<String, GridLocalEventListener> loc = g.nodeLocal(); GridLocalEventListener prev = loc.remove("lsnr"); // If there is a listener subscribed from previous runs, unsubscribe it. if (prev != null) g.removeLocalEventListener(prev); // Record new listener, so we can check it on next run. loc.put("lsnr", lsnr); // Subscribe listener. g.addLocalEventListener(lsnr, EVTS_CACHE); } }); final GridCacheProjection<Integer, String> cache = g.cache(CACHE_NAME).projection(Integer.class, String.class); final int keyCnt = 20; // Store keys in cache. for (int i = 0; i < keyCnt; i++) cache.putx(i, Integer.toString(i)); // Peek and get on local node. for (int i = 0; i < keyCnt; i++) { X.println("Peeked [key=" + i + ", val=" + cache.peek(i) + ']'); X.println("Got [key=" + i + ", val=" + cache.get(i) + ']'); } // Projection (view) for remote nodes. GridProjection rmts = g.remoteProjection(); if (!rmts.isEmpty()) { // Peek and get on remote nodes (comment it out if output gets too crowded). g.remoteProjection() .run( BROADCAST, new GridAbsClosureX() { @Override public void applyx() throws GridException { for (int i = 0; i < keyCnt; i++) { X.println("Peeked [key=" + i + ", val=" + cache.peek(i) + ']'); X.println("Got [key=" + i + ", val=" + cache.get(i) + ']'); } } }); } } finally { G.stop(true); } }