首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何手动触发定时任务

如何手动触发定时任务
EN

Stack Overflow用户
提问于 2017-05-02 23:31:41
回答 1查看 1.5K关注 0票数 2

我只想在不需要自动执行计划任务的情况下执行计划任务

代码语言:javascript
复制
   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();
            }

        }

我已经尝试使用quartz了,但是我得到了很多由自动连接的注解引起的错误,该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2017-05-04 00:46:52

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

现在,如果您希望手动执行计划任务,请使用quartz调度程序API或它的spring包装器,并完全控制何时调用任务。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43741842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档