Search in sources :

Example 1 with ParallelExecutingTask

use of org.ak.trafficController.ParallelExecutingTask in project trafficController by amitkhosla.

the class AnnotationSupportImpl method runParallel.

/**
 * Handles Parallel annotated methods.
 * Parallel if annotated, all method calls to Controlled annotated methods is made in parallel. Join annotated method joins back the the result created by controlled annotated methods.
 * It first execute the annotated method and creates task chain to be run in parallel followed by a joiner method which will join these results.
 * Parallel should be used only where we are calling different methods annotated with controlled and in different class to allow AOP play its work.
 * Output returned will be the output from Join operation or the last operation.
 * @param joinPoint Join point
 * @param parallel Parallel
 * @return Return output of the operation
 * @throws Throwable In case of any exception in processing
 */
@Around("execution(@org.ak.trafficController.annotations.api.Parallel * *(..)) && @annotation(parallel)")
public Object runParallel(ProceedingJoinPoint joinPoint, Parallel parallel) throws Throwable {
    int currentParallelId = parallelId.incrementAndGet();
    parallelJoinHelper.map.put(currentParallelId, new ConcurrentHashMap<>());
    AtomicInteger taskId = new AtomicInteger(0);
    AtomicInteger earlierParallelTaskId = new AtomicInteger(0);
    Task originalTask = parallelJoinHelper.getTask();
    boolean isSubParallelTask = originalTask != null;
    if (isSubParallelTask) {
        // this task is one task of parallel tasks.
        taskId.set(parallelJoinHelper.getObjectKeyForParalleldTask());
        earlierParallelTaskId.set(parallelJoinHelper.getParallelId());
    }
    ParallelJoinHelper.setParallelTaskId(currentParallelId);
    // if (!isSubParallelTask) {
    ExecutableTask thisParallelTask = TaskExecutor.getInstance().of(() -> {
    });
    setThreadingDetailsIfAny(thisParallelTask, parallel.threadDetailsDataExtractClass(), parallel.threadDetailsDataExtractMethodName(), parallel.threadDetailsProcessorClass(), parallel.threadDetailsProcessorMethodName(), parallel.threadDetailsCleanerClass(), parallel.threadDetailsCleanerMethodName());
    thisParallelTask.setName("ParallelTask" + currentParallelId);
    ParallelExecutingTask<Object> thenParallel = thisParallelTask.thenParallel(() -> {
    });
    thenParallel.setName("ParallelTaskName:" + currentParallelId);
    // set dummy task
    parallelJoinHelper.setTask(thenParallel);
    // }
    if (!isSubParallelTask) {
        Object val = directParallelTaskHandling(joinPoint, currentParallelId, parallel);
        return val;
    } else {
        joinPoint.proceed();
        if (originalTask instanceof ParallelTask) {
            subProcessHandling(currentParallelId, taskId, earlierParallelTaskId, originalTask, thisParallelTask);
        }
        return null;
    }
}
Also used : ParallelTask(org.ak.trafficController.ParallelTask) ParallelExecutingTask(org.ak.trafficController.ParallelExecutingTask) ExecutableTask(org.ak.trafficController.ExecutableTask) Task(org.ak.trafficController.Task) UnlinkedTask(org.ak.trafficController.UnlinkedTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutableTask(org.ak.trafficController.ExecutableTask) ParallelTask(org.ak.trafficController.ParallelTask) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ExecutableTask (org.ak.trafficController.ExecutableTask)1 ParallelExecutingTask (org.ak.trafficController.ParallelExecutingTask)1 ParallelTask (org.ak.trafficController.ParallelTask)1 Task (org.ak.trafficController.Task)1 UnlinkedTask (org.ak.trafficController.UnlinkedTask)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1