Apache Ignite compute grid's AOP API can enhance a class to execute a method in a remote node. The @Gridify
annotation can convert a method into a grid executable closure. Ignite supports three types of AOP weaving:
- AspectJ AOP: Remote Ignite node's classpath should contain the aspectj jars.
- JBoss AOP: Remote Ignite node's classpath should contain the jboss jars. You need to download the jars from Maven central and put them under Ignite's lib directory.
- Spring AOP: You need to configure build.gradle to include the spring-aop dependency. No need to put any jars in the remote Ignite node's classpath.
The following steps will convert a simple method to a grid executable closure:
- Add the following dependency to your build.gradle file:
compile group: 'org.apache.ignite', name: 'ignite-aop', version:
"${igniteVersion}"
- Create a class, GridAopTest, add the following method, and annotate it with the @Gridify annotation:
public class GridAopTest {
@Gridify
public void sayItLoud(String msg) {
System.out.println("Hey " + msg + "?");
}
}
- In the main method, add the following instructions: start an ignite instance, create an instance of the class, enhance it with GridifySpringEnhancer, and finally call the sayItLoud method on the enhanced instance of the class:
public static void main(String[] args) {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
cfg.setClientMode(true);
try (Ignite ignite = Ignition.start(cfg)) {
GridAopTest test = new GridAopTest();
test = GridifySpringEnhancer.enhance(test);
test.sayItLoud("who are you!");
}
}
- Now, launch an ignite server instance and run the program; it will print Hey who are you!? in the remote Ignite console:
The enhance method applies the Spring dynamic proxy and enhances the method that has the @Gridify annotation. The enhanced method creates an instance of GridifyDefaultTask (a ComputeTaskAdapter implementation). The default task creates a GridifyJobAdapter job instance, wraps the annotated method, and executes it on a remote node.
You can create your own task class adapter and pass it to the @Gridify annotation as a taskClass such as @Gridify(taskClass=YourClass.class).