Spring quatz优雅的实现

142 阅读1分钟

Spring quatz优雅的实现

public class RefSppCompanySyncJob implements Job {
    private static Logger log = LogManager.getLogger(RefSppCompanySyncJob.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {


    }
   


}
public class QuartzSchedulerListener implements ServletContextListener {

    public static final String EGIS_GROUP = "egis";
    public static final String EGIS_SPP_COMPANY_JOB = "sppCompanyJob";
    public static final String EGIS_SPP_COMPANY_JOB_TRIGGER = "sppCompanyJobTrigger";
    private Scheduler schedulerSppCompany;



    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        JobDetail egis_spp_company_job = JobBuilder.newJob(RefSppCompanySyncJob.class)
              .withIdentity(EGIS_SPP_COMPANY_JOB, EGIS_GROUP).build();
  
        Trigger egis_spp_company_trigger = TriggerBuilder
                       .newTrigger()
                       .withIdentity(EGIS_SPP_COMPANY_JOB_TRIGGER, EGIS_GROUP)
                       .withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 * * * ?")) 
                       .repeatForever())
                       .build();

        schedulerSppCompany = new StdSchedulerFactory().getScheduler();
        schedulerSppCompany.start();
        schedulerSppCompany.scheduleJob(egis_spp_company_job,egis_spp_company_trigger);

   }

@Override
   public void contextDestroyed(ServletContextEvent arg0) {
      try {
         schedulerSppCompany.unscheduleJob(new TriggerKey(EGIS_SPP_COMPANY_JOB_TRIGGER, EGIS_GROUP));
         schedulerSppCompany.shutdown();
      } catch (SchedulerException e) {
         e.printStackTrace();
      }
   }

}