Skip to content

Instantly share code, notes, and snippets.

@jhorsman
Last active January 16, 2018 07:48
Show Gist options
  • Select an option

  • Save jhorsman/7c2eaaf7278e214fe1f627e27f5f6dc0 to your computer and use it in GitHub Desktop.

Select an option

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
@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