Search results
Schedule publishing RefreshEvent to reload @RefreshScope-annotated Beans in Spring Boot applications
Application.java
:
@SpringBootApplication
@EnableScheduling
public class Application {
// ...
}
ScheduledTasks.java
:
@Component
public class ScheduledTasks {
private final ApplicationEventPublisher eventPublisher;
// ...
@Scheduled(cron = "${asimiotech.tasks.cron.fire-refresh-event}")
public void publishRefreshEvent() {
log.info("Publishing {} at {}", RefreshEvent.class.getSimpleName(), new Date());
this.eventPublisher.publishEvent(new RefreshEvent(
this,
RefreshEvent.class.getSimpleName(),
String.format("Refreshing Beans annotated with %s if relevant properties changed", RefreshScope.class.getSimpleName())
));
}
// ...
}
application.yml
:
asimiotech:
tasks:
cron:
fire-refresh-event: "0 0/5 * * * *" # Every 5, 10, 15, ... min of the hour