2019独角兽企业重金招聘Python工程师标准>>>
本片文章是对上一篇《SpringBoot整合Quartz》的升级。
创建SpringBoot项目参考:https://my.oschina.net/u/2289161/blog/865479
添加Quartz依赖,配置pom文件
创建QuartzConfig配置类
@Configuration
创建job
public class QuartzConfig {@Beanpublic SchedulerFactoryBean schedulerFactoryBean(){SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();return schedulerFactoryBean;}@Beanpublic Scheduler scheduler(){return schedulerFactoryBean().getScheduler();}
}
public class CloseTradeJob implements Job {@Overridepublic void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {System.out.println("测试job = " + new Date());}
创建Task
}
@Service
测试
public class MyTask {@Autowiredpublic Scheduler scheduler;@PostConstruct //等同于init-methodpublic void init(){//定义jobdetailJobDetail jobDetail = JobBuilder.newJob(CloseTradeJob.class).withIdentity("close1", "closeTrade").build();//定义triggerSimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "closeTrade").startNow().withSchedule(simpleScheduleBuilder).build();//启动jobtry {scheduler.scheduleJob(jobDetail, trigger);} catch (SchedulerException e) {e.printStackTrace();}}
}