java - Correct way to persist Quartz triggers in database -


i'm quite new quartz , need schedule jobs in spring web application.

i know spring + quartz integration (i'm using spring v 3.1.1) i'm wondering if right way follow.

in particular i need persist scheduled tasks in db can re-initialize them when application restarted.

are there utilities provided spring scheduling wrapper this? can suggest me "well known" approach follow?

here 1 way handle scenario.

first in spring configuration specify schedulerfactorybean can inject scheduler other beans.

<bean name="schedulerfactory"     class="org.springframework.scheduling.quartz.schedulerfactorybean">     <property name="applicationcontextschedulercontextkey">         <value>applicationcontext</value>     </property> </bean> 

then when create job in application store details of job in database. service called 1 of controllers , schedules job:

@component public class followjobservice {      @autowired     private followjobrepository followjobrepository;      @autowired     scheduler scheduler;      @autowired     listablebeanfactory beanfactory;      @autowired     jobschedulerlocator locator;      public followjob findbyclient(client client){         return followjobrepository.findbyclient(client);     }      public void saveandschedule(followjob job) {         job.setjobtype(jobtype.follow_job);         job.setcreateddt(new date());         job.setisenabled(true);         job.setiscompleted(false);          jobcontext context = new jobcontext(beanfactory, scheduler, locator, job);         job.setquartzgroup(context.getquartzgroup());         job.setquartzname(context.getquartzname());          followjobrepository.save(job);          jobschedulerutil.schedule(new jobcontext(beanfactory, scheduler, locator, job));     } } 

the jobcontext build contains detail job , passed utility scheduling jobs. here code utility method schedules job. notice in service autowire jobscheduler , pass jobcontext. notice store job in database using repository.

/**  * schedules data_mining_job client. job attempt enter  * followers of target database.  */ @override public void schedule(jobcontext context) {     client client = context.getnetworksociallyjob().getclient();     this.logscheduleattempt(context, client);      jobdetail jobdetails = jobbuilder.newjob(this.getjobclass()).withidentity(context.getquartzname(), context.getquartzgroup()).build();     jobdetails.getjobdatamap().put("job", context.getnetworksociallyjob());     jobdetails.getjobdatamap().put("repositories", context.getrepositories());      trigger trigger = triggerbuilder.newtrigger().withidentity(context.getquartzname() + "-trigger", context.getquartzgroup())             .withschedule(cronschedule(this.getschedule())).build();      try {         context.getscheduler().schedulejob(jobdetails, trigger);                     this.logsuccess(context, client);      } catch (schedulerexception e) {         this.logfailure(context, client);         e.printstacktrace();     } } 

so after of code executes 2 things have happened, job store in database , been scheduled using quartz scheduler. if application restarts want reschedule jobs scheduler. register bean implements applicationlistener<contextrefreshedevent> called spring each time container restarts or started.

<bean id="jobinitializer" class="com.network.socially.web.jobs.jobinitializer"/> 

jobinitializer.class

public class jobinitializer implements applicationlistener<contextrefreshedevent> {      logger logger = loggerfactory.getlogger(jobinitializer.class);      @autowired     dataminingjobrepository repository;      @autowired     applicationjobrepository jobrepository;      @autowired     scheduler scheduler;      @autowired     jobschedulerlocator locator;      @autowired     listablebeanfactory beanfactory;      @override     public void onapplicationevent(contextrefreshedevent event) {         logger.info("job initilizer started.");          //todo: modify call pull completed & enabled jobs         (applicationjob applicationjob : jobrepository.findall()) {             if (applicationjob.getisenabled() && (applicationjob.getiscompleted() == null || !applicationjob.getiscompleted())) {                 jobschedulerutil.schedule(new jobcontext(beanfactory, scheduler, locator, applicationjob));             }         }            }  } 

this class autowires scheduler , repository grabs instances of each of jobs implement applicationjob interface. using information these database records can use scheduler utility reconstruct jobs.

so manually store jobs in database , manually schedule them injecting instance of scheduler in appropriate beans. rescheduled them, query database , schedule them using applicationlistener account restarts , starts of container.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -