My team has created a web application which we hope to associate with a task scheduler which will send out e-mails once a day if any of the projects being run through the web application are running behind schedule.
The scheduler library we're using, Cron4j, is simple enough, and at the present time we're attempting to manage it through a set of web service calls, something like this (the idea being we start the scheduler and it runs for the specified time or until we stop it):
public SchedulerResource(){
s = new Scheduler();
}
@GET
@Path("start")
@Produces(MediaType.APPLICATION_JSON)
public Response invoke_workback_communicator() throws MalformedURLException {
s.schedule("* * * * *", new Runnable() {
public void run() {
WorkbackCommunicator communicator = new WorkbackCommunicator();
communicator.run();
}
});
s.start();
try {
Thread.sleep(1000L * 60L * 2L);
} catch (InterruptedException e) {
;
}
s.stop();
return Response.noContent().build();
}
@GET
@Path("stop")
@Produces(MediaType.APPLICATION_JSON)
public Response stop_workback_communicator() throws MalformedURLException {
s.stop();
return Response.noContent().build();
}
At the present time the 'invoke_workback' method works fine, the 'stop_workback' method does not work.
What I'm curious about is:
- is invoking a scheduler in a web service like this bad practice?
- if so, how would one effectively accomplish this functionality?
- if not, how can I could I create a service interface which manages my scheduler instance? Is this even necessary?
EDIT:
Have looked further into the problem and I noticed that there is something called a 'ServletContextListener' which can be used to start or kill the scheduler on server start up and shut down. So what I'm thinking at this point is to test out that functionality, and keep our service which will allow us to manage the scheduler when the app is still running.