gpt4 book ai didi

spring-boot - 从休息端点停止调度作业

转载 作者:行者123 更新时间:2023-12-04 03:13:20 33 4
gpt4 key购买 nike

我正在做一个 Spring Boot 项目

这是主要类(class)

@SpringBootApplication
@ComponentScan(basePackages="blabla.quartz")
@EnableScheduling
public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =SpringApplication.run(App.class, args);
}

}

这是 Controller
@RestController
public class Controller {

@Autowired
private SampleTask m_sampletask;

@Autowired TaskScheduler taskScheduler;

ScheduledFuture scheduledFuture;
int jobid=0;

@RequestMapping(value = "start/{job}", method = RequestMethod.GET)
public void start(@PathVariable String job) throws Exception {
m_sampletask.addJob(job);

Trigger trigger = new Trigger(){

@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
org.quartz.CronExpression cronExp=null;
CronSequenceGenerator generator = new CronSequenceGenerator("0 * * ? * *");
Date nextExecutionDate = generator.next(new Date());
System.out.println(nextExecutionDate);
return nextExecutionDate;

}

};
scheduledFuture = taskScheduler.schedule(m_sampletask, trigger);

}

}

这是 ScheduleConfigurer 的实现
@Service
public class MyTask implements SchedulingConfigurer{

@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setThreadNamePrefix("somegroup-");
scheduler.setPoolSize(10);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
scheduler.setAwaitTerminationSeconds(20);
return scheduler;
}

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
}

}

这是我作为预定作业从 Controller 调用的类
@Component
public class SampleTask implements Runnable{

private List<String> jobs=new ArrayList<String>();
private String jobName;

public void addJob(String job){
jobName=job;
}

@Override
public void run() {
System.out.println("Currently running "+jobName);
}
}

如何通过休息端点停止计划作业(假设“/stop/{jobname}”)。当我使用“/start/{jobname}”休息端点开始作业时?

最佳答案

您可能需要使用 quartz 调度程序(如果还没有),并使用所需的方法添加服务,然后将该服务注入(inject)您的 Controller 。

这里有一个不错的例子:https://github.com/javabypatel/spring-boot-quartz-demo

如果您想要内存中的作业存储(不是数据库),请查看 RAMJobStore:http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigRAMJobStore.html

停止示例

这是演示项目的摘录。归功于 Jayesh Patel:https://github.com/javabypatel

/**
* Stop a job
*/
@Override
public boolean stopJob(String jobName) {
System.out.println("JobServiceImpl.stopJob()");
try{
String jobKey = jobName;
String groupKey = "SampleGroup";

Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jkey = new JobKey(jobKey, groupKey);

return scheduler.interrupt(jkey);

} catch (SchedulerException e) {
System.out.println("SchedulerException while stopping job. error message :"+e.getMessage());
e.printStackTrace();
}
return false;
}

关于spring-boot - 从休息端点停止调度作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47038583/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com