Spring - scheduling and pooling runnables of different state (each Runnable instance has different state)

不打扰是莪最后的温柔 提交于 2020-01-25 08:31:05

问题


I can't figure out what to use for scheduling and pooling runnables of different state (each Runnable instance has different state). I could use ScheduledExecutorFactoryBean together with MethodInvokingRunnable to supply arguments. But take a look at the key ScheduledExecutorFactoryBean method, it is designed in a way that all task should start at the beginning.

    protected void registerTasks(ScheduledExecutorTask[] tasks, ScheduledExecutorService executor) {
        for (ScheduledExecutorTask task : tasks) {
            Runnable runnable = getRunnableToSchedule(task);
            if (task.isOneTimeTask()) {
                executor.schedule(runnable, task.getDelay(), task.getTimeUnit());
            }
            else {
                if (task.isFixedRate()) {
                    executor.scheduleAtFixedRate(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit());
                }
                else {
                    executor.scheduleWithFixedDelay(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit());
                }
            }
        }
}

I can't think of how to setup this scenario with ThreadPoolTaskScheduler.

Please help me out here. Thank you


EDIT: shortened version is: how to setup task scheduler that would run hundreds of "different instances of Threads (with different state) with 2 seconds interval.


回答1:


It's easy, I don't know what I was thinking that I didn't see it :-)


I have just programmatically filled the array ScheduledExecutorTask[] of ScheduledExecutorFactoryBean with thousands of tasks, each with incremented delay property and different runnable. Then I just used the factorybean...Really handy factorybean from spring guys...



来源:https://stackoverflow.com/questions/3021766/spring-scheduling-and-pooling-runnables-of-different-state-each-runnable-inst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!