y需求: 需要一个定时任务,比如 2019-5-10 10:00 执行一次期初库存推送的功能~
Spring自带的@Schedule() 的cron表达式不支持年份,所以考虑一下quartz定时任务.
1.pom文件
<!-- 定时任务--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
2.Job
import org.qua
rtz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * 实现Job接口 * * */ public class SchedulerQuartzJob1 implements Job { private void before(){ System.out.println("任务开始执行"); } @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { before(); System.out.println("开始:"+System.currentTimeMillis()); // TODO 业务 System.out.println("quartz定时任务往这里写~~~~~~~~~~"); System.out.println("结束:"+System.currentTimeMillis()); after(); } private void after(){ System.out.println("任务开始执行"); } }
3.任务调用处理
package com.am.colo.scheduled; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import java.util.Date; //import com.yvan.quartz.job.SchedulerQuartzJob1; //import com.yvan.quartz.job.SchedulerQuartzJob2; /** * 任务调度处理 * * @author yvan */ @Configuration public class QuartzSchedulers { // 任务调度 @Autowired private Scheduler scheduler; /** * 开始执行所有任务 * 对外提供关于动态管理任务的方法 * @throws SchedulerException */ public void startJob() throws SchedulerException { startJob1(scheduler); //startJob2(scheduler); scheduler.start(); } /** * 获取Job信息 * * @param name * @param group * @return * @throws SchedulerException */ public String getJobInfo(String name, String group) throws SchedulerException { TriggerKey triggerKey = new TriggerKey(name, group); CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey); return String.format("time:%s,state:%s", cronTrigger.getCronExpression(), scheduler.getTriggerState(triggerKey).name()); } /** * 修改某个任务的执行时间 * * @param name * @param group * @param time * @return * @throws SchedulerException */ public boolean modifyJob(String name, String group, String time) throws SchedulerException { Date date = null; TriggerKey triggerKey = new TriggerKey(name, group); CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey); String oldTime = cronTrigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time); CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group) .withSchedule(cronScheduleBuilder).build(); date = scheduler.rescheduleJob(triggerKey, trigger); } return date != null; } /** * 暂停所有任务 * * @throws SchedulerException */ public void pauseAllJob() throws SchedulerException { scheduler.pauseAll(); } /** * 暂停某个任务 * * @param name * @param group * @throws SchedulerException */ public void pauseJob(String name, String group) throws SchedulerException { JobKey jobKey = new JobKey(name, group); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) return; scheduler.pauseJob(jobKey); } /** * 恢复所有任务 * * @throws SchedulerException */ public void resumeAllJob() throws SchedulerException { scheduler.resumeAll(); } /** * 恢复某个任务 * * @param name * @param group * @throws SchedulerException */ public void resumeJob(String name, String group) throws SchedulerException { JobKey jobKey = new JobKey(name, group); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) return; scheduler.resumeJob(jobKey); } /** * 删除某个任务 * * @param name * @param group * @throws SchedulerException */ public void deleteJob(String name, String group) throws SchedulerException { JobKey jobKey = new JobKey(name, group); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) return; scheduler.deleteJob(jobKey); } /** * @Param: * @Description: 实例化Job,将任务触发器加入任务调度中 * @Author: zyf 2019/5/13 */ private void startJob1(Scheduler scheduler) throws SchedulerException { String job1Cron = "0 55 9 13 5 ? 2019"; // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例 // JobDetail 是具体Job实例 JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob1.class).withIdentity("job1", "group1").build(); // 基于表达式构建触发器 // CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?"); CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job1Cron); // CronTrigger表达式触发器 继承于Trigger // TriggerBuilder 用于构建触发器实例 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1") .withSchedule(cronScheduleBuilder).build(); scheduler.scheduleJob(jobDetail, cronTrigger); } /* private void startJob2(Scheduler scheduler) throws SchedulerException { JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob2.class).withIdentity("job2", "group2").build(); CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?"); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2") .withSchedule(cronScheduleBuilder).build(); scheduler.scheduleJob(jobDetail, cronTrigger); }*/ }
4. 项目启动测试:
参考出处 : https://blog.csdn.net/upxiaofeng/article/details/79415108