You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Matthias <th...@gmail.com> on 2014/02/06 13:13:48 UTC

ScheduledExecutorService in tapestry ioc service

Hi, I need to write a service where jobs can be added and should 
executed after a given delay. After execution they are finished, so 
periodical execution is not required. I prefer to user the 
ScheduledExecutorService for this task.

The question is now, how do I use my @Inject objects in the thread? Just 
passing them is not working (hibernate session is invalid). I read some 
stuff like http://wiki.apache.org/tapestry/Tapestry5HowToRunTaskInThread 
but it does'nt help me because I don't invoke the thread myself, the 
scheduler is doing it. Also the other tapestry classes like the 
ParallelExecutor or PeriodicExecutor doesn't seem to fit.

Any suggestions?

@Scope(ScopeConstants.DEFAULT)
public class MyServiceImpl implements MyService {

     @Inject
     MyDAO myDAO;

     private ScheduledExecutorService scheduler = 
Executors.newScheduledThreadPool(4);

     public void addJob(Job job, long executeDelay) {
          scheduler.schedule(new JobTask(job, myDAO), executeDelay);
     }

     public static class JobTask implements Runnable {
         // some class members
         public JobTask(MyDAO myDAO, Job job) {
             this.myDAO = myDAO;
             this.job = job;
         }

         @Override
         public void run() {
           // ...
           Object result = myDAO.getSomethingFromJob(job);
           // ...
         }
      }
}

Thanks, Matthias