Search in sources :

Example 1 with RunnableToBeExecuted

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

the class AnnotationSupportImpl method addToTaskChainAsCalledFromParallel.

/**
 * Add to task chain when Controlled called from parallel flow.
 * @param joinPoint Join point
 * @param controlled Controlled
 * @param task Parallel Task
 * @param taskExecutorPresent Is task executor present
 * @param nameForTheTaskExecutor Name of task executor
 * @param taskExecutor Task executor
 * @return Task executor
 */
protected TaskExecutor addToTaskChainAsCalledFromParallel(ProceedingJoinPoint joinPoint, Controlled controlled, Task task, boolean taskExecutorPresent, String nameForTheTaskExecutor, TaskExecutor taskExecutor) {
    int taskId = parallelJoinHelper.getObjectKeyForParalleldTask();
    int parallelTaskId = ParallelJoinHelper.getParallelId();
    logger.fine("already from same executor..so will be processed directly via different task.");
    RunnableToBeExecuted runnableToBeExecuted = () -> {
        ParallelJoinHelper.putObject(parallelTaskId, taskId, joinPoint.proceed());
    };
    // / TODO - IS this check required?
    if (taskExecutorPresent) {
        taskExecutor = task.getTaskExecutor();
    } else {
        runnableToBeExecuted = () -> {
            try {
                TaskExecutorsInUseThreadLocal.setTaskExecutor(nameForTheTaskExecutor);
                Object result = joinPoint.proceed();
                if (Objects.nonNull(result)) {
                    ParallelJoinHelper.putObject(parallelTaskId, taskId, result);
                }
            } finally {
                TaskExecutorsInUseThreadLocal.removeTaskExecutor(nameForTheTaskExecutor);
            }
        };
    }
    String name = "ParallelId:" + parallelTaskId + " taskId:" + taskId + " " + getTaskNameFromJoinPoint(joinPoint);
    org.ak.trafficController.Task.TaskType taskType = convertAnnotationTaskTypeToFrameworkTaskType(controlled.taskType());
    Task thisTask;
    if (taskType == org.ak.trafficController.Task.TaskType.NORMAL) {
        thisTask = taskExecutor.of(runnableToBeExecuted);
    } else {
        thisTask = taskExecutor.slowOf(runnableToBeExecuted);
    }
    ((ParallelTask) task).addTask(thisTask);
    setThreadingDetailsIfAny(thisTask, controlled.threadDetailsDataExtractClass(), controlled.threadDetailsDataExtractMethodName(), controlled.threadDetailsProcessorClass(), controlled.threadDetailsProcessorMethodName(), controlled.threadDetailsCleanerClass(), controlled.threadDetailsCleanerMethodName());
    return taskExecutor;
}
Also used : RunnableToBeExecuted(org.ak.trafficController.RunnableToBeExecuted) 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) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 2 with RunnableToBeExecuted

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

the class AnnotationSupportImpl method runAsync.

/**
 * Handles Async operation where user is looking to submit something to get processed but calling thread should not wait for it.
 * If called in Parallel flow, will be added as a async task/
 * @param joinPoint Join point
 * @param async Async
 * @return Returns null if no exception
 * @throws Throwable In case of exception in execution of annotated method
 */
@Around("execution(@org.ak.trafficController.annotations.api.Submit * *(..)) && @annotation(async)")
public Object runAsync(ProceedingJoinPoint joinPoint, Submit async) throws Throwable {
    RunnableToBeExecuted taskToWorkOn = () -> {
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            logger.log(java.util.logging.Level.WARNING, "exception occured while running a submit request", e);
        }
    };
    TaskExecutor taskExecutor = taskHelper.getTaskExecutor(async, joinPoint);
    Task task = null;
    TaskType taskType = async.taskType();
    switch(taskType) {
        case NORMAL:
            task = taskExecutor.of(taskToWorkOn);
            break;
        case SLOW:
            task = taskExecutor.slowOf(taskToWorkOn);
    }
    setThreadingDetailsIfAny(task, async.threadDetailsDataExtractClass(), async.threadDetailsDataExtractMethodName(), async.threadDetailsProcessorClass(), async.threadDetailsProcessorMethodName(), async.threadDetailsCleanerClass(), async.threadDetailsCleanerMethodName());
    Task taskInThread = ParallelJoinHelper.getTask();
    if (taskInThread == null) {
        task.submit();
    } else {
        AtomicReference<Task> taskReference = new AtomicReference<Task>(task);
        ((ParallelTask) taskInThread).addRunnables(convertAnnotationTaskTypeToFrameworkTaskType(taskType), taskExecutor, () -> taskExecutor.enque(taskReference.get()));
    }
    return null;
}
Also used : RunnableToBeExecuted(org.ak.trafficController.RunnableToBeExecuted) TaskExecutor(org.ak.trafficController.TaskExecutor) 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) TaskType(org.ak.trafficController.annotations.api.TaskType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ParallelTask(org.ak.trafficController.ParallelTask) Around(org.aspectj.lang.annotation.Around)

Example 3 with RunnableToBeExecuted

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

the class AnnotationSupportImpl method executeControlled.

/**
 * Execute controlled in direct flow.
 * @param joinPoint Join point
 * @param parallel Controlled
 * @param nameForTheTaskExecutor Name of task executor
 * @param taskExecutor Task executor
 * @return Output of annotated method
 * @throws Throwable In case annotated method throws exception
 */
protected Object executeControlled(ProceedingJoinPoint joinPoint, Controlled parallel, String nameForTheTaskExecutor, TaskExecutor taskExecutor) throws Throwable {
    AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>(null);
    AtomicReference<Object> value = new AtomicReference<Object>(null);
    RunnableToBeExecuted taskToWorkOn = () -> {
        try {
            TaskExecutorsInUseThreadLocal.setTaskExecutor(nameForTheTaskExecutor);
            Object k = joinPoint.proceed();
            value.set(k);
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Exception occured while executing a parallel request.", e);
            throwableRef.set(e);
        } finally {
            TaskExecutorsInUseThreadLocal.removeTaskExecutor(nameForTheTaskExecutor);
        }
    };
    switch(parallel.taskType()) {
        case NORMAL:
            taskExecutor.of(taskToWorkOn).start(parallel.waitTimeInMilliSeconds());
            break;
        case SLOW:
            taskExecutor.slowOf(taskToWorkOn).start(parallel.waitTimeInMilliSeconds());
    }
    Throwable throwable = throwableRef.get();
    if (throwable != null) {
        throw throwable;
    }
    return value.get();
}
Also used : RunnableToBeExecuted(org.ak.trafficController.RunnableToBeExecuted) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

RunnableToBeExecuted (org.ak.trafficController.RunnableToBeExecuted)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 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 TaskExecutor (org.ak.trafficController.TaskExecutor)1 TaskType (org.ak.trafficController.annotations.api.TaskType)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1