-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use spring application event to broadcast the config change event
- Loading branch information
Showing
6 changed files
with
206 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,33 +16,33 @@ | |
*/ | ||
package com.ctrip.framework.apollo.spring.config; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.ConfigChangeListener; | ||
import com.ctrip.framework.apollo.ConfigService; | ||
import com.ctrip.framework.apollo.build.ApolloInjector; | ||
import com.ctrip.framework.apollo.spring.property.AutoUpdateConfigChangeListener; | ||
import com.ctrip.framework.apollo.spring.spi.ApolloConfigChangeEvent; | ||
import com.ctrip.framework.apollo.spring.util.SpringInjector; | ||
import com.ctrip.framework.apollo.util.ConfigUtil; | ||
import com.google.common.collect.ImmutableSortedSet; | ||
import com.google.common.collect.LinkedHashMultimap; | ||
import com.google.common.collect.Multimap; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.ConfigService; | ||
|
||
import com.google.common.collect.Sets; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.context.EnvironmentAware; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.PriorityOrdered; | ||
import org.springframework.core.env.CompositePropertySource; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import org.springframework.core.env.MutablePropertySources; | ||
import org.springframework.core.env.PropertySource; | ||
|
||
|
@@ -56,14 +56,16 @@ | |
* | ||
* @author Jason Song([email protected]) | ||
*/ | ||
public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware, PriorityOrdered { | ||
public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware, | ||
ApplicationEventPublisherAware, PriorityOrdered { | ||
private static final Multimap<Integer, String> NAMESPACE_NAMES = LinkedHashMultimap.create(); | ||
private static final Set<BeanFactory> AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES = Sets.newConcurrentHashSet(); | ||
|
||
private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector | ||
.getInstance(ConfigPropertySourceFactory.class); | ||
private ConfigUtil configUtil; | ||
private ConfigurableEnvironment environment; | ||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
public static boolean addNamespaces(Collection<String> namespaces, int order) { | ||
return NAMESPACE_NAMES.putAll(order, namespaces); | ||
|
@@ -134,17 +136,16 @@ private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environme | |
} | ||
|
||
private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFactory beanFactory) { | ||
if (!configUtil.isAutoUpdateInjectedSpringPropertiesEnabled() || | ||
!AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.add(beanFactory)) { | ||
if (!AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.add(beanFactory)) { | ||
return; | ||
} | ||
|
||
AutoUpdateConfigChangeListener autoUpdateConfigChangeListener = new AutoUpdateConfigChangeListener( | ||
environment, beanFactory); | ||
ConfigChangeListener configChangeEventPublisher = changeEvent -> | ||
applicationEventPublisher.publishEvent(new ApolloConfigChangeEvent(changeEvent)); | ||
|
||
List<ConfigPropertySource> configPropertySources = configPropertySourceFactory.getAllConfigPropertySources(); | ||
for (ConfigPropertySource configPropertySource : configPropertySources) { | ||
configPropertySource.addChangeListener(autoUpdateConfigChangeListener); | ||
configPropertySource.addChangeListener(configChangeEventPublisher); | ||
} | ||
} | ||
|
||
|
@@ -165,4 +166,9 @@ static void reset() { | |
NAMESPACE_NAMES.clear(); | ||
AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.clear(); | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
this.applicationEventPublisher = applicationEventPublisher; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...o-client/src/main/java/com/ctrip/framework/apollo/spring/spi/ApolloConfigChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import com.ctrip.framework.apollo.model.ConfigChangeEvent; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
/** | ||
* A Spring Application Event that is fired when Apollo config changes. | ||
*/ | ||
public class ApolloConfigChangeEvent extends ApplicationEvent { | ||
|
||
public ApolloConfigChangeEvent(ConfigChangeEvent source) { | ||
super(source); | ||
} | ||
|
||
public ConfigChangeEvent getConfigChangeEvent() { | ||
return (ConfigChangeEvent) getSource(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
.../src/test/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2022 Apollo 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 com.ctrip.framework.apollo.spring.config; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.ConfigChangeListener; | ||
import com.ctrip.framework.apollo.model.ConfigChangeEvent; | ||
import com.ctrip.framework.apollo.spring.AbstractSpringIntegrationTest; | ||
import com.ctrip.framework.apollo.spring.spi.ApolloConfigChangeEvent; | ||
import com.google.common.collect.Lists; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.core.env.CompositePropertySource; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.MutablePropertySources; | ||
|
||
public class PropertySourcesProcessorTest extends AbstractSpringIntegrationTest { | ||
|
||
private ConfigurableEnvironment environment; | ||
private ConfigurableListableBeanFactory beanFactory; | ||
private PropertySourcesProcessor processor; | ||
private MutablePropertySources propertySources; | ||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
@Override | ||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
propertySources = mock(MutablePropertySources.class); | ||
environment = mock(ConfigurableEnvironment.class); | ||
when(environment.getPropertySources()).thenReturn(propertySources); | ||
beanFactory = mock(ConfigurableListableBeanFactory.class); | ||
applicationEventPublisher = mock(ApplicationEventPublisher.class); | ||
processor = new PropertySourcesProcessor(); | ||
processor.setEnvironment(environment); | ||
processor.setApplicationEventPublisher(applicationEventPublisher); | ||
} | ||
|
||
@Override | ||
@After | ||
public void tearDown() throws Exception { | ||
super.tearDown(); | ||
PropertySourcesProcessor.reset(); | ||
} | ||
|
||
@Test | ||
public void testInitializePropertySources() { | ||
String namespaceName = "someNamespace"; | ||
String anotherNamespaceName = "anotherNamespace"; | ||
Config config = mock(Config.class); | ||
Config anotherConfig = mock(Config.class); | ||
mockConfig(namespaceName, config); | ||
mockConfig(anotherNamespaceName, anotherConfig); | ||
PropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaceName, anotherNamespaceName), | ||
0); | ||
|
||
processor.postProcessBeanFactory(beanFactory); | ||
|
||
ArgumentCaptor<CompositePropertySource> argumentCaptor = ArgumentCaptor.forClass( | ||
CompositePropertySource.class); | ||
verify(propertySources).addFirst(argumentCaptor.capture()); | ||
|
||
CompositePropertySource compositePropertySource = argumentCaptor.getValue(); | ||
assertEquals(2, compositePropertySource.getPropertySources().size()); | ||
|
||
ConfigPropertySource propertySource = (ConfigPropertySource) Lists.newArrayList( | ||
compositePropertySource.getPropertySources()).get(0); | ||
ConfigPropertySource anotherPropertySource = (ConfigPropertySource) Lists.newArrayList( | ||
compositePropertySource.getPropertySources()).get(1); | ||
|
||
assertEquals(namespaceName, propertySource.getName()); | ||
assertSame(config, propertySource.getSource()); | ||
assertEquals(anotherNamespaceName, anotherPropertySource.getName()); | ||
assertSame(anotherConfig, anotherPropertySource.getSource()); | ||
} | ||
|
||
@Test | ||
public void testApplicationEvent() { | ||
String namespaceName = "someNamespace"; | ||
Config config = mock(Config.class); | ||
mockConfig(namespaceName, config); | ||
PropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaceName), 0); | ||
ConfigChangeEvent someConfigChangeEvent = mock(ConfigChangeEvent.class); | ||
|
||
processor.postProcessBeanFactory(beanFactory); | ||
|
||
ArgumentCaptor<ConfigChangeListener> argumentCaptor = ArgumentCaptor.forClass( | ||
ConfigChangeListener.class); | ||
verify(config).addChangeListener(argumentCaptor.capture()); | ||
|
||
ConfigChangeListener listener = argumentCaptor.getValue(); | ||
listener.onChange(someConfigChangeEvent); | ||
|
||
ArgumentCaptor<ApolloConfigChangeEvent> eventCaptor = ArgumentCaptor.forClass( | ||
ApolloConfigChangeEvent.class); | ||
verify(applicationEventPublisher).publishEvent(eventCaptor.capture()); | ||
|
||
ApolloConfigChangeEvent event = eventCaptor.getValue(); | ||
assertSame(someConfigChangeEvent, event.getConfigChangeEvent()); | ||
} | ||
} |