Search in sources :

Example 1 with Gridify

use of org.apache.ignite.compute.gridify.Gridify in project ignite by apache.

the class GridifyAspectJAspect method gridify.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * @param joinPnt Join point provided by AspectJ AOP.
 * @return Method execution result.
 * @throws Throwable If execution failed.
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "unchecked" })
@Around("execution(@org.apache.ignite.compute.gridify.Gridify * *(..)) && !cflow(call(* org.apache.ignite.compute.ComputeJob.*(..)))")
public Object gridify(ProceedingJoinPoint joinPnt) throws Throwable {
    Method mtd = ((MethodSignature) joinPnt.getSignature()).getMethod();
    Gridify ann = mtd.getAnnotation(Gridify.class);
    assert ann != null : "Intercepted method does not have gridify annotation.";
    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended Ignite instance name.
    // NOTE: the 'ann.igniteInstanceName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String igniteInstanceName = F.isEmpty(ann.igniteInstanceName()) ? ann.gridName() : ann.igniteInstanceName();
    if (F.isEmpty(igniteInstanceName))
        igniteInstanceName = null;
    if (G.state(igniteInstanceName) != STARTED)
        throw new IgniteCheckedException("Grid is not locally started: " + igniteInstanceName);
    // Initialize defaults.
    GridifyArgument arg = new GridifyArgumentAdapter(mtd.getDeclaringClass(), mtd.getName(), mtd.getParameterTypes(), joinPnt.getArgs(), joinPnt.getTarget());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return joinPnt.proceed();
    }
    if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty()) {
        throw new IgniteCheckedException("Gridify annotation must specify either Gridify.taskName() or " + "Gridify.taskClass(), but not both: " + ann);
    }
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        // If task class was specified.
        if (!ann.taskClass().equals(GridifyDefaultTask.class)) {
            return ignite.compute().withTimeout(ann.timeout()).execute((Class<? extends ComputeTask<GridifyArgument, Object>>) ann.taskClass(), arg);
        }
        // If task name was not specified.
        if (ann.taskName().isEmpty()) {
            return ignite.compute().withTimeout(ann.timeout()).execute(new GridifyDefaultTask(joinPnt.getSignature().getDeclaringType()), arg);
        }
        // If task name was specified.
        return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
    } catch (Exception e) {
        for (Class<?> ex : ((MethodSignature) joinPnt.getSignature()).getMethod().getExceptionTypes()) {
            // Descend all levels down.
            Throwable cause = e.getCause();
            while (cause != null) {
                if (ex.isAssignableFrom(cause.getClass()))
                    throw cause;
                cause = cause.getCause();
            }
            if (ex.isAssignableFrom(e.getClass()))
                throw e;
        }
        throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Gridify(org.apache.ignite.compute.gridify.Gridify) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) GridifyArgument(org.apache.ignite.compute.gridify.GridifyArgument) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyDefaultTask(org.apache.ignite.compute.gridify.aop.GridifyDefaultTask) GridifyArgumentAdapter(org.apache.ignite.compute.gridify.aop.GridifyArgumentAdapter) Ignite(org.apache.ignite.Ignite) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Around(org.aspectj.lang.annotation.Around)

Example 2 with Gridify

use of org.apache.ignite.compute.gridify.Gridify in project ignite by apache.

