I got it, this is the code just in case anyone needs it.
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Async {
}
@Async
@Interceptor
public class AsyncInterceptor implements Serializable {
@Resource(name = "TravelcAsynchronousPool")
private ManagedExecutorService executor;
@AroundInvoke
public Object submitAsync(InvocationContext ctx) throws Exception {
return new FutureDelegator(executor.submit(() -> {
return ctx.proceed();
}));
}
}
public class FutureDelegator implements Future, ManagedTask,
ManagedTaskListener {
private final Future<?> future;
private Map<String, String> mdcCopy;
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();
}
@Override
public ManagedTaskListener getManagedTaskListener() {
return this;
}
@Override
public Map<String, String> getExecutionProperties() {
return new HashMap();
}
@Override
public void taskSubmitted(Future<?> future, ManagedExecutorService
executor, Object task) {
mdcCopy = MDC.getCopyOfContextMap();
}
@Override
public void taskAborted(Future<?> future, ManagedExecutorService
executor, Object task, Throwable exception) {
//NADA
}
@Override
public void taskDone(Future<?> future, ManagedExecutorService executor,
Object task, Throwable exception) {
//NADA
}
@Override
public void taskStarting(Future<?> future, ManagedExecutorService
executor, Object task) {
MDC.setContextMap(mdcCopy);
}
}
--
View this message in context:
http://tomee-openejb.979440.n4.nabble.com/MDC-and-Asynchronous-tp4680927p4680931.html
Sent from the TomEE Users mailing list archive at Nabble.com.