[weld-dev] @ThreadScoped for Weld SE

Peter Royle howardmoon at screamingcoder.com
Thu Dec 10 19:45:09 EST 2009


Hi,

I've implemented ThreadContext and @ThreadScoped (revision 5267) for Weld SE. 

Quick example:

---
public class DateFormatterProducer {

   /** Produces a thread-safe date formatter (one instance per thread).    */
   @Produces @ThreadScoped
   public SimpleDateFormat dateFormat()  {
      return new SimpleDateFormat("yyyyMMdd HHmm");
   }
}
---
public class ThreadThing implements Runnable {
   @Inject SimpleDateFormat dateFormat;
   public void run() {
	...
        dateFormat.format(date);
        ...
   }
}
---
public class MainClass {

   public void main(@Observes ContainerInitialized, Instance<ThreadThing> threadThing, WeldContainer weld) {
      Thread thread1 = new Thread(threadThing.select().get());   
      thread1.start();

      Thread thread2 = new Thread(threadThing.select().get());  
      thread2.start();

      thread1.join();
      thread2.join();

      weld.shutdown();
   }

}
--- requires this decorator to be enabled ---
<beans>
   <decorators>
      <decorator>org.jboss.weld.environment.se.threading.RunnableDecorator</decorator>
   </decorator>
</beans>

Anyone please let me know how you think this could be improved.

Thanks,

Pete R.





More information about the weld-dev mailing list