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;
}
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;
}
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();
}
Aggregations