4
votes

Is there the notion of an @RequestScope post-refresh hook?

Use Case: We need to push config changes. Currently, this is manual and fires as a part of the app restart. However, if we can trigger a /bus/refresh, once the Environment gets refreshed, not destroy the bean (an @Service in this case), yet fire some config changes once the new environment value gets updated? Is something like this possible?

1
There's a Spring Application event that is published. On my phone, so I can't remember exactly what it is. (RefreshEvent?). - spencergibb
@spencergibb thanks. I will look into monitoring for that and see if this solves the issue. - dmfrey
@spencergibb I am finding RefreshEvent and RefreshScopeRefreshedEvent. There is also, already, a RefreshListener, which it looks like that is what I really want to implement in my service. Is there already a RefreshEndpoint exposed that I can autowire in? - dmfrey
A @RefreshScope bean is also a proxy with a target whose lifecycle is tied to those refresh events. So that's a good way to react to the events without writing any Spring-specific code. I guess I didn't understand the use case if that wouldn't work. - Dave Syer
@DaveSyer That was my original goal, however, they didn't want the bean to be destroyed. I found out some more about why they want this. It is a search process and this specific case has to do with being able to manually resize the cache at will. I won't go into why they want to do this in this manner, but this is the requirement. It's a legacy app that sounds like it needs to be refactored to take advantage of better caching technologies out there. - dmfrey

1 Answers

4
votes

The solution I used was to make my @Service implement ApplicationListener and override onApplicationEvent(RefreshRemoteApplicationEvent event). This is already performed in RefreshListener, but my implementation was able to inspect the changed keys and react according to the rules the client setup for the specific key changes.