Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
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
apollo
Commits
6781b213
Commit
6781b213
authored
Mar 25, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add auto refresh config support
parent
1a3a2842
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
1 deletion
+45
-1
ApolloConfigManager.java
...ain/java/com/ctrip/apollo/client/ApolloConfigManager.java
+32
-1
ConfigUtil.java
...rc/main/java/com/ctrip/apollo/client/util/ConfigUtil.java
+13
-0
No files found.
apollo-client/src/main/java/com/ctrip/apollo/client/ApolloConfigManager.java
View file @
6781b213
...
...
@@ -24,6 +24,10 @@ import org.springframework.core.env.CompositePropertySource;
import
org.springframework.core.env.MutablePropertySources
;
import
java.util.List
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.atomic.AtomicLong
;
import
java.util.concurrent.atomic.AtomicReference
;
/**
...
...
@@ -37,6 +41,9 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
private
ConfigLoaderManager
configLoaderManager
;
private
ConfigurableApplicationContext
applicationContext
;
private
ConfigUtil
configUtil
;
private
ScheduledExecutorService
executorService
;
private
AtomicLong
counter
;
private
RefreshScope
scope
;
public
ApolloConfigManager
()
{
...
...
@@ -44,6 +51,15 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
throw
new
IllegalStateException
(
"There should be only one ApolloConfigManager instance!"
);
}
this
.
configLoaderManager
=
ConfigLoaderFactory
.
getInstance
().
getConfigLoaderManager
();
this
.
configUtil
=
ConfigUtil
.
getInstance
();
this
.
counter
=
new
AtomicLong
();
executorService
=
Executors
.
newScheduledThreadPool
(
1
,
new
ThreadFactory
()
{
@Override
public
Thread
newThread
(
Runnable
r
)
{
Thread
thread
=
new
Thread
(
r
,
"ApolloConfigManager-"
+
counter
.
incrementAndGet
());
return
thread
;
}
});
}
@Override
...
...
@@ -53,7 +69,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
String
.
format
(
"ApplicationContext must implement ConfigurableApplicationContext, but found: %s"
,
applicationContext
.
getClass
().
getName
()));
}
this
.
applicationContext
=
(
ConfigurableApplicationContext
)
applicationContext
;
ConfigUtil
.
getInstance
()
.
setApplicationContext
(
applicationContext
);
this
.
configUtil
.
setApplicationContext
(
applicationContext
);
}
/**
...
...
@@ -65,6 +81,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
public
void
postProcessBeanDefinitionRegistry
(
BeanDefinitionRegistry
registry
)
throws
BeansException
{
registerDependentBeans
(
registry
);
initializePropertySource
();
schedulePeriodicRefresh
();
}
/**
...
...
@@ -119,9 +136,23 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
currentPropertySources
.
addFirst
(
currentPropertySource
);
}
void
schedulePeriodicRefresh
()
{
executorService
.
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
updatePropertySource
();
}
catch
(
Throwable
e
)
{
logger
.
error
(
"Refreshing config failed"
,
e
);
}
}
},
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshTimeUnit
());
}
public
List
<
PropertyChange
>
updatePropertySource
()
{
PropertySourceReloadResult
result
=
this
.
configLoaderManager
.
reloadPropertySource
();
if
(
result
.
hasChanges
())
{
logger
.
info
(
"Found changes, refresh environment and refreshscope beans."
);
updateEnvironmentPropertySource
(
result
.
getPropertySource
());
refreshBeans
();
}
...
...
apollo-client/src/main/java/com/ctrip/apollo/client/util/ConfigUtil.java
View file @
6781b213
...
...
@@ -15,12 +15,17 @@ import java.net.URL;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.concurrent.TimeUnit
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ConfigUtil
{
public
static
final
String
APOLLO_PROPERTY
=
"apollo.properties"
;
//TODO read from config?
private
static
final
int
refreshInterval
=
5
;
private
static
final
TimeUnit
refreshIntervalTimeUnit
=
TimeUnit
.
MINUTES
;
private
static
ConfigUtil
configUtil
=
new
ConfigUtil
();
private
ApplicationContext
applicationContext
;
...
...
@@ -40,6 +45,14 @@ public class ConfigUtil {
this
.
applicationContext
=
applicationContext
;
}
public
int
getRefreshInterval
()
{
return
refreshInterval
;
}
public
TimeUnit
getRefreshTimeUnit
()
{
return
refreshIntervalTimeUnit
;
}
public
List
<
ApolloRegistry
>
loadApolloRegistries
()
throws
IOException
{
List
<
URL
>
resourceUrls
=
Collections
.
list
(
ClassLoaderUtil
.
getLoader
().
getResources
(
APOLLO_PROPERTY
));
...
...
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