#8. quartz-demo
package com.jordyn.quartz; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; /** * @description * @date 2019-5-27 */ public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobDetail实例,将该实例与HelloJob Class绑定 JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("myJob").build(); /*//创建一个Trigger触发器的实例,定义该job立即执行,并且每2秒执行一次,一直执行 SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger").startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build(); */ //cronTrigger 每日9点40触发任务 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 28 10 * * ?")).build(); //创建schedule实例 StdSchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler(); scheduler.start(); scheduler.scheduleJob(job,cronTrigger); } }
package com.jordyn.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import java.time.LocalDateTime; /** * @description * @date 2019-5-27 */ public class HelloJob implements Job{ public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException{ //打印当前的时间 System.out.println("现在时间是:" + LocalDateTime.now()); //具体的业务逻辑 System.out.println("开始生成任务报表"); } }
<dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency> </dependencies>
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