You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Martin Kersten <ma...@gmail.com> on 2013/09/21 21:41:54 UTC

How to best construct and express this service?

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)

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
It was appearently easy. Registry + PerThreadManager. The only thing I need
to learn now is how to invoke a method using the IOC.


2013/9/23 Martin Kersten <ma...@gmail.com>

> @Lance
> SessionSource is not perThread but HibernateManager is. And on
> instanciation the HibernateManager creates the session object. And the
> Session is retrieved by calling HibernateManager.getSession(). So
> SessionSource is global and you can create many sessions per thread but
> HibernateManager is thread aware and holds only one session object.
>
> So the only thing left to do is learn how to trigger a construction of my
> service or hibernate manager service in my own thread. I guess this is in
> the user guide somewhere.
>
>
> 2013/9/22 Martin Kersten <ma...@gmail.com>
>
>> @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 <tr...@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.mk@gmail.com
>>> > wrote:
>>>
>>> > Thanks Lance. This cleanup advise was what i was looking for. Cheers.
>>> >
>>> >
>>> > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
>>> > > > > >
>>> > > > >
>>> > > >
>>> > >
>>> >
>>>
>>
>>
>

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
@Lance
SessionSource is not perThread but HibernateManager is. And on
instanciation the HibernateManager creates the session object. And the
Session is retrieved by calling HibernateManager.getSession(). So
SessionSource is global and you can create many sessions per thread but
HibernateManager is thread aware and holds only one session object.

So the only thing left to do is learn how to trigger a construction of my
service or hibernate manager service in my own thread. I guess this is in
the user guide somewhere.


2013/9/22 Martin Kersten <ma...@gmail.com>

> @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 <tr...@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.mk@gmail.com
>> > wrote:
>>
>> > Thanks Lance. This cleanup advise was what i was looking for. Cheers.
>> >
>> >
>> > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
>> > > > > >
>> > > > >
>> > > >
>> > >
>> >
>>
>
>

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
Hi Dmitry,

  Thanks for the info, i will check it out. My service is just for mail
delivery. It use a simple timestamp for task aquire and timeout. Exceptions
are stored in the task entity and that it. The task itself Speed the Name
oft the message template and a json model to process it.

In that Ende i just want a job API that combines all that auto inject and
autoinvoke capabilities oft lets say a page. That is it and i dont want to
contribute object provider also.

I have that feeling that this is missing in the picture. Shouldnt be hard
to implement also.
Am 23.09.2013 13:35 schrieb "Dmitry Gusev" <dm...@gmail.com>:

> 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.mk@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 <la...@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" <ma...@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 <tr...@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.mk@gmail.com
> > > > > > wrote:
> > > > >
> > > > > > Thanks Lance. This cleanup advise was what i was looking for.
> > Cheers.
> > > > > >
> > > > > >
> > > > > > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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
>

Re: How to best construct and express this service?

Posted by Dmitry Gusev <dm...@gmail.com>.
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.mk@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 <la...@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" <ma...@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 <tr...@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.mk@gmail.com
> > > > > wrote:
> > > >
> > > > > Thanks Lance. This cleanup advise was what i was looking for.
> Cheers.
> > > > >
> > > > >
> > > > > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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

Re: How to best construct and express this service?

Posted by Lance Java <la...@googlemail.com>.
Assuming MyTask implements Runnable and takes a Session in it's
constructor:

Either
   @Startup
   public static void scheduleMyJob(PeriodicExecutor executor, Session
hibernateSession) {
       Runnable mytask = new MyTask(hibernateSession);
       executor.addJob(new IntervalSchedule(1000000L), "Do Stuff", myTask);
   }

Or
   @Startup
   public static void scheduleMyJob(PeriodicExecutor executor, @Autobuild
MyTask myTask) {
       executor.addJob(new IntervalSchedule(1000000L), "Do Stuff", myTask);
   }

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
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 <la...@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" <ma...@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 <tr...@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.mk@gmail.com
> > > > wrote:
> > >
> > > > Thanks Lance. This cleanup advise was what i was looking for. Cheers.
> > > >
> > > >
> > > > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: How to best construct and express this service?

Posted by Lance Java <la...@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" <ma...@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 <tr...@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.mk@gmail.com
> > > wrote:
> >
> > > Thanks Lance. This cleanup advise was what i was looking for. Cheers.
> > >
> > >
> > > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
@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 <tr...@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.mk@gmail.com
> > wrote:
>
> > Thanks Lance. This cleanup advise was what i was looking for. Cheers.
> >
> >
> > 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
> > > > > >
> > > > >
> > > >
> > >
> >
>

Re: How to best construct and express this service?

Posted by Barry Books <tr...@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.mk@gmail.com
> wrote:

> Thanks Lance. This cleanup advise was what i was looking for. Cheers.
>
>
> 2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
> > > > >
> > > >
> > >
> >
>

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
Thanks Lance. This cleanup advise was what i was looking for. Cheers.


2013/9/22 Lance Java <la...@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.mk@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 <tr...@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.mk@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)
> > > >
> > >
> >
>

Re: How to best construct and express this service?

Posted by Lance Java <la...@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 <ma...@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 <tr...@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.mk@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)
> > >
> >
>

Re: How to best construct and express this service?

Posted by Martin Kersten <ma...@gmail.com>.
:) 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 <tr...@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.mk@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)
> >
>

Re: How to best construct and express this service?

Posted by Barry Books <tr...@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.mk@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)
>