Use RefreshEvent/Listener from s-c-commons

This commit is contained in:
Spencer Gibb
2016-02-08 17:11:50 -07:00
parent fd0befb196
commit 3e875bca82
5 changed files with 5 additions and 111 deletions

View File

@@ -27,6 +27,7 @@ import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
@@ -81,7 +82,8 @@ public class ConfigWatch implements Closeable, ApplicationEventPublisherAware {
if (newIndex != null && !newIndex.equals(currentIndex)) {
// don't publish the same index again, don't publish the first time (-1) so index can be primed
if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
this.publisher.publishEvent(new ConsulConfigRefreshEvent(this, new RefreshEventData(context, currentIndex, newIndex)));
RefreshEventData data = new RefreshEventData(context, currentIndex, newIndex);
this.publisher.publishEvent(new RefreshEvent(this, data, data.toString()));
}
this.consulIndexes.put(context, newIndex);
}

View File

@@ -18,7 +18,6 @@ package org.springframework.cloud.consul.config;
import com.ecwid.consul.v1.ConsulClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.endpoint.RefreshEndpoint;
@@ -34,13 +33,6 @@ public class ConsulConfigAutoConfiguration {
@Configuration
@ConditionalOnClass(RefreshEndpoint.class)
protected static class ConsulRefreshConfiguration {
@Bean
@ConditionalOnBean(RefreshEndpoint.class)
public ConsulConfigRefreshListener consulConfigRefreshListener(
RefreshEndpoint refreshEndpoint) {
return new ConsulConfigRefreshListener(refreshEndpoint);
}
@Bean
@ConditionalOnProperty(name = "spring.cloud.consul.config.watch.enabled", matchIfMissing = true)
public ConfigWatch configWatch(ConsulPropertySourceLocator locator,

View File

@@ -1,45 +0,0 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.config;
import org.springframework.context.ApplicationEvent;
/**
* @author Spencer Gibb
*/
public class ConsulConfigRefreshEvent extends ApplicationEvent {
private Object event;
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public ConsulConfigRefreshEvent(Object source, Object event) {
super(source);
this.event = event;
}
public Object getEvent() {
return event;
}
public String getEventDesc() {
return event.toString();
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.config;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.context.event.EventListener;
import lombok.extern.apachecommons.CommonsLog;
/**
* @author Spencer Gibb
*/
@CommonsLog
public class ConsulConfigRefreshListener {
private RefreshEndpoint refresh;
private AtomicBoolean ready = new AtomicBoolean(false);
public ConsulConfigRefreshListener(RefreshEndpoint refresh) {
this.refresh = refresh;
}
@EventListener
public void handle(ApplicationReadyEvent event) {
this.ready.compareAndSet(false, true);
}
@EventListener
public void handle(ConsulConfigRefreshEvent event) {
if (this.ready.get()) { // don't handle events before app is ready
log.info("Event received " + event.getEventDesc());
if (this.refresh != null) {
String[] keys = this.refresh.refresh();
log.info("Refresh keys changed: " + Arrays.asList(keys));
}
}
}
}

View File

@@ -117,13 +117,13 @@ public class ConsulPropertySourceLocatorTests {
this.client.setKVValue(KEY, "testPropValUpdate");
TestRefreshEndpoint endpoint = this.context.getBean(TestRefreshEndpoint.class);
boolean receivedEvent = endpoint.successLatch.await(15, TimeUnit.SECONDS);
boolean receivedEvent = endpoint.successLatch.await(60, TimeUnit.SECONDS);
assertThat("listener didn't receive event", receivedEvent, is(true));
testProp = this.environment.getProperty("testProp");
assertThat("testProp was wrong after update", testProp, is(equalTo("testPropValUpdate")));
boolean receivedExtraEvent = endpoint.toManyLatch.await(2, TimeUnit.SECONDS);
boolean receivedExtraEvent = endpoint.toManyLatch.await(15, TimeUnit.SECONDS);
assertThat("refresh called to many times", receivedExtraEvent, is(false));
}
}