从休止端点停止调度作业

时间:2017-10-31 15:05:54

标签: spring-boot job-scheduling spring-scheduled

我正在做一个Spring Boot Project

这是主要的课程

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

}

这是控制器

@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) {
    }   

}

这是我从控制器调用的类作为预定作业

@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);       
    }
}

如何通过休息端点停止计划作业(假设&#34; / stop / {jobname}&#34;)..当我使用&#34; / start / {jobname}启动作业时&#34;休息终点?

1 个答案:

答案 0 :(得分:1)

您可能需要使用quartz调度程序(如果尚未使用),并使用所需方法添加服务,然后将该服务注入您的控制器。

这里有一个不错的例子: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;
}