It's not, my bad. The interceptor was not being applied and I thought it
was OK.

The problem I have now is that the excutor is not being inyected.

    @Resource(name = "TravelcAsynchronousPool")
    private ManagedExecutorService executor;


And it is declared in resources.xml


    <Resource id="TravelcAsynchronousPool" type="ManagedExecutorService">
        Core 100
        Max 200
        Queue 1500
        KeepAlive 1 s
        ThreadFactory
org.apache.openejb.threads.impl.ManagedThreadFactoryImpl
        Lazy true
    </Resource>



On Wed, Jan 25, 2017 at 1:53 PM, Romain Manni-Bucau [via TomEE & OpenEJB] <
[email protected]> wrote:

> 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 <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=4680932&i=0>>:
>
> > 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.
> >
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://tomee-openejb.979440.n4.nabble.com/MDC-and-Asynchronous-
> tp4680927p4680932.html
> To unsubscribe from MDC and @Asynchronous, click here
> <http://tomee-openejb.979440.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4680927&code=Y29jb3Jvc3NlbGxvQGdtYWlsLmNvbXw0NjgwOTI3fC05Mzc2MzQ4MzY=>
> .
> NAML
> <http://tomee-openejb.979440.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: 
http://tomee-openejb.979440.n4.nabble.com/MDC-and-Asynchronous-tp4680927p4680933.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Reply via email to