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