Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to start a long running background process from a Servlet?  RSS feed

 
Nipuna Gunathilake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

New user of the forum here. (Registered a while back but never posted anything )
Please let me know if I posted this on the wrong catagory.

I'm doing a Java project that needs to run a task in the background.
I'm using Jetty Embedded for this project

I'm currently using the following code to start an executor.

executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new RefreshTask(), 0, 1, TimeUnit.SECONDS);

How I can start this method manually via a call through a Servlet?
I need to pass in some parameters for runnable method as well.

A similar question was asked here: http://stackoverflow.com/a/25245786/3156644 But it's not clear how I should go about injecting the resources or start the process.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this long running process really need to be part of the JVM running the servlet engine or could it be entirely separate?

What resources does this long running process use and what kind of output does it have to create?

Bill
 
Nipuna Gunathilake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:Does this long running process really need to be part of the JVM running the servlet engine or could it be entirely separate?

What resources does this long running process use and what kind of output does it have to create?

Bill


Hey, Thank you for the reply.

The application is supposed to be in one jar file (that uses Jetty embedded). Therefor, I think it should be running in the same JVM.

The purpose of the background process is to monitor a feed of data (About every 10 minute) and check for any errors.
The output is to log any errors that are found in the said feed to a database.



 
Nipuna Gunathilake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The feed type is of GTFS-Real-time(https://developers.google.com/transit/gtfs-realtime/).
These these feed are usually accessible from a given URL such as this one - http://api.bart.gov/gtfsrt/tripupdate.aspx

So, essentially what I want to be doing is to call the URL every 10 minutes and check for errors.
This part should be done in the background.

I was thinking about using Sheduledexecutorservice for this purpose.
But I can't figure out how to trigger the service from a servlet.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuff like that (or the Quartz scheduler) are usually best started and stopped via ServletContextListener methods. On no account should anything that plays with threads be run from a servlet's service methods.

Your best bet is probably to run the monitor on schedule and have the monitor thread post the results to a thread-safe object. Servlets can then do thread-safe queries to that object in order to construct the servlet response.
 
Nipuna Gunathilake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Stuff like that (or the Quartz scheduler) are usually best started and stopped via ServletContextListener methods. On no account should anything that plays with threads be run from a servlet's service methods.

Your best bet is probably to run the monitor on schedule and have the monitor thread post the results to a thread-safe object. Servlets can then do thread-safe queries to that object in order to construct the servlet response.


The feeds to be monitored are to be entered by the user after the web application has started.
The problem I had with the ServletContextListener was whether if it was possible to pass in parameters when the application has already started.

Can I pass in the parameters for the method (the URLs, time intervals etc.) after the ServeletContext has been initialized?

This is a pretty new topic for me so please correct me if I'm completely off track.

Thanks again.
Nipuna
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you construct a process "engine" that runs long-running tasks either or schedule or on demand - or both - you can certainly pull parameters for it from some sort of request queue and/or static configuration. What you set up for such purposes is up to you.
 
Nipuna Gunathilake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the replies so far. It's been extremely helpful.

I've written some code and it seems to work fine for the purpose.
But could you please tell me if it's a usable solution?

The ScheduledExecutorService gets started on context initialized.


At this time the parameters needed to run the "RefreshTask" doesnot exist.
But the task gets refreshed every second looking for an input.

The refresh task will look a bit like this.

Even though the task runs every second, the code inside the if condition only runs if the count is changed from it's default value of zero. This can be done from a call to a servlet.

While this works I have a couple of problems with it.
1. If the interval is longer (about 10 min) and the static variable is changed one minute after the thread executes, is there any way to force run the thread again?
2. Is this good code? I know this is an objective thing but, what are the major things I'm doing wrong?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!