Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Job Scheduling using Spring 3

I have developed a web crawler which crawls with Start URL as seed parameter. I want to allow users to schedule this task in terms of a Job if possible.

Currently I am using Spring 3.1.2 and Hibernate. I need to give users a front end which receives cronJob Parameters and based on that I want to run the crawler. Is it possible to do it using spring.

I read a bit about Quartz but the articles on SO or other websites are not at all clear or they are not complete in order to fully understand how to implement a scheduler in spring.

I know basics that there are three components to it

  1. SchedulerFacotry
  2. Trigger
  3. Job (Service to Run)

I hope someone could guide me in correct direction.

like image 705
Dhruvenkumar Shah Avatar asked Aug 28 '12 15:08

Dhruvenkumar Shah


1 Answers

Quartz scheduler is just the right tool for the job. For some reason almost all tutorials focus on defining jobs at startup, in XML - while Quartz is fully capable of (re-|un-) scheduling jobs at runtime.

You can and should takr advantage of Spring to start the Quartz Scheduler, but then you can interact with it directly from your code. Here is a simple example from the documentation:

JobDetail job = newJob(SimpleJob.class)
    .withIdentity("job1", "group1")
    .build();

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();

scheduler.scheduleJob(job, trigger);

Here you define a job (piece of Java code to run), trigger (when to run it, user can supply any valid CRON expression) and wrap it all by scheduling it. The scheduler instance can be injected by Spring. Spring will also handle proper shutdown.

like image 135
Tomasz Nurkiewicz Avatar answered Sep 20 '22 09:09

Tomasz Nurkiewicz