如何手动触发计划任务

时间:2017-05-02 15:31:41

标签: spring-boot scheduled-tasks spring-batch

我只想在不需要自动

的情况下执行预定任务
   import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.JobParametersBuilder;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Import;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @EnableScheduling
    public class RunScheduler {
        @Autowired
        private JobLauncher jobLauncher;

        @Autowired
        private Job job;

        @RequestMapping("collector/test")
        @Scheduled(fixedRate = 5000)
        public void run() {

            try {

                String dateParam = new Date().toString();
                JobParameters param = new JobParametersBuilder().addString("date", dateParam).toJobParameters();

                System.out.println(dateParam);

                JobExecution execution = jobLauncher.run(job, param);
                System.out.println("Exit Status : " + execution.getStatus());

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

我已经尝试过使用石英但是由于自动装配的注释而导致很多错误, 怎么做

1 个答案:

答案 0 :(得分:-2)

从run方法中删除@Scheduled注释。此注释会导致定期调用该方法。

现在,您想要手动执行计划任务,请使用quartz scheduler API或其spring包装器,并完全控制何时调用任务。