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
c9fc5b11
Commit
c9fc5b11
authored
Mar 28, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #49 from yiming187/thread
Extract thread build into ThreadFactory
parents
1d3cecb9
c853b31d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
70 deletions
+42
-70
ApolloConfigManager.java
...ain/java/com/ctrip/apollo/client/ApolloConfigManager.java
+32
-48
ConfigLoaderManager.java
...a/com/ctrip/apollo/client/loader/ConfigLoaderManager.java
+10
-22
No files found.
apollo-client/src/main/java/com/ctrip/apollo/client/ApolloConfigManager.java
View file @
c9fc5b11
...
...
@@ -5,6 +5,7 @@ import com.ctrip.apollo.client.loader.ConfigLoaderManager;
import
com.ctrip.apollo.client.model.PropertyChange
;
import
com.ctrip.apollo.client.model.PropertySourceReloadResult
;
import
com.ctrip.apollo.client.util.ConfigUtil
;
import
com.ctrip.apollo.core.utils.ApolloThreadFactory
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -27,8 +28,6 @@ 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,17 +36,18 @@ import java.util.concurrent.atomic.AtomicReference;
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ApolloConfigManager
implements
BeanDefinitionRegistryPostProcessor
,
PriorityOrdered
,
ApplicationContextAware
{
implements
BeanDefinitionRegistryPostProcessor
,
PriorityOrdered
,
ApplicationContextAware
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ApolloConfigManager
.
class
);
private
static
AtomicReference
<
ApolloConfigManager
>
singletonProtector
=
private
static
AtomicReference
<
ApolloConfigManager
>
singletonProtector
=
new
AtomicReference
<
ApolloConfigManager
>();
private
ConfigLoaderManager
configLoaderManager
;
private
ConfigurableApplicationContext
applicationContext
;
private
ConfigUtil
configUtil
;
private
ScheduledExecutorService
executorService
;
private
AtomicLong
counter
;
private
RefreshScope
scope
;
public
ApolloConfigManager
()
{
...
...
@@ -56,32 +56,25 @@ public class ApolloConfigManager
}
this
.
configLoaderManager
=
ConfigLoaderFactory
.
getInstance
().
getConfigLoaderManager
();
this
.
configUtil
=
ConfigUtil
.
getInstance
();
this
.
counter
=
new
AtomicLong
();
executorService
=
Executors
.
newScheduledThreadPool
(
1
,
new
ThreadFactory
()
{
@Override
public
Thread
newThread
(
Runnable
runnable
)
{
Thread
thread
=
new
Thread
(
runnable
,
"ApolloConfigManager-"
+
counter
.
incrementAndGet
());
return
thread
;
}
});
executorService
=
Executors
.
newScheduledThreadPool
(
1
,
ApolloThreadFactory
.
create
(
"ConfigManager"
,
true
));
}
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
if
(!(
applicationContext
instanceof
ConfigurableApplicationContext
))
{
throw
new
RuntimeException
(
String
.
format
(
"ApplicationContext must implement ConfigurableApplicationContext, but found: %s"
,
applicationContext
.
getClass
().
getName
()));
throw
new
RuntimeException
(
String
.
format
(
"ApplicationContext must implement ConfigurableApplicationContext, but found: %s"
,
applicationContext
.
getClass
().
getName
()));
}
this
.
applicationContext
=
(
ConfigurableApplicationContext
)
applicationContext
;
this
.
configUtil
.
setApplicationContext
(
applicationContext
);
}
/**
* This is the first method invoked, so we could prepare the property sources here.
*
Specifically we need to finish preparing property source before PropertySourcesPlaceholderConfigurer
*
so that
configurations could be injected correctly
* This is the first method invoked, so we could prepare the property sources here.
Specifically
*
we need to finish preparing property source before PropertySourcesPlaceholderConfigurer so that
* configurations could be injected correctly
*/
@Override
public
void
postProcessBeanDefinitionRegistry
(
BeanDefinitionRegistry
registry
)
...
...
@@ -93,22 +86,16 @@ public class ApolloConfigManager
/**
* Register beans needed for Apollo Config Client
* <li>
* - RefreshScope: used to refresh beans when configurations changes
* </li>
* <li>
* - PropertySourcesPlaceholderConfigurer: used to support placeholder configuration injection
* <li>- RefreshScope: used to refresh beans when configurations changes</li>
* <li>- PropertySourcesPlaceholderConfigurer: used to support placeholder configuration injection
* </li>
*/
private
void
registerDependentBeans
(
BeanDefinitionRegistry
registry
)
{
BeanDefinition
refreshScope
=
BeanDefinition
refreshScope
=
BeanDefinitionBuilder
.
genericBeanDefinition
(
RefreshScope
.
class
).
getBeanDefinition
();
registry
.
registerBeanDefinition
(
"refreshScope"
,
refreshScope
);
BeanDefinition
propertySourcesPlaceholderConfigurer
=
BeanDefinitionBuilder
.
genericBeanDefinition
(
PropertySourcesPlaceholderConfigurer
.
class
)
.
getBeanDefinition
();
BeanDefinition
propertySourcesPlaceholderConfigurer
=
BeanDefinitionBuilder
.
genericBeanDefinition
(
PropertySourcesPlaceholderConfigurer
.
class
).
getBeanDefinition
();
registry
.
registerBeanDefinition
(
"propertySourcesPlaceholderConfigurer"
,
propertySourcesPlaceholderConfigurer
);
}
...
...
@@ -118,8 +105,7 @@ public class ApolloConfigManager
*/
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
}
throws
BeansException
{}
/**
* Make sure this bean is called before other beans
...
...
@@ -133,15 +119,14 @@ public class ApolloConfigManager
* Initialize property sources
*/
void
initializePropertySource
()
{
//TODO stop application from starting when config cannot be loaded?
//
TODO stop application from starting when config cannot be loaded?
CompositePropertySource
result
=
this
.
configLoaderManager
.
loadPropertySource
();
updateEnvironmentPropertySource
(
result
);
}
private
void
updateEnvironmentPropertySource
(
CompositePropertySource
currentPropertySource
)
{
MutablePropertySources
currentPropertySources
=
MutablePropertySources
currentPropertySources
=
applicationContext
.
getEnvironment
().
getPropertySources
();
if
(
currentPropertySources
.
contains
(
currentPropertySource
.
getName
()))
{
currentPropertySources
.
replace
(
currentPropertySource
.
getName
(),
currentPropertySource
);
...
...
@@ -151,17 +136,16 @@ public class ApolloConfigManager
}
void
schedulePeriodicRefresh
()
{
executorService
.
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
updatePropertySource
();
}
catch
(
Throwable
ex
)
{
logger
.
error
(
"Refreshing config failed"
,
ex
);
}
}
},
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshInterval
(),
executorService
.
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
updatePropertySource
();
}
catch
(
Throwable
ex
)
{
logger
.
error
(
"Refreshing config failed"
,
ex
);
}
}
},
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshInterval
(),
configUtil
.
getRefreshTimeUnit
());
}
...
...
apollo-client/src/main/java/com/ctrip/apollo/client/loader/ConfigLoaderManager.java
View file @
c9fc5b11
...
...
@@ -10,6 +10,7 @@ import com.ctrip.apollo.client.model.PropertyChange;
import
com.ctrip.apollo.client.model.PropertySourceReloadResult
;
import
com.ctrip.apollo.client.util.ConfigUtil
;
import
com.ctrip.apollo.core.dto.ApolloConfig
;
import
com.ctrip.apollo.core.utils.ApolloThreadFactory
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -26,8 +27,6 @@ import java.util.concurrent.ExecutionException;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.atomic.AtomicLong
;
/**
* @author Jason Song(song_s@ctrip.com)
...
...
@@ -38,7 +37,6 @@ public class ConfigLoaderManager {
private
ConfigLoader
configLoader
;
private
ConfigUtil
configUtil
;
private
final
ExecutorService
executorService
;
private
final
AtomicLong
counter
;
private
Map
<
ApolloRegistry
,
ApolloConfig
>
currentApolloRegistryConfigCache
;
private
Map
<
ApolloRegistry
,
ApolloConfig
>
previousApolloRegistryConfigCache
;
private
List
<
ApolloRegistry
>
apolloRegistries
;
...
...
@@ -46,14 +44,8 @@ public class ConfigLoaderManager {
public
ConfigLoaderManager
(
ConfigLoader
configLoader
,
ConfigUtil
configUtil
)
{
this
.
configLoader
=
configLoader
;
this
.
configUtil
=
configUtil
;
this
.
counter
=
new
AtomicLong
();
this
.
executorService
=
Executors
.
newFixedThreadPool
(
5
,
new
ThreadFactory
()
{
@Override
public
Thread
newThread
(
Runnable
runnable
)
{
Thread
thread
=
new
Thread
(
runnable
,
"ConfigLoaderManager-"
+
counter
.
incrementAndGet
());
return
thread
;
}
});
this
.
executorService
=
Executors
.
newFixedThreadPool
(
5
,
ApolloThreadFactory
.
create
(
"ConfigLoaderManager"
,
true
));
this
.
currentApolloRegistryConfigCache
=
Maps
.
newConcurrentMap
();
}
...
...
@@ -69,17 +61,15 @@ public class ConfigLoaderManager {
public
PropertySourceReloadResult
reloadPropertySource
()
{
CompositePropertySource
composite
=
loadPropertySourceWithApolloRegistries
(
apolloRegistries
);
List
<
ApolloConfig
>
previous
=
List
<
ApolloConfig
>
previous
=
Lists
.
newArrayList
(
this
.
previousApolloRegistryConfigCache
.
values
());
List
<
ApolloConfig
>
current
=
Lists
.
newArrayList
(
this
.
currentApolloRegistryConfigCache
.
values
());
return
new
PropertySourceReloadResult
(
composite
,
calcPropertyChanges
(
previous
,
current
));
}
/**
* Load property source with apollo registries provided
* Should not be invoked in parallel since there are some operations like create/destroy cache,
* writing to files etc.
* Load property source with apollo registries provided Should not be invoked in parallel since
* there are some operations like create/destroy cache, writing to files etc.
*/
private
synchronized
CompositePropertySource
loadPropertySourceWithApolloRegistries
(
List
<
ApolloRegistry
>
apolloRegistries
)
{
...
...
@@ -104,7 +94,7 @@ public class ConfigLoaderManager {
}
List
<
PropertyChange
>
calcPropertyChanges
(
List
<
ApolloConfig
>
previous
,
List
<
ApolloConfig
>
current
)
{
List
<
ApolloConfig
>
current
)
{
Map
<
String
,
Object
>
previousMap
=
collectConfigurations
(
previous
);
Map
<
String
,
Object
>
currentMap
=
collectConfigurations
(
current
);
...
...
@@ -130,9 +120,8 @@ public class ConfigLoaderManager {
if
(
previousMap
.
get
(
commonKey
).
equals
(
currentMap
.
get
(
commonKey
)))
{
continue
;
}
changes
.
add
(
new
PropertyChange
(
commonKey
,
previousMap
.
get
(
commonKey
),
currentMap
.
get
(
commonKey
),
PropertyChangeType
.
MODIFIED
));
changes
.
add
(
new
PropertyChange
(
commonKey
,
previousMap
.
get
(
commonKey
),
currentMap
.
get
(
commonKey
),
PropertyChangeType
.
MODIFIED
));
}
return
changes
;
...
...
@@ -173,8 +162,7 @@ public class ConfigLoaderManager {
}
ApolloConfig
loadSingleApolloConfig
(
ApolloRegistry
apolloRegistry
)
{
ApolloConfig
result
=
ApolloConfig
result
=
configLoader
.
loadApolloConfig
(
apolloRegistry
,
getPreviousApolloConfig
(
apolloRegistry
));
if
(
result
==
null
)
{
logger
.
error
(
"Loaded config null..."
);
...
...
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