2 changed files with 51 additions and 0 deletions
@ -0,0 +1,39 @@ |
|||||
|
package cn.chjyj.szwh.task; |
||||
|
|
||||
|
import cn.chjyj.szwh.job.SzwhJob; |
||||
|
import org.quartz.*; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.ApplicationArguments; |
||||
|
import org.springframework.boot.ApplicationRunner; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 定时任务任务配置 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class TaskInit implements ApplicationRunner { |
||||
|
private static String sid="SZWH_Schedule"; |
||||
|
|
||||
|
@Autowired |
||||
|
private Scheduler scheduler; |
||||
|
|
||||
|
@Override |
||||
|
public void run(ApplicationArguments args) throws Exception { |
||||
|
// 任务细节
|
||||
|
JobDetail jobDetail = JobBuilder.newJob(SzwhJob.class) |
||||
|
.withIdentity(sid + " 01") |
||||
|
.storeDurably() |
||||
|
.build(); |
||||
|
// cron 构建
|
||||
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ? *"); |
||||
|
// 创建任务触发器
|
||||
|
Trigger trigger = TriggerBuilder.newTrigger() |
||||
|
.forJob(jobDetail) |
||||
|
.withIdentity(sid + " 01Trigger") |
||||
|
.withSchedule(scheduleBuilder) |
||||
|
.startNow() //立即執行一次任務
|
||||
|
.build(); |
||||
|
// 手动将触发器与任务绑定到调度器内
|
||||
|
scheduler.scheduleJob(jobDetail, trigger); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue