From b60d76ff5ad932216b2860ef5ae6ac85b193e8c5 Mon Sep 17 00:00:00 2001 From: SAMUEL J MATHEW <1846982+samuelj90@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:15:43 +0000 Subject: [PATCH] Support for Targeted Property Refresh in /actuator/refresh Endpoint Fixes #1370 --- .../cloud/context/refresh/ContextRefresher.java | 11 +++++++---- .../cloud/context/refresh/RefreshAllStrategy.java | 10 ++++++++++ .../refresh/RefreshSpecificPropertiesStrategy.java | 10 ++++++++++ .../cloud/context/refresh/RefreshStrategy.java | 7 +++++++ .../cloud/endpoint/RefreshEndpoint.java | 7 +++++-- 5 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshAllStrategy.java create mode 100644 spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshSpecificPropertiesStrategy.java create mode 100644 spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshStrategy.java diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java index 981493929..cf0c17cbe 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java @@ -90,10 +90,13 @@ protected RefreshScope getScope() { return this.scope; } - public synchronized Set refresh() { - Set keys = refreshEnvironment(); - this.scope.refreshAll(); - return keys; + public Set refresh(Set propertiesToRefresh, RefreshStrategy strategy) { + return strategy.refresh(this, propertiesToRefresh); + } + + public synchronized Set refreshSpecificEnvironment(Set propertiesToRefresh){ + //TODO: Implement this. + return propertiesToRefresh; } public synchronized Set refreshEnvironment() { diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshAllStrategy.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshAllStrategy.java new file mode 100644 index 000000000..71e520249 --- /dev/null +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshAllStrategy.java @@ -0,0 +1,10 @@ +package org.springframework.cloud.context.refresh; + +import java.util.Set; + +public class RefreshAllStrategy implements RefreshStrategy { + @Override + public Set refresh(ContextRefresher contextRefresher, Set propertiesToRefresh) { + return contextRefresher.refreshEnvironment(); + } +} \ No newline at end of file diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshSpecificPropertiesStrategy.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshSpecificPropertiesStrategy.java new file mode 100644 index 000000000..a9747d70d --- /dev/null +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshSpecificPropertiesStrategy.java @@ -0,0 +1,10 @@ +package org.springframework.cloud.context.refresh; + +import java.util.Set; + +public class RefreshSpecificPropertiesStrategy implements RefreshStrategy { + @Override + public Set refresh(ContextRefresher contextRefresher, Set propertiesToRefresh) { + return contextRefresher.refreshSpecificEnvironment(propertiesToRefresh); + } +} \ No newline at end of file diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshStrategy.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshStrategy.java new file mode 100644 index 000000000..cc833e5bb --- /dev/null +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/RefreshStrategy.java @@ -0,0 +1,7 @@ +package org.springframework.cloud.context.refresh; + +import java.util.Set; + +public interface RefreshStrategy { + Set refresh(ContextRefresher contextRefresher, Set propertiesToRefresh); +} \ No newline at end of file diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java b/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java index 42dcc995e..8f5373b22 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java @@ -42,8 +42,11 @@ public RefreshEndpoint(ContextRefresher contextRefresher) { } @WriteOperation - public Collection refresh() { - Set keys = this.contextRefresher.refresh(); + public Collection refresh(Set propertiesToRefresh) { + RefreshStrategy strategy = (propertiesToRefresh != null && !propertiesToRefresh.isEmpty()) + ? new RefreshSpecificPropertiesStrategy() + : new RefreshAllStrategy(); + Set keys = this.contextRefresher.refresh(propertiesToRefresh, strategy); LOG.info("Refreshed keys : " + keys); return keys; }