Background thread in Jetty

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Background thread in Jetty

Josh Spiegel
Hi,

I am using Jetty and I would like to create a background task that periodically wakes up and checks on some cached state, possibly doing some cleanup.  I see several ways to do this but I am not sure what the best practice is.  One idea is to use Jetty's scheduler service (i.e. schedule the task to wake up and have it continuously reschedule itself when it is done).  Does anybody know if this is a reasonable approach?

Thanks,
Josh

_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: Background thread in Jetty

Simone Bordet-3
Hi,
On Tue, Jun 19, 2018 at 4:35 PM Josh Spiegel <[hidden email]> wrote:
>
> Hi,
>
> I am using Jetty and I would like to create a background task that periodically wakes up and checks on some cached state, possibly doing some cleanup.  I see several ways to do this but I am not sure what the best practice is.  One idea is to use Jetty's scheduler service (i.e. schedule the task to wake up and have it continuously reschedule itself when it is done).  Does anybody know if this is a reasonable approach?

Yes it is.
It has the added benefit that its lifecycle will be bound to that of
Jetty and you don't have to write shutdown code for your own thread.

--
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support
from the Jetty & CometD experts.
_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: Background thread in Jetty

Alexander Farber
Hello,

On Mon, Jul 2, 2018 at 3:41 PM, Simone Bordet <[hidden email]> wrote:
On Tue, Jun 19, 2018 at 4:35 PM Josh Spiegel <[hidden email]> wrote:
>
> I am using Jetty and I would like to create a background task that periodically wakes up and checks on some cached state, possibly doing some cleanup.  I see several ways to do this but I am not sure what the best practice is.  One idea is to use Jetty's scheduler service (i.e. schedule the task to wake up and have it continuously reschedule itself when it is done).  Does anybody know if this is a reasonable approach?

Yes it is.
It has the added benefit that its lifecycle will be bound to that of
Jetty and you don't have to write shutdown code for your own thread.


are you discussing the 


It is difficult to find a usage example, for example for running a task every hour...

Regards
Alex
 

_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: Background thread in Jetty

Simone Bordet-3
Hi,

On Sun, Jul 22, 2018 at 5:00 PM Alexander Farber
<[hidden email]> wrote:
> are you discussing the
>
> https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/util/thread/ScheduledExecutorScheduler.html ?

Yes.

> It is difficult to find a usage example, for example for running a task every hour...

The API is very similar to that of JDK's schedulers:

Scheduler scheduler = new ScheduledExecutorScheduler();
scheduler.start();

scheduler.schedule(() -> { ... }, 1, TimeUnit.HOURS);

If it needs to be recurring, you just call schedule() again from
within the task.

--
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support
from the Jetty & CometD experts.
_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users