|
GridGain 2.1.0
Java API Specification |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||||
@Documented @Retention(value=RUNTIME) @Target(value=METHOD) public @interface Gridify
Start Here
Gridify annotation is the main way to grid-enable existing code.
This annotation can be applied to any public method that needs to be grid-enabled, static or non-static. When this annotation is applied to a method, the method execution is considered to be grid-enabled. In general, the execution of this method can be transfered to another node in the grid, potentially splitting it into multiple subtasks to be executed in parallel on multiple grid nodes. But from the caller perspective this method still looks and behaves like a local call. This is achieved by utilizing AOP-based interception of the annotated code and injecting all the necessary grid logic into the method execution.
By default, when neither Gridify.taskClass() or Gridify.taskName() are specified
a method with @Gridify annotation will be executed on randomly picked remote node.
Note that when using @Gridify annotation with default task (without
specifying explicit grid task), the state of the whole instance will be
serialized and sent out to remote node. Therefore the class must implement
Serializable interface. If you cannot make the class Serializable,
then you must implement custom grid task which will take care of proper state
initialization (see
HelloWorld - Gridify With State
example). In either case, GridGain must be able to serialize the state passed to remote node.
Refer to GridTask documentation for more information on how a task
can be split into multiple sub-jobs.
@Gridify
public static void sayIt(String arg) {
// Simply print out the argument.
System.out.println(arg);
}
Here is an example of how to grid-enable a Java method with custom task. The custom task
logic will provide a way to split and aggregate the result. The method sayIt will
be executed on remote node.
public class GridifyHelloWorldTaskExample {
...
@Gridify(taskClass = GridifyHelloWorldTask.class, timeout = 3000)
public static integer sayIt(String arg) {
// Simply print out the argument.
System.out.println(">>> Printing '" + arg + "' on this node from grid-enabled method.");
return arg.length();
}
...
}
The custom task will actually take the String passed into sayIt(String) method,
split it into words and execute every word on different remote node.
public class GridifyHelloWorldTask extends GridifyTaskSplitAdapter<Integer> {
@Override
protected Collection<? extends GridJob> split(int gridSize, GridifyArgument arg) throws GridException {
String[] words = ((String)arg.getMethodParameters()[0]).split(" ");
List<GridJobAdapter<String>> jobs = new ArrayList<GridJobAdapter<String>>(words.length);
for (String word : words) {
// Every job gets its own word as an argument.
jobs.add(new GridJobAdapter<String>(word) {
public Serializable execute() throws GridException {
// Execute gridified method.
// Note that since we are calling this method from within the grid job
// AOP-based grid enabling will not cross-cut it and method will just
// execute normally.
return GridifyHelloWorldTaskExample.sayIt(getArgument());
}
});
}
return jobs;
}
public Integer reduce(List<GridJobResult> results) throws GridException {
int totalCharCnt = 0;
for (GridJobResult res : results) {
// Every job returned a number of letters
// for the phrase it was responsible for.
Integer charCnt = res.getData();
totalCharCnt += charCnt;
}
// Account for spaces. For simplicity we assume one space between words.
totalCharCnt += results.size() - 1;
// Total number of characters in the phrase
// passed into task execution.
return totalCharCnt;
}
}
GridifySpringEnhancer.enhance(Object) for the object
that has method with Gridify annotation.
Note that this method of weaving is rather inconvenient and AspectJ or JBoss AOP is
recommended over it. Spring AOP can be used in situation when code augmentation is
undesired and cannot be used. It also allows for very fine grained control of what gets
weaved.
See Also:
Documentation
Email Support
Online Forums
Issue Tracking
Author: 2005-2008 Copyright © GridGain Systems. All Rights Reserved. ver. 2.1.0

| Optional Element Summary | |
|---|---|
String |
gridName
Name of the grid to use. |
Class<? extends GridifyInterceptor> |
interceptor
Optional interceptor class. |
Class<? extends GridTask<GridifyArgument,?>> |
taskClass
Optional gridify task class. |
String |
taskName
Optional gridify task name. |
long |
timeout
Optional gridify task execution timeout. |
public abstract String taskName
Gridify.taskClass() must
be specified - but not both. If neither one is specified tasks' fully qualified name
will be used as a default name.
public abstract Class<? extends GridTask<GridifyArgument,?>> taskClass
Gridify.taskName() must
be specified - but not both. If neither one is specified tasks' fully qualified name
will be used as a default name.
public abstract long timeout
public abstract Class<? extends GridifyInterceptor> interceptor
public abstract String gridName
GridFactory for information about named grids.
|
GridGain 2.1.0
Java API Specification |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||||
|
GridGain - Grid Computing Made Simple, ver. 2.1.0.19122008
2005-2008 Copyright © GridGain Systems. All Rights Reserved. |
|
|