I finally went without the managedTask, simpler (I couldn't get the resource
inyection, I don't know why, with priority and declaring it in beans.xml)
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Async {
}
@Async
@Interceptor
public class AsyncInterceptor implements Serializable {
private ManagedExecutorService executor;
@AroundInvoke
public Object submitAsync(InvocationContext ctx) throws Exception {
if (executor == null) {
executor = (ManagedExecutorService) new
InitialContext().lookup("openejb:Resource/TravelcAsynchronousPool");
}
Map<String, String> m = MDC.getCopyOfContextMap();
FutureDelegator delegator = new FutureDelegator(executor.submit(()
-> {
MDC.setContextMap(m);
return ctx.proceed();
}));
return delegator;
}
}
public class FutureDelegator implements Future {
private final Future<?> future;
public FutureDelegator(Future<?> future) {
this.future = future;
}
@Override
public Object get() throws InterruptedException, ExecutionException {
AsyncResult<?> asyncResult = (AsyncResult<?>) future.get();
if (asyncResult == null) {
return null;
}
return asyncResult.get();
}
@Override
public Object get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException {
AsyncResult<?> asyncResult = (AsyncResult<?>) future.get(timeout,
unit);
if (asyncResult == null) {
return null;
}
return asyncResult.get();
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return future.cancel(mayInterruptIfRunning);
}
@Override
public boolean isCancelled() {
return future.isCancelled();
}
@Override
public boolean isDone() {
return future.isDone();
}
}
Thanks Romain
--
View this message in context:
http://tomee-openejb.979440.n4.nabble.com/MDC-and-Asynchronous-tp4680927p4680936.html
Sent from the TomEE Users mailing list archive at Nabble.com.