简体   繁体   English

调度:在spring boot中只执行一次任务

[英]Scheduling: execute tasks only one time in spring boot

Im trying to manage scheduled tasks using spring boot. 我试图使用spring boot管理计划任务。 I want to execute my job only one time at a particular date ( specified by the user ). 我想在特定日期 (由用户指定)执行我的工作一次 User can add dates for execution as much as he wants.Here is my Job : 用户可以根据需要添加执行日期。这是我的工作:

@Component
public class JobScheduler{

    @Autowired
    ServiceLayer service;

    @PostConstruct
    public void executeJob(){
        try {
            service.execute();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

And here is the execute method : 这是执行方法:

private TaskScheduler scheduler;

Runnable exampleRunnable = new Runnable(){
    @Override
    public void run() {
        System.out.println("do something ...");
    }
};

@Override
    @Async
    public void execute() throws Exception {
        try {

            List<Date> myListOfDates = getExecutionTime();  // call dao to get dates insered by the user

            ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
            scheduler = new ConcurrentTaskScheduler(localExecutor);
            for(Date d : myListOfDates ){
            scheduler.schedule(exampleRunnable, d);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Issue 1 : Im using PostConstruct annotation. 问题1:我正在使用PostConstruct注释。 Thus, when executeJob method is called, there is no dates in the List 'myListOfDates'. 因此,当调用executeJob方法时,列表'myListOfDates'中没有日期。

Issue 2 : Supposing that myListOfDates contains dates, how can i get the latest dates in case user entered another one? 问题2:假设myListOfDates包含日期,如果用户输入另一个日期,我如何获得最新日期?

Issue 3 : If i use @Scheduled(initailDelay=10000, fixedRate=20000) instead of @PostConstruct annotation, it will resolve the first issue, but it will execute my job every 20s for instance. 问题3:如果我使用@Scheduled(ini​​tailDelay = 10000,fixedRate = 20000)而不是@PostConstruct注释,它将解决第一个问题,但它将每隔20秒执行一次我的工作。

Any clue ? 任何线索?

From what I can infer from your question is, you are asking how to make jobs triggered based on some list of date when spring started. 从我的问题可以推断出,你问的是如何根据春天开始时的一些日期列表触发工作。

First , instead of using @PostConstruct in a bean/component, I think it's better to hook it into application level event listener instead. 首先 ,我认为最好将它挂钩到应用程序级事件侦听器中,而不是在bean /组件中使用@PostConstruct See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/event/ContextRefreshedEvent.html 请参阅http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/event/ContextRefreshedEvent.html

That way,you can make sure you all beans initialized, hence you can load myListOfDates , and then start the scheduler. 这样,您可以确保所有bean都已初始化,因此您可以加载myListOfDates ,然后启动调度程序。

Second , like what I said in my comment, I would suggest you to uses existing 3rd-party library instead. 其次 ,就像我在评论中所说的那样,我建议您使用现有的第三方库。 I only ever uses Quartz in java, so I will ilustrate using Quartz. 我只在java中使用Quartz,所以我将使用Quartz进行说明。

Third , I guess you are storing myListOfDates in some kind of database (not memory), hence the ability of user to modify the scheduled dates. 第三 ,我猜你将myListOfDates存储在某种数据库(而不是内存)中,因此用户可以修改预定日期。 If you follow my suggestion in using 3rd-party library, Quartz have JobStore using JDBC See http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-09.html#TutorialLesson9-JDBCJobStore 如果您按照我的建议使用第三方库,Quartz使用JDBC的JobStore请参阅http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-09.html#TutorialLesson9- JDBCJobStore

Honestly I never uses that one, but I believe the library have mechanism to trigger the job based on what saved in the database. 老实说,我从来没有使用过那个,但我相信图书馆有根据数据库中保存的内容触发工作的机制。 This might be what you are looking for. 这可能就是你要找的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM