Hi all,

at first, CDI + CODI is really cool stuff for building a webapp.

1) Our Environment:
We are using OWB 1.1.3, CODI 1.0.2 on a Tomcat 7 environment with CODI 
@Transactional.

Database Producer:
 @PersistenceContext(unitName = "myunit")
 private EntityManager extendedEntityManager;

 @Produces
 @ConversationScoped
 @MyQualifier
 public ExtendedEntityManager createExtendedEntityManager() {
  return new ExtendedEntityManager(this.extendedEntityManager);
 }

 public void dispose(@Disposes @Siemens ExtendedEntityManager 
extendedEntityManager) {
  if (extendedEntityManager.isOpen()) {
   extendedEntityManager.close();
  }
 }

-DatabaseService which gets above EntityManager injected
-OtherServices which getting the database Service injected


2) The Problem:
Using Quartz, Seam Cron or the following class, which creates a thread, leads 
into the same problem.

@ApplicationScoped
public class ApplicationTimer {

 @Inject
 private Log log;

 @Inject
 private OtherService otherService; // gets databaseService injected, 
databaseService gets EntityManager injected

 public void init() {
  log.info("initialize timer thread");
  try {
   otherService.doSomething(); // WORKS in Context of this ApplicationScoped 
bean
  } catch (Exception e) {
   log.error(e);
  }

  Thread timer = new Thread() {
   public void run() {
    while (true) {
     log.info("That's the thread");
     try {
      otherService.doSomething(); // EXCEPTION occurs in Child Thread
      Thread.sleep(5000);
     } catch (Exception e) {
      log.error(e);
     }
    }
   }
  };
  timer.start();
 }
}

An exception
javax.enterprise.context.ContextNotActiveException: WebBeans context with scope 
type annotation @ConversationScoped does not exist within current thread
occurs

We also tried to start a (javax) Conversation. Leads into the same Excpetion 
with ... a @RequestScoped does not exist.

The child thread is not in context of a web request, we guess the EntityManager 
have to (as descriped in CODI doc).

But how to solve this problem?
Is there a possibility to create/provide an appropriate context to the child 
thread or to start scheduled services in another way?


Best Regards
Erwin

Reply via email to