Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-592

Support use of decorator of Callable<T>

    XMLWordPrintable

Details

    Description

      Decorator exists for Runnable. Should allow for Callable<T> to be used as well.

      This is actually pretty useful, for example if you want to execute under a temporary new scope in the same thread, e.g.

      @Inject Instance<PaymentProcessor> paymentProcessorSource; // which implements Callable<Result>
      Result result = paymentProcessorSource.get().call();

      Or, in the case of a thread pool, you can use Future<T> etc.

      /**

      • Decorator for all beans which implements Callable. It intercepts the call
      • to the call() method to set up the ThreadContext for the new thread so that
      • instances of @ThreadScoped beans can be correctly resolved.
        */
        @Decorator
        public class CallableDecorator<T> implements Callable<T> {

      @Inject @Delegate Callable<T> callable;

      /**

      • Set up the ThreadContet and delegate.
        */
        public T call()
        {
        // set up context for this thread
        final ThreadContext threadContext = WeldSEBeanRegistrant.THREAD_CONTEXT;
        threadContext.setBeanStore(new HashMapBeanStore());
        threadContext.setActive(true);
        // run the original thread
        try { return callable.call(); }

        finally

        { threadContext.destroy(); }

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            genman_jira Elias Ross (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: