Last active
January 16, 2018 07:48
-
-
Save jhorsman/7c2eaaf7278e214fe1f627e27f5f6dc0 to your computer and use it in GitHub Desktop.
Run once on application startup with a scheduler (JAVA). See https://stackoverflow.com/a/31304767/1678525. If you truely need to run something only once, this is a better way: https://stackoverflow.com/a/17037758/1678525
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @EnableScheduling | |
| @Component | |
| public class ScheduledTasks { | |
| private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTasks.class); | |
| private static boolean needToRunStartupMethod = true; | |
| @Scheduled(fixedRate = 3600000) | |
| public void keepAlive() { | |
| //log "alive" every hour for sanity checks | |
| LOGGER.debug("alive"); | |
| if (needToRunStartupMethod) { | |
| runOnceOnlyOnStartup(); | |
| needToRunStartupMethod = false; | |
| } | |
| } | |
| public void runOnceOnlyOnStartup() { | |
| LOGGER.debug("running startup job"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment