Thanks to visit codestin.com
Credit goes to apacheignite.readme.io

AOP-Based Grid Enabling

Turn any Java method into a distributed closure.

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

With aspect-oriented-programming you can implicitly change any method's behavior (for example, add logging or make a method transactional). In Ignite, you can turn your method into a closure which will run on the grid. This is as simple as adding a @Gridify annotation to a method:

@Gridify
public void sayHello() {
    System.out.println("Hello!");
}

You can use any of the following AOP libraries: AspectJ, JBoss, or Spring AOP. Provided that you have properly configured one of these frameworks for your master node (the node which will originate the execution), a mere invocation of the above method would result in a task execution across the grid, which will print "Hello!" on all worker nodes in your topology.

Gridify Annotation

@Gridify annotation can be used on any method, and has the following parameters, all of which are optional:

Parameter Name

Type

Description

Default Value

taskClass

Class

Custom task class.

GridifyDefaultTask.class

taskName

String

Name of the custom task to launch.

" "

timeout

long

Task execution timeout. 0 for no timeout.

0

interceptor

Class

Interceptor class for filtering gridified methods.

GridifyInterceptor.class

gridName String

String

Name of the grid to use.

" "

Generally, if you call a Gridified method, the following happens:

  • A grid with specified gridName will be used for execution (if no grid name is specified, default no-name grid will be used).
  • If specified, an interceptor is used to check if the method should be grid-enabled. If the interceptor returns false, the method is called as usual, without grid-enabling.
  • A grid task is created and executed with effective method arguments, this object (if method is non-static), and timeout.
  • The return value of grid task is returned from the Gridified method.

Default Behaviour

If you use @Gridify annotation with no parameters, the default behavior is implied, which is the following:

  • A task of class GridifyDefaultTask is created, which generates 1 job of class GridifyJobAdapter, and uses default load balancer for choosing a worker node.
  • A job on remote node invokes a method with the passed-in parameters, using deserialized this object (or null if the method is static), and returns the method result as the job result.
  • The job result on remote node will become a task result on the caller side.
  • Task result will be returned to user as a Gridified method return value.

Custom Task

You can use a custom task for specifying grid-enabling logic for a gridified method. The example below broadcasts the gridified method to all available worker nodes and skips the reduce step (meaning that this task returns nothing) :

// Custom task class.
class GridifyBroadcastMethodTask extends GridifyTaskAdapter<Void> {
    @Nullable @Override
    public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable GridifyArgument arg) throws IgniteException {
        Map<ComputeJob, ClusterNode> ret = new HashMap<>(subgrid.size());

        // Broadcast method to all nodes.
        for (ClusterNode node : subgrid)
            ret.put(new GridifyJobAdapter(arg), node);

        return ret;
    }

    @Nullable @Override
    public Void reduce(List<ComputeJobResult> list) throws IgniteException {
        return null; // No-op.
    }
}
 
public class GridifyHelloWorldTaskExample {
	...
	// Gridified method. 
	@Gridify(taskClass = GridifyBroadcastMethodTask.class)
	public static void sayHello(String arg) {
	    System.out.println("Hello, " + arg + '!');
	}
	...
}

Configuring AOP

Ignite supports 3 AOP frameworks:

  • AspectJ
  • JBoss AOP
  • Spring AOP

In this section we describe the configuration details for each of the above frameworks. We assume IGNITE_HOME to be an Ignite installation directory.

🚧

These details apply to a logical master node only (the node that initiates the execution). Logical worker nodes should be left intact.

AspectJ

To enable AspectJ byte code weaving, your master node's JVM should be configured as follows:

  • It should be launched with the -javaagent:IGNITE_HOME/libs/aspectjweaver-1.8.9.jar argument.
  • The classpath should contain IGNITE_HOME/config/aop/aspectj folder.

JBoss AOP
To enable JBoss byte code weaving, your master node's JVM should have the following configuration:

  • It should be launched with the arguments:
    • -javaagent:[path to jboss-aop-jdk50-4.x.x.jar]
    • -Djboss.aop.class.path=[path to ignite.jar]
    • -Djboss.aop.exclude=org,com
    • -Djboss.aop.include=[your package name]
  • It should contain the following jars in classpath:
    • javassist-3.x.x.jar
    • jboss-aop-jdk50-4.x.x.jar
    • jboss-aspect-library-jdk50-4.x.x.jar
    • jboss-common-4.x.x.jar
    • trove-1.0.2.jar
🚧

JBoss Dependencies

Ignite is not shipped with JBoss, and necessary libraries will have to be downloaded separately (they come standard if you have JBoss installed already).

Spring AOP

Spring AOP framework is based on dynamic proxy implementation and doesn't require any specific runtime parameters for online weaving. All weaving is on-demand and should be performed by calling the GridifySpringEnhancer.enhance() method for the object that has a method with @Gridify annotation.
Note that this method of weaving is rather inconvenient and AspectJ or JBoss AOP are recommended instead. Spring AOP can be used in a situation when code augmentation is undesired and cannot be used. It also allows for very fine grained control of what gets weaved.