[weld-issues] [JBoss JIRA] Commented: (WELD-862) Interceptors not threadsafe

Peter Royle (JIRA) jira-events at lists.jboss.org
Fri May 6 03:05:18 EDT 2011

    [ https://issues.jboss.org/browse/WELD-862?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12600244#comment-12600244 ] 

Peter Royle commented on WELD-862:

For the record I have implemented @Asynchronous in a practically ientical way in Seam Cron. It doesn't work against Weld for me either. For giggles I tested it out against embedded Open Web Beans using Arquillian and it worked as intended, presumably because OWB's interceptor code is not similarly affected.

> Interceptors not threadsafe
> ---------------------------
>                 Key: WELD-862
>                 URL: https://issues.jboss.org/browse/WELD-862
>             Project: Weld
>          Issue Type: Bug
>          Components: Interceptors and Decorators
>    Affects Versions: 1.1.0.Final
>         Environment: Jetty, Weld Filter
>            Reporter: Sebastian Schaffert
> I am trying to implement an "@Asynchronous" interceptor that runs methods annotated with the @Asynchronous annotation in a separate thread. The implementation of the interceptor currently looks as follows:
>     private static final ThreadGroup asyncMethods = new ThreadGroup("asynchronous method invocations");
>     @AroundInvoke
>     public Object manageAsynchronous(final InvocationContext ctx) throws Exception {
>         final UUID threadID = UUID.randomUUID();
>         Runnable r = new Runnable() {
>             @Override
>             public void run() {
>                 try {
>                     log.debug("asynchronous method invocation of {}.{} (Thread ID {})",new Object[] {ctx.getTarget().getClass().getName(),ctx.getMethod().getName(), threadID});
>                     Object val = ctx.proceed();
>                     if(val != null) {
>                         log.debug("asynchronous method invocation of {}.{} (Thread ID {}) returned value {}",new Object[] {ctx.getClass().getName(),ctx.getMethod().getName(), threadID, val});
>                     }
>                 } catch(Exception ex) {
>                     log.error("exception during asynchronous method invocation",ex);
>                 }
>             }
>         };
>         Thread t = new Thread(asyncMethods,r);
>         t.setName("asynchronous method invocation of "+ctx.getTarget().getClass().getName()+ctx.getMethod().getName() + " (Threaad ID " + threadID+")");
>         t.start();
>         return null;
>     }
> Now the problem is that the interceptor is called infinitely often. The reason is that the annotated method forks a new thread and then returns instantly, setting the variable "currentPosition" in SimpleInterceptorChain back to the value 0 (in a "finally" block). So when the proceed() method is called inside the thread, the interceptor chain again points to the first interceptor in the chain and it all repeats infinitely.

This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

More information about the weld-issues mailing list