Search in sources :

Example 1 with ExecutableTask

use of org.ak.trafficController.ExecutableTask 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)

Example 2 with ExecutableTask

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

the class AnnotationSupportImpl method subProcessHandling.

/**
 * Sub process handling. This method will be called while handling Parallel in case where parallel is being called inside another parallel flow.
 * @param currentParallelId Current parallel id
 * @param taskId Task id
 * @param earlierParallelTaskId Earlier parallel task id
 * @param originalTask Original task
 * @param thisParallelTask Current parallel task
 */
protected void subProcessHandling(int currentParallelId, AtomicInteger taskId, AtomicInteger earlierParallelTaskId, Task originalTask, ExecutableTask thisParallelTask) {
    Task task = parallelJoinHelper.getTask().then(() -> {
        Object result = performCleanup(currentParallelId);
        if (Objects.nonNull(result)) {
            ParallelJoinHelper.putObject(earlierParallelTaskId.get(), taskId.get(), result);
        }
    });
    ((ParallelTask) originalTask).addTask(thisParallelTask);
    parallelJoinHelper.setTask(originalTask);
    ParallelJoinHelper.removeParallelId(currentParallelId);
    ParallelJoinHelper.setParallelTaskId(earlierParallelTaskId.get());
}
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) ParallelTask(org.ak.trafficController.ParallelTask)

Aggregations

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