2 Replies Latest reply on Oct 29, 2014 2:08 PM by andrea.battaglia

    Need help to instantiate a new Runnable with Weld SE 2.2.6

    andrea.battaglia

      Hi all,

       

      I'm trying to add CDI to my software using weld-se 2.2.6.

      Before starting code review, I developer a simple POC based on Weld reference 2.2.6, Chapter 18.

      My goal is to use the Java SE specific scope @ThreadScoped, but I'm non able to generate new Runnable classes via CDI.

      Application is started using org.jboss.weld.environment.se.StartMain as suggested by weld reference.

      All works fine but the injection of the @ThreadScoped object ThreadScopedObjectImpl.

      Here is my simple code:

       

       

      MyApplication

      @ApplicationScoped
      public class MyApplication {
          @Inject
          private Instance<SimpleThread> threads;
      
          public void start(@Observes ContainerInitialized event) {
              System.out.println("Hello");
              for (int i = 0; i < 3; i++) {
                  new Thread(threads.get()).start();
          }
      }
      

       

      SimpleThread

      public interface SimpleThread extends Runnable {}
      

       

      SimpleThreadImpl

      @Dependent
      public class SimpleThreadImpl implements SimpleThread {
          private static final String uuid = UUID.randomUUID().toString();
          @Inject
          private ThreadScopedObject threadScopedObject;
          public void run() {
              System.out.println("thead " + uuid + " - threadScopedObject: " + threadScopedObject);
          }
      }
      

       

       

      ThreadScopedObject

      public interface ThreadScopedObject {}
      

       

      ThreadScopedObjectImpl

      @ThreadScoped
      public class ThreadScopedObjectImpl implements ThreadScopedObject {}
      

       

      beans.xml

      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" bean-discovery-mode="annotated" version="1.1">
          <decorators>
              <class>org.jboss.weld.environment.se.threading.RunnableDecorator</class>
          </decorators>
      </beans>
      

       

       

      But I get this error:

       

      Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ThreadScopedObject with qualifiers @Default
        at injection point [BackedAnnotatedField] @Inject private com.contactlab.test.weldse.main.SimpleThreadImpl.threadScopedObject
        at com.contactlab.test.weldse.main.SimpleThreadImpl.threadScopedObject(SimpleThreadImpl.java:0)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:372)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:293)
        at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
        at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:167)
        at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:531)
        at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
        at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
      

       

       

      Moreover, I think the code

       @Inject
          private Instance<SimpleThread> threads;
      

      is not the best way to init new Runnable implementations.

       

      Maybe a later implementation sould be to use a CDI ThreadPoolExecutor

       

      Thank you in advance,

       

      Andrea