Does it work? shouldn't it be:

    @AroundInvoke
    public Object submitAsync(InvocationContext ctx) throws Exception {
        return executor.submit(new FutureDelegator(() -> { // in the
constructor capture current MDC and (re)set it ManagedTaskListener hooks
            return ctx.proceed();
        }));
    }


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-01-25 13:24 GMT+01:00 cocorossello <[email protected]>:

> 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.
>

Reply via email to