简体   繁体   中英

Spring and Dropwizard: Running a scheduled task after app has fully started up

This is a very general question about best practice, so I apologize in advance if it shouldn't go here.

I'm trying to set up a task schedule to check to see what rules in a database are active, using spring task scheduler. These rules will be added to a hashmap for reference. As of right now, we have an init() method that runs in Dropwizard's run method in the main application class. I've set up a refresh method to check every 5 seconds after it runs, using the Spring task scheduler in the Spring XML configuration file:

<context:component-scan base-package="com.myproject.db"/>

<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="ruleDAO" method="refreshRules" initial-delay="10000" fixed-delay="5000"/>
</task:scheduled-tasks>

<task:scheduler id="scheduler"/>

This does technically work. However, I don't like the idea of hardcoding that 10 second initial delay in the XML file; I would like to set this up to run 10 seconds after init() finishes, to ensure that there is no crossover between the two methods. Is there anyway to do this in the XML file?

如果您的init()方法是PostConstruct ,则由于ScheduledAnnotationBeanPostProcessor ,调度程序将一直等到init()完成之后。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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