概述
Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.5.4</version>
</dependency>
Job
public class Job1 extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
StringJoiner outStr=new StringJoiner(" | ")
.add("Job1")
.add(Thread.currentThread().getName())
.add(context.getTrigger().getKey().getName());
System.out.println(outStr);
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
String somethingToUse = jobDataMap.getString("somethingToUse");
System.out.println(somethingToUse);
}
JobService
@Service
public class JobService {
@Autowired
Scheduler scheduler;
static TriggerKey task1 = new TriggerKey("task1");
public void startOrResumeJob(String somethingToUse) throws SchedulerException {
if (scheduler.getTriggerState(task1).toString()=="PAUSED") {
scheduler.resumeTrigger(task1);
}else{
JobDetail jobDetail = JobBuilder
.newJob(Job1.class)
.usingJobData("somethingToUse",somethingToUse)
.build();
Trigger trigger= TriggerBuilder.newTrigger()
.withIdentity(task1)
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
scheduler.scheduleJob(jobDetail,trigger);
}
}
public void pauseJob() throws SchedulerException{
scheduler.pauseTrigger(task1);
}
}
Controller
@RestController
@RequestMapping("job")
public class JobController {
@Autowired
JobService jobService;
@GetMapping("start")
public String start(String somethingToUse){
try {
jobService.startOrResumeJob(somethingToUse);
} catch (SchedulerException e) {
e.printStackTrace();
return "fail";
} return "SUCCESS";
}
@RequestMapping("pause")
public String pause(){
try {
jobService.pauseJob();
} catch (SchedulerException e) {
e.printStackTrace();
return "fail";
} return "succesToPause";
}
}
最后
以上就是强健荔枝为你收集整理的springboot整合quartz实现手动控制任务启动,暂停,重启的全部内容,希望文章能够帮你解决springboot整合quartz实现手动控制任务启动,暂停,重启所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复