Grid Event Management Example
Every significant action in GridGain generates event. These events can be queried by:
- Looking through collection of events stored on local node by definining event filter
- Defining listener that is triggered at event occurence
This example demonstrates both aproaches.
(will be available with 1.6.2 release)
Package:
org.gridgain.examples.helloworld.gridify.failover
There are three classes implemented for this example:
Running Grid Node
This example will need one remote node to be running. Note that you don't need another machine for it - you can start remote node on the same machine you are running example on.
To start a remote node open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.
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. Define Event Listener.
Implementation of GridLocalEventListener ![]()
GridLocalEventListener listener = new GridLocalEventListener() { /** * {@inheritDoc} */ public void onEvent(GridEvent evt) { System.out.println(">>> Grid event occurred: " + evt); } };
4. Register listener on the grid.
grid.addLocalEventListener(listener);
5. Execute task on the grid.
grid.execute(GridEventsExampleTask.class, null).get();
6. Unregister listener from the grid.
grid.removeLocalEventListener(listener);
Full Source Code
package org.gridgain.examples.events; import org.gridgain.grid.*; public final class GridEventsQuerytExample { /** * Ensure singleton. */ private GridEventsQuerytExample() { // No-op. } /** * Executes an example task on the grid in order to generate events and then lists * all events that have occured from task execution. * * @param args Command line arguments, none required but if provided * first one should point to the Spring 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); } } }
GridEventsQuerytExample.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. Execute task on the grid.
Execute task to generate events on the grid.
grid.execute(GridEventsExampleTask.class, null).get();
4. Define Event Filter.
Implementation of GridEventFilter ![]()
GridEventFilter evtFilter = new GridEventFilter() { public boolean accept(GridEvent evt) { return true; } };
5. Retrieve all local events on the grid.
for(GridEvent evt : grid.queryLocalEvents(evtFilter)) { System.out.println(">>> Found grid event: " + evt); }
Full Source Code
package org.gridgain.examples.events; import org.gridgain.grid.*; public final class GridEventsQuerytExample { /** * Ensure singleton. */ private GridEventsQuerytExample() { // No-op. } /** * Executes an example task on the grid in order to generate events and then lists * all events that have occured from task execution. * * @param args Command line arguments, none required but if provided * first one should point to the Spring 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); } } }
GridEventsExampleTask.java
1. Import GridGain classes.
import org.gridgain.grid.*; import org.gridgain.grid.logger.*; import org.gridgain.grid.resources.*;
2. Split Logic.
This is a grid task implementation that is responsible map and reduce logic. Note that this task implements GridTask ![]()
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.*; /** * Example task used to generate events for event demonstration example. * */ public class GridEventsExampleTask extends GridTaskAdapter<String> { /** 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() { /* * Simply prints the job's argument. */ public Serializable execute() { if (log.isInfoEnabled() == true) { log.info(">>> Exectuting 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 Serializable reduce(List<GridJobResult> results) throws GridException { // Nothing to reduce. return null; } }
