public interface GridCompute
GridProjection. Instance of GridCompute is obtained from grid projection
as follows:
GridCompute c = GridGain.grid().compute();The methods are grouped as follows:
apply(...) methods execute GridClosure jobs over nodes in the projection.call(...) methods execute Callable jobs over nodes in the projection.
Use GridCallable for better performance as it implements Serializable.
run(...) methods execute Runnable jobs over nodes in the projection.
Use GridRunnable for better performance as it implements Serializable.
broadcast(...) methods broadcast jobs to all nodes in the projection.affinity(...) methods colocate jobs with nodes on which a specified key is cached.GridEmptyProjectionException will be thrown out of result future.
Runnable and Callable implementations must support serialization as required
by the configured marshaller. For example, GridOptimizedMarshaller requires Serializable
objects by default, but can be configured not to. Generally speaking objects that implement Serializable
or Externalizable will perform better. For Runnable and Callable interfaces
GridGain provides analogous GridRunnable and GridCallable classes which are
Serializable and should be used to run computations on the grid.
broadcast(...), GridGain must select a node for a computation
to be executed. The node will be selected based on the underlying GridLoadBalancingSpi,
which by default sequentially picks next available node from grid projection. Other load balancing
policies, such as random or adaptive, can be configured as well by selecting
a different load balancing SPI in grid configuration. If your logic requires some custom
load balancing behavior, consider implementing GridComputeTask directly.
GridFailoverSpi in grid configuration.
GridTaskSessionResourceGridInstanceResourceGridLoggerResourceGridHomeResourceGridExecutorServiceResourceGridLocalNodeIdResourceGridMBeanServerResourceGridMarshallerResourceGridSpringApplicationContextResourceGridSpringResourceGrid into a computation:
public class MyGridJob extends GridRunnable {
...
@GridInstanceResource
private Grid grid;
...
}
GridComputeTaskSpis annotation. Refer to GridComputeTask documentation for more information.| Modifier and Type | Method and Description |
|---|---|
<R> GridFuture<R> |
affinityCall(String cacheName,
Object affKey,
Callable<R> job)
Executes given job on the node where data for provided affinity key is located
(a.k.a.
|
GridFuture<?> |
affinityRun(String cacheName,
Object affKey,
Runnable job)
Executes given job on the node where data for provided affinity key is located
(a.k.a.
|
<T,R> GridFuture<Collection<R>> |
apply(GridClosure<T,R> job,
Collection<? extends T> args)
Executes provided closure job on nodes within this grid projection.
|
<R,T> GridFuture<R> |
apply(GridClosure<T,R> job,
T arg)
Executes provided closure job on a node in this grid projection.
|
<R1,R2,T> GridFuture<R2> |
apply(GridClosure<T,R1> job,
Collection<? extends T> args,
GridReducer<R1,R2> rdc)
Executes provided closure job on nodes within this grid projection.
|
<R> GridFuture<Collection<R>> |
broadcast(Callable<R> job)
Broadcasts given job to all nodes in grid projection.
|
<R,T> GridFuture<Collection<R>> |
broadcast(GridClosure<T,R> job,
T arg)
Broadcasts given closure job with passed in argument to all nodes in grid projection.
|
GridFuture<?> |
broadcast(Runnable job)
Broadcasts given job to all nodes in grid projection.
|
<R> GridFuture<R> |
call(Callable<R> job)
Executes provided job on a node in this grid projection.
|
<R> GridFuture<Collection<R>> |
call(Collection<? extends Callable<R>> jobs)
Executes collection of jobs on nodes within this grid projection.
|
<R1,R2> GridFuture<R2> |
call(Collection<? extends Callable<R1>> jobs,
GridReducer<R1,R2> rdc)
Executes collection of jobs on nodes within this grid projection.
|
void |
cancelJob(GridUuid jobId)
Cancels job with the given job ID, if it is currently running inside this projection.
|
void |
cancelTask(GridUuid sesId)
Cancels task with the given execution session ID, if it currently running inside this projection.
|
<T,R> GridComputeTaskFuture<R> |
execute(Class<? extends GridComputeTask<T,R>> taskCls,
T arg)
Executes given task on the grid projection.
|
<T,R> GridComputeTaskFuture<R> |
execute(GridComputeTask<T,R> task,
T arg)
Executes given task on this grid projection.
|
<T,R> GridComputeTaskFuture<R> |
execute(String taskName,
T arg)
Executes given task on this grid projection.
|
ExecutorService |
executorService()
Creates new
ExecutorService which will execute all submitted
Callable and Runnable jobs on nodes in this grid projection. |
void |
localDeployTask(Class<? extends GridComputeTask> taskCls,
ClassLoader clsLdr)
Explicitly deploys a task with given class loader on the local node.
|
Map<String,Class<? extends GridComputeTask<?,?>>> |
localTasks()
Gets map of all locally deployed tasks keyed by their task name .
|
GridProjection |
projection()
Gets grid projection to which this
GridCompute instance belongs. |
GridFuture<?> |
run(Collection<? extends Runnable> jobs)
Executes collection of jobs on grid nodes within this grid projection.
|
GridFuture<?> |
run(Runnable job)
Executes provided job on a node in this grid projection.
|
<R> GridComputeTaskFuture<R> |
taskFuture(GridUuid sesId)
Gets task future based on execution session ID.
|
void |
undeployTask(String taskName)
Makes the best attempt to undeploy a task with given name from this grid projection.
|
GridCompute |
withName(String taskName)
Sets task name for the next executed task on this projection in the current thread.
|
GridCompute |
withNoFailover()
Sets no-failover flag for the next executed task on this projection in the current thread.
|
GridCompute |
withTimeout(long timeout)
Sets task timeout for the next executed task on this projection in the current thread.
|
GridProjection projection()
GridCompute instance belongs.GridCompute instance belongs.GridFuture<?> affinityRun(@Nullable String cacheName, Object affKey, Runnable job)
cacheName - Name of the cache to use for affinity co-location.affKey - Affinity key.job - Job which will be co-located on the node with given affinity key.GridComputeJobContext.cacheName(),
GridComputeJobContext.affinityKey()<R> GridFuture<R> affinityCall(@Nullable String cacheName, Object affKey, Callable<R> job)
cacheName - Name of the cache to use for affinity co-location.affKey - Affinity key.job - Job which will be co-located on the node with given affinity key.GridComputeJobContext.cacheName(),
GridComputeJobContext.affinityKey()<T,R> GridComputeTaskFuture<R> execute(Class<? extends GridComputeTask<T,R>> taskCls, @Nullable T arg)
GridComputeTask documentation.taskCls - Class of the task to execute. If class has GridComputeTaskName annotation,
then task is deployed under a name specified within annotation. Otherwise, full
class name is used as task name.arg - Optional argument of task execution, can be null.<T,R> GridComputeTaskFuture<R> execute(GridComputeTask<T,R> task, @Nullable T arg)
GridComputeTask documentation.task - Instance of task to execute. If task class has GridComputeTaskName annotation,
then task is deployed under a name specified within annotation. Otherwise, full
class name is used as task name.arg - Optional argument of task execution, can be null.<T,R> GridComputeTaskFuture<R> execute(String taskName, @Nullable T arg)
GridComputeTask documentation.
If task for given name has not been deployed yet, then taskName will be
used as task class name to auto-deploy the task (see localDeployTask(Class, ClassLoader) method).
taskName - Name of the task to execute.arg - Optional argument of task execution, can be null.for information about task execution.GridFuture<?> broadcast(Runnable job)
job - Job to broadcast to all projection nodes.<R> GridFuture<Collection<R>> broadcast(Callable<R> job)
job - Job to broadcast to all projection nodes.<R,T> GridFuture<Collection<R>> broadcast(GridClosure<T,R> job, @Nullable T arg)
job - Job to broadcast to all projection nodes.arg - Job closure argument.GridFuture<?> run(Runnable job)
job - Job closure to execute.GridFuture<?> run(Collection<? extends Runnable> jobs)
jobs - Collection of jobs to execute.<R> GridFuture<R> call(Callable<R> job)
job - Job to execute.<R> GridFuture<Collection<R>> call(Collection<? extends Callable<R>> jobs)
jobs - Collection of jobs to execute.<R1,R2> GridFuture<R2> call(Collection<? extends Callable<R1>> jobs, GridReducer<R1,R2> rdc)
jobs - Collection of jobs to execute.rdc - Reducer to reduce all job results into one individual return value.<R,T> GridFuture<R> apply(GridClosure<T,R> job, @Nullable T arg)
run(...) and call(...) methods in a way that it receives job argument
which is then passed into the closure at execution time.job - Job to run.arg - Job argument.<T,R> GridFuture<Collection<R>> apply(GridClosure<T,R> job, Collection<? extends T> args)
job - Job to run.args - Job arguments.<R1,R2,T> GridFuture<R2> apply(GridClosure<T,R1> job, Collection<? extends T> args, GridReducer<R1,R2> rdc)
job - Job to run.args - Job arguments.rdc - Reducer to reduce all job results into one individual return value.ExecutorService executorService()
ExecutorService which will execute all submitted
Callable and Runnable jobs on nodes in this grid projection. This essentially
creates a Distributed Thread Pool that can be used as a
replacement for local thread pools.ExecutorService.@Nullable <R> GridComputeTaskFuture<R> taskFuture(GridUuid sesId)
R - Task result type.sesId - Session ID for task execution.null otherwise.void cancelTask(GridUuid sesId) throws GridException
sesId - Execution session ID.GridException - If task cancellation failed.void cancelJob(GridUuid jobId) throws GridException
jobId - Job ID.GridException - If task cancellation failed.GridCompute withName(String taskName)
GridComputeTask.
Here is an example.
GridGain.grid().withName("MyTask").run(new MyRunnable() {...});
taskName - Task name.GridCompute instance for chaining calls.GridCompute withTimeout(long timeout)
GridComputeTask.
Here is an example.
GridGain.grid().withTimeout(10000).run(new MyRunnable() {...});
timeout - Computation timeout in milliseconds.GridCompute instance for chaining calls.GridCompute withNoFailover()
Here is an example.
GridGain.grid().compute().withNoFailover().run(new MyRunnable() {...});
this).void localDeployTask(Class<? extends GridComputeTask> taskCls, ClassLoader clsLdr) throws GridException
Note that tasks are automatically deployed upon first execution (if peer-class-loading is enabled),
so use this method only when the provided class loader is different from the
taskClass.getClassLoader().
Another way of class deployment is deployment from local class path. Classes from local class path always have a priority over P2P deployed ones.
Note that class can be deployed multiple times on remote nodes, i.e. re-deployed. GridGain maintains internal version of deployment for each instance of deployment (analogous to class and class loader in Java). Execution happens always on the latest deployed instance.
This method has no effect if the class passed in was already deployed.
taskCls - Task class to deploy. If task class has GridComputeTaskName annotation,
then task will be deployed under the name specified within annotation. Otherwise, full
class name will be used as task's name.clsLdr - Task class loader. This class loader is in charge
of loading all necessary resources for task execution.GridException - If task is invalid and cannot be deployed.Map<String,Class<? extends GridComputeTask<?,?>>> localTasks()
void undeployTask(String taskName) throws GridException
taskName - Name of the task to undeploy.GridException - Thrown if undeploy failed.Copyright © 2014. All rights reserved.