Grid Task Events Processing Examples.
Package:
org.gridgain.examples.events
These examples demonstrate usage of grid task events. Being deployed/executed task produces number of events which can be queried later to analyze GridEventsQueryExample.java. One can also set up events listener to process events synchronously GridEventsListenerExample.java.
There are three classes implemented for these examples:
Running Grid Node
These examples need not remote node to be started. They simply deploy and execute dummy task in the same grid.
GridEventsQueryExample.java
1. Import GridGain classes.
import org.gridgain.grid.*;
2. Add Grid Start and Stop.
GridFactory.start();
try {
...
}
finally {
GridFactory.stop(true);
}
finally clause allows for graceful grid shutdown in case of the exceptions.
3. Add Grid Task Execution.
grid.execute(GridEventsExampleTask.class, null).get();
Note that method grid.execute(...) returns GridTaskFuture ![]()
![]()
4. Query events.
for(GridEvent evt : grid.queryLocalEvents(evtFilter)) { System.out.println(">>> Found grid event: " + evt); }
All queried events are printed out.
|
Querying events requires mandatory event filter which should implement GridEventFilter |
Full Source Code
package org.gridgain.examples.events; import org.gridgain.grid.*; public final class GridEventsQueryExample { /** * Ensure singleton. */ private GridEventsQueryExample() { // No-op. } /** * Executes an example task on the grid in order to generate events and then lists * all events that have occurred from task execution. * * @param args Command line arguments, none required but if provided * first one should point to the Spring XML configuration file. See * <tt>"examples/config/"</tt> for configuration file examples. * @throws GridException If example execution failed. */ public static void main(String[] args) throws GridException { if (args.length == 0) { GridFactory.start(); } else { GridFactory.start(args[0]); } try { Grid grid = GridFactory.getGrid(); // Execute example task to generate events. grid.execute(GridEventsExampleTask.class, null).get(); // Define event filter. GridEventFilter evtFilter = new GridEventFilter() { public boolean accept(GridEvent evt) { return true; } }; // Retrieve and filter local node events. for(GridEvent evt : grid.queryLocalEvents(evtFilter)) { System.out.println(">>> Found grid event: " + evt); } System.out.println(">>>"); System.out.println(">>> Finished executing Grid Events Query Example."); System.out.println(">>> Check local node output."); System.out.println(">>>"); } finally { GridFactory.stop(true); } } }
GridEventsListenerExample.java
1. Import GridGain classes.
import org.gridgain.grid.*;
2. Add Grid Start and Stop.
GridFactory.start();
try {
...
}
finally {
GridFactory.stop(true);
}
finally clause allows for graceful grid shutdown in case of the exceptions.
3. Set up query events listener.
grid.addLocalEventListener(listener);
4. Add Grid Task Execution.
grid.execute(GridEventsExampleTask.class, null).get();
Note that method grid.execute(...) returns GridTaskFuture ![]()
![]()
Full Source Code
package org.gridgain.examples.events; import org.gridgain.grid.*; public final class GridEventsListenerExample { /** * Ensure singleton. */ private GridEventsListenerExample() { // No-op. } /** * Registers a listener to local grid events and prints all events that * occurred from a sample task execution. * * @param args Command line arguments, none required but if provided * first one should point to the Spring XML configuration file. See * <tt>"examples/config/"</tt> for configuration file examples. * @throws GridException If example execution failed. */ public static void main(String[] args) throws GridException { if (args.length == 0) { GridFactory.start(); } else { GridFactory.start(args[0]); } try { Grid grid = GridFactory.getGrid(); // Define event listener GridLocalEventListener listener = new GridLocalEventListener() { /** * {@inheritDoc} */ public void onEvent(GridEvent evt) { System.out.println(">>> Grid event occurred: " + evt); } }; // Register event listener for local events. grid.addLocalEventListener(listener); // Execute example task to generate events. grid.execute(GridEventsExampleTask.class, null).get(); // Remove local event listener. grid.removeLocalEventListener(listener); System.out.println(">>>"); System.out.println(">>> Finished executing Grid Event Listener Example."); System.out.println(">>> Check local node output."); System.out.println(">>>"); } finally { GridFactory.stop(true); } } }
GridEventsExampleTask.java
This is a grid task implementation that is responsible for split and aggregate (a.k.a map/reduce) logic. Note that this implementation uses GridTaskSplitAdapter ![]()
This task creates dummy job which does nothing.
Full Source Code
package org.gridgain.examples.events; import java.io.*; import java.util.*; import org.gridgain.grid.*; import org.gridgain.grid.logger.*; import org.gridgain.grid.resources.*; public class GridEventsExampleTask extends GridTaskAdapter<String, Object> { /** Grid logger. */ @GridLoggerResource private GridLogger log = null; /** Instance of grid. */ @GridInstanceResource private Grid grid = null; /** * {@inheritDoc} */ public Map<? extends GridJob, GridNode> map(List<GridNode> subgrid, String arg) throws GridException { GridJob job = new GridJobAdapter<Serializable>() { /** * {@inheritDoc} */ public Serializable execute() { if (log.isInfoEnabled() == true) { log.info(">>> Executing event example job on this node."); } // This job does not return any result. return null; } }; Map<GridJob, GridNode> jobs = new HashMap<GridJob, GridNode>(1); // Use only local node for this example. jobs.put(job, grid.getLocalNode()); return jobs; } /** * {@inheritDoc} */ public Object reduce(List<GridJobResult> results) throws GridException { // Nothing to reduce. return null; } }
