Browse Source

Moves away from @EventListener to interface implementations for framework 5.1 changes

fixes gh-405
pull/413/head
Spencer Gibb 6 years ago
parent
commit
1d646290ab
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 22
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java
  2. 33
      spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java

22
spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java

@ -1,7 +1,13 @@ @@ -1,7 +1,13 @@
package org.springframework.cloud.client.serviceregistry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PreDestroy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
@ -9,13 +15,9 @@ import org.springframework.cloud.client.discovery.ManagementServerPortUtils; @@ -9,13 +15,9 @@ import org.springframework.cloud.client.discovery.ManagementServerPortUtils;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.event.EventListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import javax.annotation.PreDestroy;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Lifecycle methods that may be useful and common to {@link ServiceRegistry}
* implementations.
@ -27,7 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger; @@ -27,7 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* @author Spencer Gibb
*/
public abstract class AbstractAutoServiceRegistration<R extends Registration>
implements AutoServiceRegistration, ApplicationContextAware {
implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<WebServerInitializedEvent> {
private static final Log logger = LogFactory
.getLog(AbstractAutoServiceRegistration.class);
@ -60,7 +62,13 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration> @@ -60,7 +62,13 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
return context;
}
@EventListener(WebServerInitializedEvent.class)
@Override
@SuppressWarnings("deprecation")
public void onApplicationEvent(WebServerInitializedEvent event) {
bind(event);
}
@Deprecated
public void bind(WebServerInitializedEvent event) {
ApplicationContext context = event.getApplicationContext();
if (context instanceof ConfigurableWebServerApplicationContext) {

33
spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java

@ -16,10 +16,6 @@ @@ -16,10 +16,6 @@
package org.springframework.cloud.endpoint;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
@ -28,8 +24,8 @@ import java.util.List; @@ -28,8 +24,8 @@ import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
@ -43,7 +39,7 @@ import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEven @@ -43,7 +39,7 @@ import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEven
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
@ -51,6 +47,10 @@ import org.springframework.stereotype.Component; @@ -51,6 +47,10 @@ import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
* @author Venil Noronha
@ -132,7 +132,6 @@ public class RefreshEndpointTests { @@ -132,7 +132,6 @@ public class RefreshEndpointTests {
}
@Test
@Ignore //FIXME: 2.1.0
public void eventsPublishedInOrder() throws Exception {
this.context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run();
@ -148,7 +147,6 @@ public class RefreshEndpointTests { @@ -148,7 +147,6 @@ public class RefreshEndpointTests {
}
@Test
@Ignore //FIXME: 2.1.0
public void shutdownHooksCleaned() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
@ -174,17 +172,22 @@ public class RefreshEndpointTests { @@ -174,17 +172,22 @@ public class RefreshEndpointTests {
}
@Configuration
protected static class Empty {
protected static class Empty implements SmartApplicationListener {
private List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
@EventListener(EnvironmentChangeEvent.class)
public void changed(EnvironmentChangeEvent event) {
this.events.add(event);
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return EnvironmentChangeEvent.class.isAssignableFrom(eventType)
|| RefreshScopeRefreshedEvent.class.isAssignableFrom(eventType);
}
@EventListener(RefreshScopeRefreshedEvent.class)
public void refreshed(RefreshScopeRefreshedEvent event) {
this.events.add(event);
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EnvironmentChangeEvent ||
event instanceof RefreshScopeRefreshedEvent) {
this.events.add(event);
}
}
}

Loading…
Cancel
Save