Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
01c5b44a
Commit
01c5b44a
authored
Jan 26, 2015
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
propagate EnvironmentChangeEvents to archaius dynamic property listenters
fixes gh-144
parent
614ec44c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
ArchaiusAutoConfiguration.java
...ork/cloud/netflix/archaius/ArchaiusAutoConfiguration.java
+31
-0
ArchaiusAutoConfigurationTests.java
...loud/netflix/archaius/ArchaiusAutoConfigurationTests.java
+25
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/archaius/ArchaiusAutoConfiguration.java
View file @
01c5b44a
...
@@ -22,15 +22,22 @@ import java.util.concurrent.atomic.AtomicBoolean;
...
@@ -22,15 +22,22 @@ import java.util.concurrent.atomic.AtomicBoolean;
import
javax.annotation.PreDestroy
;
import
javax.annotation.PreDestroy
;
import
lombok.extern.apachecommons.CommonsLog
;
import
lombok.extern.apachecommons.CommonsLog
;
import
org.apache.commons.configuration.AbstractConfiguration
;
import
org.apache.commons.configuration.ConfigurationBuilder
;
import
org.apache.commons.configuration.ConfigurationBuilder
;
import
org.apache.commons.configuration.EnvironmentConfiguration
;
import
org.apache.commons.configuration.EnvironmentConfiguration
;
import
org.apache.commons.configuration.SystemConfiguration
;
import
org.apache.commons.configuration.SystemConfiguration
;
import
org.apache.commons.configuration.event.ConfigurationEvent
;
import
org.apache.commons.configuration.event.ConfigurationListener
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.actuate.endpoint.Endpoint
;
import
org.springframework.boot.actuate.endpoint.Endpoint
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.Environment
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.ReflectionUtils
;
import
com.netflix.config.ConcurrentCompositeConfiguration
;
import
com.netflix.config.ConcurrentCompositeConfiguration
;
...
@@ -85,6 +92,30 @@ public class ArchaiusAutoConfiguration {
...
@@ -85,6 +92,30 @@ public class ArchaiusAutoConfiguration {
}
}
}
}
@Configuration
@ConditionalOnProperty
(
value
=
"archaius.propagate.environmentChangedEvent"
,
matchIfMissing
=
true
)
@ConditionalOnClass
(
EnvironmentChangeEvent
.
class
)
protected
static
class
PropagateEventsConfiguration
implements
ApplicationListener
<
EnvironmentChangeEvent
>
{
@Autowired
private
Environment
env
;
@Override
public
void
onApplicationEvent
(
EnvironmentChangeEvent
event
)
{
AbstractConfiguration
manager
=
ConfigurationManager
.
getConfigInstance
();
for
(
String
key
:
event
.
getKeys
())
{
for
(
ConfigurationListener
listener
:
manager
.
getConfigurationListeners
())
{
Object
source
=
event
.
getSource
();
// TODO: Handle add vs set vs delete?
int
type
=
AbstractConfiguration
.
EVENT_SET_PROPERTY
;
String
value
=
env
.
getProperty
(
key
);
boolean
beforeUpdate
=
false
;
listener
.
configurationChanged
(
new
ConfigurationEvent
(
source
,
type
,
key
,
value
,
beforeUpdate
));
}
}
}
}
@SuppressWarnings
(
"deprecation"
)
@SuppressWarnings
(
"deprecation"
)
protected
void
configureArchaius
(
ConfigurableEnvironmentConfiguration
envConfig
)
{
protected
void
configureArchaius
(
ConfigurableEnvironmentConfiguration
envConfig
)
{
if
(
initialized
.
compareAndSet
(
false
,
true
))
{
if
(
initialized
.
compareAndSet
(
false
,
true
))
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/archaius/ArchaiusAutoConfigurationTests.java
View file @
01c5b44a
...
@@ -16,11 +16,18 @@
...
@@ -16,11 +16,18 @@
package
org
.
springframework
.
cloud
.
netflix
.
archaius
;
package
org
.
springframework
.
cloud
.
netflix
.
archaius
;
import
com.google.common.collect.Sets
;
import
com.netflix.config.ConfigurationManager
;
import
org.apache.commons.configuration.AbstractConfiguration
;
import
org.apache.commons.configuration.AbstractConfiguration
;
import
org.apache.commons.configuration.event.ConfigurationEvent
;
import
org.apache.commons.configuration.event.ConfigurationListener
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
/**
/**
...
@@ -29,6 +36,7 @@ import static org.junit.Assert.assertNotNull;
...
@@ -29,6 +36,7 @@ import static org.junit.Assert.assertNotNull;
public
class
ArchaiusAutoConfigurationTests
{
public
class
ArchaiusAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
private
AnnotationConfigApplicationContext
context
;
private
Object
propertyValue
;
@After
@After
public
void
close
()
{
public
void
close
()
{
...
@@ -46,4 +54,21 @@ public class ArchaiusAutoConfigurationTests {
...
@@ -46,4 +54,21 @@ public class ArchaiusAutoConfigurationTests {
assertNotNull
(
config
.
getString
(
"java.io.tmpdir"
));
assertNotNull
(
config
.
getString
(
"java.io.tmpdir"
));
}
}
@Test
public
void
environmentChangeEventPropagated
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
ArchaiusAutoConfiguration
.
class
);
ConfigurationManager
.
getConfigInstance
().
addConfigurationListener
(
new
ConfigurationListener
()
{
@Override
public
void
configurationChanged
(
ConfigurationEvent
event
)
{
if
(
event
.
getPropertyName
().
equals
(
"my.prop"
))
{
propertyValue
=
event
.
getPropertyValue
();
}
}
});
EnvironmentTestUtils
.
addEnvironment
(
context
,
"my.prop=my.newval"
);
context
.
publishEvent
(
new
EnvironmentChangeEvent
(
Sets
.
newHashSet
(
"my.prop"
)));
assertEquals
(
"my.newval"
,
propertyValue
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment