Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.ak.trafficController.annotations.impl;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.inject.Named;
import org.ak.trafficController.ExecutableTask;
import org.ak.trafficController.ParallelExecutingTask;
import org.ak.trafficController.ParallelTask;
import org.ak.trafficController.RunnableToBeExecuted;
import org.ak.trafficController.Task;
import org.ak.trafficController.TaskExecutor;
import org.ak.trafficController.ThreadingDetails;
import org.ak.trafficController.UnlinkedTask;
import org.ak.trafficController.annotations.api.Controlled;
import org.ak.trafficController.annotations.api.Join;
import org.ak.trafficController.annotations.api.Parallel;
import org.ak.trafficController.annotations.api.Submit;
import org.ak.trafficController.annotations.api.TaskType;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.util.StringUtils;
/**
* This class handles task handling annotations (Controlled, Submit, Parallel, Join).
* Controlled if annotated, in normal flow will run in specified executor. This helps in throttling as only specified number of tasks can run at a time.
* Submit if annotated, method is submitted to task executor will be run async without and won't make calling thread to wait for completion of logic.
* 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.
* @author amit.khosla
*/
@Aspect
@Named
public class AnnotationSupportImpl {
Logger logger = Logger.getLogger(AnnotationSupportImpl.class.getName());
@Inject
MethodInvoker methodInvoker;
/**
* Parallel id
*/
AtomicInteger parallelId = new AtomicInteger(0);
@Inject
TaskHelper taskHelper;
@Inject
ParallelJoinHelper parallelJoinHelper;
/**
* 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