Browse Source

RefreshEventListener no longer depends on RefreshEndpoint.

Instead it depends on ContextRefresher with actually does the work.

This allows applications to use the listener even if they don't depend
on actuator.

fixes gh-171
pull/208/head
Spencer Gibb 8 years ago
parent
commit
b40ceef356
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 6
      spring-cloud-context/pom.xml
  2. 7
      spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java
  3. 16
      spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java
  4. 14
      spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/event/RefreshEventListener.java
  5. 42
      spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java

6
spring-cloud-context/pom.xml

@ -59,5 +59,11 @@ @@ -59,5 +59,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>
</dependencies>
</project>

7
spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java

@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; @@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.cloud.context.environment.EnvironmentManager;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.cloud.endpoint.event.RefreshEventListener;
import org.springframework.cloud.logging.LoggingRebinder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@ -88,4 +89,10 @@ public class RefreshAutoConfiguration { @@ -88,4 +89,10 @@ public class RefreshAutoConfiguration {
return new ContextRefresher(context, scope);
}
@Bean
public RefreshEventListener refreshEventListener(
ContextRefresher contextRefresher) {
return new RefreshEventListener(contextRefresher);
}
}

16
spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java

@ -16,16 +16,10 @@ @@ -16,16 +16,10 @@
package org.springframework.cloud.autoconfigure;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.InfoEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -34,18 +28,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClas @@ -34,18 +28,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClas
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.cloud.endpoint.event.RefreshEventListener;
import org.springframework.cloud.health.RefreshScopeHealthIndicator;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
/**
@ -119,11 +109,5 @@ public class RefreshEndpointAutoConfiguration { @@ -119,11 +109,5 @@ public class RefreshEndpointAutoConfiguration {
return endpoint;
}
@Bean
public RefreshEventListener refreshEventListener(
RefreshEndpoint refreshEndpoint) {
return new RefreshEventListener(refreshEndpoint);
}
}
}

14
spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/event/RefreshEventListener.java

@ -1,25 +1,25 @@ @@ -1,25 +1,25 @@
package org.springframework.cloud.endpoint.event;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.event.EventListener;
import lombok.extern.apachecommons.CommonsLog;
/**
* Calls {@link RefreshEndpoint#refresh()} when a {@link RefreshEvent} is received.
* Calls {@link RefreshEventListener#refresh} when a {@link RefreshEvent} is received.
* Only responds to {@link RefreshEvent} after receiving an {@link ApplicationReadyEvent} as the RefreshEvent's might come to early in the application lifecycle.
* @author Spencer Gibb
*/
@CommonsLog
public class RefreshEventListener {
private RefreshEndpoint refresh;
private ContextRefresher refresh;
private AtomicBoolean ready = new AtomicBoolean(false);
public RefreshEventListener(RefreshEndpoint refresh) {
public RefreshEventListener(ContextRefresher refresh) {
this.refresh = refresh;
}
@ -32,8 +32,8 @@ public class RefreshEventListener { @@ -32,8 +32,8 @@ public class RefreshEventListener {
public void handle(RefreshEvent event) {
if (this.ready.get()) { // don't handle events before app is ready
log.debug("Event received " + event.getEventDesc());
String[] keys = this.refresh.refresh();
log.info("Refresh keys changed: " + Arrays.asList(keys));
Set<String> keys = this.refresh.refresh();
log.info("Refresh keys changed: " + keys);
}
}
}

42
spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
package org.springframework.cloud.autoconfigure;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.ClassPathExclusions;
import org.springframework.cloud.FilteredClassPathRunner;
import org.springframework.cloud.endpoint.event.RefreshEventListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertFalse;
/**
* @author Spencer Gibb
*/
@RunWith(FilteredClassPathRunner.class)
@ClassPathExclusions({"spring-boot-actuator-*.jar", "spring-boot-starter-actuator-*.jar"})
public class RefreshAutoConfigurationClassPathTests {
@Test
public void refreshEventListenerCreated() {
try (ConfigurableApplicationContext context = getApplicationContext(
Config.class)) {
assertFalse(context.getBeansOfType(RefreshEventListener.class).isEmpty());
assertFalse(context.containsBean("refeshEndpoint"));
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(false)
.properties(properties).run();
}
@Configuration
@EnableAutoConfiguration
static class Config {
}
}
Loading…
Cancel
Save