the class GridifySpringAspect method invoke.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * {@inheritDoc}
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "unchecked" })
@Override
public Object invoke(MethodInvocation invoc) throws Throwable {
    Method mtd = invoc.getMethod();
    Gridify ann = mtd.getAnnotation(Gridify.class);
    assert ann != null : "Intercepted method does not have gridify annotation.";
    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended Ignite instance name.
    // NOTE: the 'ann.igniteInstanceName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String igniteInstanceName = F.isEmpty(ann.igniteInstanceName()) ? ann.gridName() : ann.igniteInstanceName();
    if (F.isEmpty(igniteInstanceName))
        igniteInstanceName = null;
    if (G.state(igniteInstanceName) != STARTED)
        throw new IgniteCheckedException("Grid is not locally started: " + igniteInstanceName);
    // Initialize defaults.
    GridifyArgument arg = new GridifyArgumentAdapter(mtd.getDeclaringClass(), mtd.getName(), mtd.getParameterTypes(), invoc.getArguments(), invoc.getThis());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return invoc.proceed();
    }
    if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty())
        throw new IgniteCheckedException("Gridify annotation must specify either Gridify.taskName() or " + "Gridify.taskClass(), but not both: " + ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        if (!ann.taskClass().equals(GridifyDefaultTask.class))
            return ignite.compute().withTimeout(ann.timeout()).execute((Class<? extends ComputeTask<GridifyArgument, Object>>) ann.taskClass(), arg);
        // If task name was not specified.
        if (ann.taskName().isEmpty())
            return ignite.compute().withTimeout(ann.timeout()).execute(new GridifyDefaultTask(invoc.getMethod().getDeclaringClass()), arg);
        // If task name was specified.
        return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
    } catch (Exception e) {
        for (Class<?> ex : invoc.getMethod().getExceptionTypes()) {
            // Descend all levels down.
            Throwable cause = e.getCause();
            while (cause != null) {
                if (ex.isAssignableFrom(cause.getClass()))
                    throw cause;
                cause = cause.getCause();
            }
            if (ex.isAssignableFrom(e.getClass()))
                throw e;
        }
        throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) Method(java.lang.reflect.Method) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Gridify(org.apache.ignite.compute.gridify.Gridify) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) GridifyArgument(org.apache.ignite.compute.gridify.GridifyArgument) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyDefaultTask(org.apache.ignite.compute.gridify.aop.GridifyDefaultTask) GridifyArgumentAdapter(org.apache.ignite.compute.gridify.aop.GridifyArgumentAdapter) Ignite(org.apache.ignite.Ignite) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException)

Example 3 with Gridify

use of org.apache.ignite.compute.gridify.Gridify in project ignite by apache.

the class P2PGridifySelfTest method executeGridifyResource.

/**
 * Note that this method sends instance of test class to remote node.
 * Be sure that this instance does not have none-serializable fields or references
 * to the objects that could not be instantiated like class loaders and so on.
 *
 * @param res Result.
 * @return The same value as parameter has.
 */
@Gridify(igniteInstanceName = "org.apache.ignite.p2p.GridP2PGridifySelfTest1")
public Integer executeGridifyResource(int res) {
    String path = "org/apache/ignite/p2p/p2p.properties";
    GridTestClassLoader tstClsLdr = new GridTestClassLoader(GridP2PTestTask.class.getName(), GridP2PTestJob.class.getName());
    // Test property file load.
    byte[] bytes = new byte[20];
    try (InputStream in = tstClsLdr.getResourceAsStream(path)) {
        if (in == null) {
            System.out.println("Resource could not be loaded: " + path);
            return -2;
        }
        in.read(bytes);
    } catch (IOException e) {
        System.out.println("Failed to read from resource stream: " + e.getMessage());
        return -3;
    }
    String rsrcVal = new String(bytes).trim();
    System.out.println("Remote resource content is : " + rsrcVal);
    if (!rsrcVal.equals("resource=loaded")) {
        System.out.println("Invalid loaded resource value: " + rsrcVal);
        return -4;
    }
    return res;
}
Also used : InputStream(java.io.InputStream) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) IOException(java.io.IOException) Gridify(org.apache.ignite.compute.gridify.Gridify)

Aggregations

Gridify (org.apache.ignite.compute.gridify.Gridify)3 Method (java.lang.reflect.Method)2 Ignite (org.apache.ignite.Ignite)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridifyArgument (org.apache.ignite.compute.gridify.GridifyArgument)2 GridifyInterceptor (org.apache.ignite.compute.gridify.GridifyInterceptor)2 GridifyRuntimeException (org.apache.ignite.compute.gridify.GridifyRuntimeException)2 GridifyArgumentAdapter (org.apache.ignite.compute.gridify.aop.GridifyArgumentAdapter)2 GridifyDefaultTask (org.apache.ignite.compute.gridify.aop.GridifyDefaultTask)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ComputeTask (org.apache.ignite.compute.ComputeTask)1 GridTestClassLoader (org.apache.ignite.testframework.GridTestClassLoader)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1