0

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.

mcraenich
  • 745
  • 1
  • 14
  • 38

1 Answers1

0

I'm still interested in some outside perspectives on this topic, however we did find a solution which looks like it'll work reasonably well. What we did was implement a 'ServletContextListener' which initiates our scheduler on context start, when the server is started, and stops the scheduler on context destroy, when the server is stopped.

We just had to add the listener as an entry in our web.xml file in the WEB-INF folder of our project:

<listener>
    <listener-class>com.mmm.marketing.utils.SchedulerServletContextListener</listener-class>
</listener>

We also wrote a web service which will allow us to stop and start the scheduler singleton manually if we like.

mcraenich
  • 745
  • 1
  • 14
  • 38