GridGain™ 2.0.3
Java API Specification

org.gridgain.grid
Class GridJobAdapter<G extends Serializable>

java.lang.Object
  extended by org.gridgain.grid.GridJobAdapter<G>
Type Parameters:
G - Type of the optional job argument.
All Implemented Interfaces:
Serializable, GridJob
Direct Known Subclasses:
GridAffinityJobAdapter, GridCoherenceAffinityJobAdapter, GridifyJobAdapter

public abstract class GridJobAdapter<G extends Serializable>
extends Object
implements GridJob

Convenience adapter for GridJob implementations. It provides the following functionality:

Here is an example of how GridJobAdapter can be used from task logic to create jobs. The example creates job adapter as anonymous class, but you are free to create a separate class for it. Note that all state passed within GridJobAdapter must be Serializable.
 public class TestGridTask extends GridTaskSplitAdapter<String, Integer> {
     // Used to imitate some logic for the
     // sake of this example
     private int multiplier = 3;

     @Override
     protected Collection<? extends GridJob> split(int gridSize, final String arg) throws GridException {
         List<GridJobAdapter<String>> jobs = new ArrayList<GridJobAdapter<String>>(gridSize);

         for (int i = 0; i < gridSize; i++) {
             jobs.add(new GridJobAdapter<String>() {
                 // Job execution logic.
                 public Serializable execute() throws GridException {
                     return multiplier * arg.length();
                 }
             });
        }

         return jobs;
     }

     // Aggregate multiple job results into
     // one task result.
     public Integer reduce(List<GridJobResult> results) throws GridException {
         int sum = 0;

         // For the sake of this example, let's sum all results.
         for (GridJobResult res : results) {
             sum += (Integer)res.getData();
         }

         return sum;
     }
 }
 




See Also:

  Documentation
  Email Support
  Online Forums
  Issue Tracking

Author:   2005-2008 Copyright © GridGain Systems. All Rights Reserved. ver. 2.0.3

See Also:
Serialized Form
 

Constructor Summary
protected GridJobAdapter()
          No-arg constructor.
protected GridJobAdapter(G... args)
          Creates job with specified arguments.
 
Method Summary
 void addArgument(G arg)
          Adds an optional job argument.
 void cancel()
          This method is called when when system detects that completion of this job can no longer alter the overall outcome (for example, when parent task has already reduced the results).
 List<G> getAllArguments()
          Gets ordered list of all job arguments set so far.
 G getArgument()
          Gets job argument at position 0 or null if no argument was previously set.
 G getArgument(int pos)
          Gets argument at specified position.
protected  boolean isCancelled()
          This method tests whether or not this job was cancelled.
 void setArgument(G arg)
          Sets an optional job argument at position 0.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.gridgain.grid.GridJob
execute
 

Constructor Detail

GridJobAdapter

protected GridJobAdapter()
No-arg constructor.

Note: the job argument will be null which usually is not the intended behavior. You can use GridJobAdapter.setArgument(Serializable) to set job argument.


GridJobAdapter

protected GridJobAdapter(G... args)
Creates job with specified arguments.

Parameters:
args - Job arguments.
Method Detail

cancel

public void cancel()
This method is called when when system detects that completion of this job can no longer alter the overall outcome (for example, when parent task has already reduced the results). Job is also cancelled when GridTaskFuture.cancel() is called.

Note that job cancellation is only a hint, and just like with Thread.interrupt() method, it is really upto the actual job instance to gracefully finish execution and exit.

Specified by:
cancel in interface GridJob

isCancelled

protected final boolean isCancelled()
This method tests whether or not this job was cancelled. This method is thread-safe and can be called without extra synchronization.

This method can be periodically called in GridJob.execute() method implementation to check whether or not this job cancelled. Note that system calls GridJobAdapter.cancel() method only as a hint and this is a responsibility of the implementation of the job to properly cancel its execution.

Returns:
true if this job was cancelled, false otherwise.

setArgument

public void setArgument(G arg)
Sets an optional job argument at position 0.

Parameters:
arg - Executable argument.

addArgument

public void addArgument(G arg)
Adds an optional job argument.

Parameters:
arg - Job argument.

getArgument

public G getArgument()
Gets job argument at position 0 or null if no argument was previously set.

Returns:
Job argument.

getArgument

public G getArgument(int pos)
Gets argument at specified position.

Parameters:
pos - Position of the argument.
Returns:
Argument at specified position.

getAllArguments

public List<G> getAllArguments()
Gets ordered list of all job arguments set so far. Note that the same list as used internally is returned, so modifications to it will affect the state of this job adapter instance.

Returns:
Ordered list of all job arguments.

GridGain™ 2.0.3
Java API Specification

GridGain™ - Grid Computing Made Simple, ver. 2.0.3.20052008
2005-2008 Copyright © GridGain Systems. All Rights Reserved.