I did something similar some time before. https://gist.github.com/dmitrygusev/6669065
My idea was based on multiple Workers and a single Task entity. Every worker can perform its own kind of tasks. Tasks stored in a single database table, they all have the same attributes like status, start date, etc. and also "parameters" attribute stored in a string column, where params were serialized in the form of URL. Every worker can perform one task at a time in its run() method. There is AbstractWorker class, its run() method starts from querying the database for new tasks. If there is no tasks -- it will waits on a lock (implemented using BlockingQueue). When found new task -- it calls its abstract runTask() method. After worker finished performing a task it exits the run() method. There are multiple implementations of Worker classes, like: GitPullWorker, GitCloneWorker, etc. There is a WorkerRoutine class that runs infinite loop and calls for a worker instances and handles exceptions. Each WorkerRoutine instance running in its own thread using parallel executor. You submit new task by TaskManager.submitTask() You may notify workers that new task appeared by calling WorkerManager.taskSubmitted() I hope I could explain it better, but I think you can look at the code yourself and maybe find something useful for your needs. I also hope that one day I can convert it to reusable stuff and just open source it, but right now its very coupled with the project its used in. PS: Just found one bug: PerthreadManager.cleanup() should also be invoked from AbstractWorker.cleanup(). PPS: Also I'd recommend looking at some queueing solutions if you have a need in this stuff, like rabbitmq, or similar, and maybe integrate tapestry with them. I'd implemented such integration if I new about these solutions before I started implementing my workers. Though, my solution has some advantages for my project, its still not well tested and not really reusable in its current implementation. On Mon, Sep 23, 2013 at 2:29 PM, Martin Kersten <martin.kersten...@gmail.com > wrote: > I have seen no magic in the parallelExecutor. It just passes a runnable / > Callable / Invokable to the worker queue and the worker simply calls its > run method. So there is nothing like dependency injection going on here. > > I might use parallelExecutor to execute my processor, I am not sure. I can > create a service / object using the registery and get constructor and > private field injection working. But that is not enough unless I make my > inner worker job a PerThread service and use the registery to optain it > inside the parallel task. I will check this out. > > But if you know a way I can specify a method to invoke and the > dependencies are injected automatically (and I dont have to write it myself > using the registery to look up for services that apply to the parameters). > - Well I can do this but hey this is a IOC. It should be capable of doing > so. > > > 2013/9/23 Lance Java <lance.j...@googlemail.com> > > > Is there any reason why you can't just @Inject Session and start using it > > like a normal tapestry page or service? > > > > The only difference being that you need to @Inject PerThreadManager and > > call cleanup() in the rare case that you are NOT inside a tapestry > request > > / response AND NOT using ParallelExecutor / PeriodicExecutor. > > On 22 Sep 2013 22:38, "Martin Kersten" <martin.kersten...@gmail.com> > > wrote: > > > > > @Lance > > > Using Hibernate SessionSource will just create a new session with no > > thread > > > local awareness. Is there another service that gives me a per thread > > > session? Would be nice to have a service that automatically gives the > > > session of the current thread? Especially if the thread local thingy is > > > working outside of the page processing? > > > > > > @Barry > > > I just managed to find out that my other service is also PerThread > Scope > > so > > > I need support for this. I will check out if I can get the IOC to work > > > outside the page processing and have support for perThread scope. > > > > > > I will also try out a resteasy tapestry page to inject the service and > > > compose it as PerThread scope. > > > But I will use a simple worker thread to trigger the page call. Cron > jobs > > > are good but I need the possibility to end the waiting phase and issue > > the > > > processing of tasks instantly. > > > > > > > > > 2013/9/22 Barry Books <trs...@gmail.com> > > > > > > > It's much easier to just create a page at let Tapestry handle the > > > > threading. That's what it's built to do. > > > > > > > > You can just add synchronized to method if you only want one > invocation > > > at > > > > a time. > > > > > > > > If you don't want Hudson. Then I'd create a cron service that takes a > > > > configuration of times/urls and calls the pages. That would make it > > easy > > > to > > > > work either way. I have used Tapestry scheduling but found it's much > > > better > > > > to to have external control over running tasks. I think someone said > > > > > > > > Any sufficiently complicated application contains an ad hoc, > > > > informally-specified, bug-ridden < > > > > http://en.wikipedia.org/wiki/Computer_bug>, > > > > slow implementation of half of Hudson. > > > > > > > > > > > > On Sun, Sep 22, 2013 at 4:53 AM, Martin Kersten < > > > > martin.kersten...@gmail.com > > > > > wrote: > > > > > > > > > Thanks Lance. This cleanup advise was what i was looking for. > Cheers. > > > > > > > > > > > > > > > 2013/9/22 Lance Java <lance.j...@googlemail.com> > > > > > > > > > > > Igor has written a blog about scheduling jobs with tapestry here > > > > > > > > > > > > > > > > > > > > > > > > > > > http://blog.tapestry5.de/index.php/2011/09/18/scheduling-jobs-with-tapestry/ > > > > > > > > > > > > The hibernate session provided by tapestry is a singleton and can > > be > > > > > > injected as any other service. The singleton is a proxy to a > > > per-thread > > > > > > instance which is created on demand and cleaned up by > > > > > > PerThreadManager.cleanup(). > > > > > > If you use the PeriodicExecutor or the ParallelExecutor then the > > (per > > > > > > thread) hibernate session will be cleaned up after your job runs. > > If > > > > you > > > > > > are not using these services (ie you are using > > java.util.concurrent.* > > > > > > directly) then you will need to call either > > > PerThreadManager.cleanup() > > > > or > > > > > > Registry.cleanupThread() explicitly to close the hibernate > session. > > > > > > > > > > > > > > > > > > > > > > > > On 22 September 2013 08:12, Martin Kersten < > > > > martin.kersten...@gmail.com > > > > > > >wrote: > > > > > > > > > > > > > :) I know Barry. I marked your former post about this. But I > dont > > > > want > > > > > a > > > > > > > page right now. > > > > > > > > > > > > > > But this calling it directly ... well that is a good one. But > > > > > > object.notify > > > > > > > is also easy and makes it possible to assume only one > invocation > > of > > > > the > > > > > > > processor is running once at a time per JVM. > > > > > > > > > > > > > > But sadly making the process a singleton I have again the > > Hibernate > > > > > > Session > > > > > > > stuff. > > > > > > > > > > > > > > > > > > > > > 2013/9/21 Barry Books <trs...@gmail.com> > > > > > > > > > > > > > > > Here is what I do: > > > > > > > > > > > > > > > > 1. Write a simple service that just performs the action you > > want > > > > > > > > 2. If you need real time processing just call it. > > > > > > > > 3. Create a page that just calls the service and schedule > > > accessing > > > > > > that > > > > > > > > page with Hudson/curl > > > > > > > > > > > > > > > > > > > > > > > > On Sat, Sep 21, 2013 at 2:41 PM, Martin Kersten < > > > > > > > > martin.kersten...@gmail.com > > > > > > > > > wrote: > > > > > > > > > > > > > > > > > Hi there, > > > > > > > > > > > > > > > > > > > > > > > > > > > I need to implement a service that reads tasks > > > (descriptions) > > > > > from > > > > > > > the > > > > > > > > > database, does some tasks and sleeps again. The thread must > > be > > > > able > > > > > > to > > > > > > > > woke > > > > > > > > > up if an other service demands just in time processing. > > > > > > > > > > > > > > > > > > Requirements: > > > > > > > > > 1. Need a Hibernate Session inside the main loop. > > > > > > > > > 2. Needs to be able to woke up (just use Object.notify and > > > > > > > Object.wait). > > > > > > > > > 3. Needs to sleep for a couple of minutes, check db for > work > > > and > > > > > > sleep > > > > > > > > > again. > > > > > > > > > 4. On shut down it needs to suspend and decompose > gracefully. > > > > > > > > > What is the best way to do so? > > > > > > > > > > > > > > > > > > So first I looked at periodic job etc. Nothing to use. So > it > > > ends > > > > > up > > > > > > > > doing > > > > > > > > > some kind of a > > > > > > > > > service that spawns a thread and the thread does all the > > > > > progressing. > > > > > > > > > > > > > > > > > > The thread itself uses a runnable to guard against failures > > and > > > > > those > > > > > > > > > failures are logged > > > > > > > > > within each task during which the failure occures. > > > > > > > > > > > > > > > > > > So here comes the big question: > > > > > > > > > > > > > > > > > > What should I do. > > > > > > > > > > > > > > > > > > The naive answer is using a SessionSource and create a > > session > > > > each > > > > > > > time > > > > > > > > > the thread's > > > > > > > > > runnable starts the processing. > > > > > > > > > > > > > > > > > > Another idea would be set up the worker part as a service > > that > > > is > > > > > > > created > > > > > > > > > every time and > > > > > > > > > let the IOC do all the session creation and handling. But I > > > fear > > > > > that > > > > > > > > this > > > > > > > > > is way more > > > > > > > > > complicated then the SessionSource idea. > > > > > > > > > > > > > > > > > > The decomposition on the teardown of the tapestry > application > > > > > > requires > > > > > > > to > > > > > > > > > deal with > > > > > > > > > certain kind of listeners. What is the best service to add > > the > > > > > > listener > > > > > > > > > too? > > > > > > > > > > > > > > > > > > > > > > > > > > > Thanks in advance, > > > > > > > > > > > > > > > > > > Martin (Kersten) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- Dmitry Gusev AnjLab Team http://anjlab.com