Detecting refreshing of RefreshScope beans
When the refresh occurs EnvironmentChangeEvent
would be raised in your config client, as the documentation states:
The application will listen for an
EnvironmentChangedEvent
and react to the change in a couple of standard ways (additionalApplicationListener
s can be added as@Bean
s by the user in the normal way).
So, you can define your event listener for this event:
public class YourEventListener implements ApplicationListener<EnvironmentChangeEvent> {
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
// do stuff
}
}
I think an approach can be to annotate with @RefreshScope
all your bean that have properties externalized by the configuration and annotated within @Value ( "${your.prop.key}" )
annotation.
These properties are updated when they changed on configuration.
More specifically, after the refresh of properties and application context under scope RefreshScope
, an event RefreshScopeRefreshedEvent
is triggered. You can have a listener for this given the understanding that the properties has finished updates (you can be sure to capture updated values only).
EnvironmentChangeEvent
is fired when there's a change in Environment
. In terms of Spring Cloud Config it means it's triggered when /env
actuator endpoint is called.
RefreshScopeRefreshedEvent
is fired when refresh of @RefreshScope
beans has been initiated, e.g. /refresh
actuator endpoint is called.
That means that you need to register ApplicationListener<RefreshScopeRefreshedEvent>
like that:
@Configuration
public class AppConfig {
@EventListener(RefreshScopeRefreshedEvent.class)
public void onRefresh(RefreshScopeRefreshedEvent event) {
// Your code goes here...
}
}