Search in sources :

Example 1 with TaskExecutionException

use of io.micronaut.scheduling.exceptions.TaskExecutionException in project micronaut-core by micronaut-projects.

the class AsyncInterceptor method intercept.

@Nullable
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    String executorServiceName = context.stringValue(Async.class).orElse(TaskExecutors.SCHEDULED);
    ExecutorService executorService;
    if (TaskExecutors.SCHEDULED.equals(executorServiceName) && scheduledExecutorService.isPresent()) {
        executorService = scheduledExecutorService.get().get();
    } else {
        executorService = scheduledExecutorServices.computeIfAbsent(executorServiceName, name -> beanLocator.findBean(ExecutorService.class, Qualifiers.byName(name)).orElseThrow(() -> new TaskExecutionException("No ExecutorService named [" + name + "] configured in application context")));
    }
    InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
    try {
        switch(interceptedMethod.resultType()) {
            case PUBLISHER:
                return interceptedMethod.handleResult(interceptedMethod.interceptResultAsPublisher(executorService));
            case COMPLETION_STAGE:
                return interceptedMethod.handleResult(CompletableFuture.supplyAsync(() -> interceptedMethod.interceptResultAsCompletionStage(), executorService).thenCompose(Function.identity()));
            case SYNCHRONOUS:
                ReturnType<Object> rt = context.getReturnType();
                Class<?> returnType = rt.getType();
                if (void.class == returnType) {
                    executorService.submit(() -> {
                        try {
                            context.proceed();
                        } catch (Throwable e) {
                            if (LOG.isErrorEnabled()) {
                                LOG.error("Error occurred executing @Async method [" + context.getExecutableMethod() + "]: " + e.getMessage(), e);
                            }
                        }
                    });
                    return null;
                }
                throw new TaskExecutionException("Method [" + context.getExecutableMethod() + "] must return either void, or an instance of Publisher or CompletionStage");
            default:
                return interceptedMethod.unsupported();
        }
    } catch (Exception e) {
        return interceptedMethod.handleException(e);
    }
}
Also used : TaskExecutionException(io.micronaut.scheduling.exceptions.TaskExecutionException) Async(io.micronaut.scheduling.annotation.Async) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) CompletableFuture(java.util.concurrent.CompletableFuture) Internal(io.micronaut.core.annotation.Internal) InterceptedMethod(io.micronaut.aop.InterceptedMethod) Function(java.util.function.Function) TaskExecutors(io.micronaut.scheduling.TaskExecutors) MethodInterceptor(io.micronaut.aop.MethodInterceptor) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) BeanProvider(io.micronaut.context.BeanProvider) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) ReturnType(io.micronaut.core.type.ReturnType) Optional(java.util.Optional) BeanLocator(io.micronaut.context.BeanLocator) InterceptPhase(io.micronaut.aop.InterceptPhase) ExecutorService(java.util.concurrent.ExecutorService) Named(jakarta.inject.Named) TaskExecutionException(io.micronaut.scheduling.exceptions.TaskExecutionException) Async(io.micronaut.scheduling.annotation.Async) InterceptedMethod(io.micronaut.aop.InterceptedMethod) ExecutorService(java.util.concurrent.ExecutorService) TaskExecutionException(io.micronaut.scheduling.exceptions.TaskExecutionException) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

InterceptPhase (io.micronaut.aop.InterceptPhase)1 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 BeanLocator (io.micronaut.context.BeanLocator)1 BeanProvider (io.micronaut.context.BeanProvider)1 Internal (io.micronaut.core.annotation.Internal)1 Nullable (io.micronaut.core.annotation.Nullable)1 ReturnType (io.micronaut.core.type.ReturnType)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 TaskExecutors (io.micronaut.scheduling.TaskExecutors)1 Async (io.micronaut.scheduling.annotation.Async)1 TaskExecutionException (io.micronaut.scheduling.exceptions.TaskExecutionException)1 Named (jakarta.inject.Named)1 Singleton (jakarta.inject.Singleton)1 Map (java.util.Map)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1