Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
942fffdf
apollo
项目概览
superrain51
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
942fffdf
编写于
4月 29, 2017
作者:
N
nobodyiam
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add config file change listener
上级
bd1832bf
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
287 addition
and
6 deletion
+287
-6
apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFile.java
.../src/main/java/com/ctrip/framework/apollo/ConfigFile.java
+7
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFileChangeListener.java
.../com/ctrip/framework/apollo/ConfigFileChangeListener.java
+14
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java
.../ctrip/framework/apollo/internals/AbstractConfigFile.java
+62
-1
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PlainTextConfigFile.java
...ctrip/framework/apollo/internals/PlainTextConfigFile.java
+7
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PropertiesConfigFile.java
...trip/framework/apollo/internals/PropertiesConfigFile.java
+6
-5
apollo-client/src/main/java/com/ctrip/framework/apollo/model/ConfigFileChangeEvent.java
...m/ctrip/framework/apollo/model/ConfigFileChangeEvent.java
+56
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java
...st/java/com/ctrip/framework/apollo/ConfigServiceTest.java
+5
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java
.../framework/apollo/internals/DefaultConfigManagerTest.java
+6
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java
.../framework/apollo/internals/PropertiesConfigFileTest.java
+23
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java
...m/ctrip/framework/apollo/internals/XmlConfigFileTest.java
+93
-0
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/api/ApolloConfigDemo.java
...com/ctrip/framework/apollo/demo/api/ApolloConfigDemo.java
+8
-0
未找到文件。
apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFile.java
浏览文件 @
942fffdf
...
...
@@ -29,4 +29,11 @@ public interface ConfigFile {
* @return the config file format enum
*/
ConfigFileFormat
getConfigFileFormat
();
/**
* Add change listener to this config file instance.
*
* @param listener the config file change listener
*/
void
addChangeListener
(
ConfigFileChangeListener
listener
);
}
apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFileChangeListener.java
0 → 100644
浏览文件 @
942fffdf
package
com.ctrip.framework.apollo
;
import
com.ctrip.framework.apollo.model.ConfigFileChangeEvent
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
interface
ConfigFileChangeListener
{
/**
* Invoked when there is any config change for the namespace.
* @param changeEvent the event for this change
*/
void
onChange
(
ConfigFileChangeEvent
changeEvent
);
}
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java
浏览文件 @
942fffdf
package
com.ctrip.framework.apollo.internals
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Properties
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.atomic.AtomicReference
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.ctrip.framework.apollo.ConfigFile
;
import
com.ctrip.framework.apollo.ConfigFileChangeListener
;
import
com.ctrip.framework.apollo.core.utils.ApolloThreadFactory
;
import
com.ctrip.framework.apollo.enums.PropertyChangeType
;
import
com.ctrip.framework.apollo.model.ConfigFileChangeEvent
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.ctrip.framework.apollo.tracer.spi.Transaction
;
import
com.ctrip.framework.apollo.util.ExceptionUtil
;
import
com.google.common.collect.Lists
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
abstract
class
AbstractConfigFile
implements
ConfigFile
,
RepositoryChangeListener
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AbstractConfigFile
.
class
);
private
static
ExecutorService
m_executorService
;
protected
ConfigRepository
m_configRepository
;
protected
String
m_namespace
;
protected
AtomicReference
<
Properties
>
m_configProperties
;
private
List
<
ConfigFileChangeListener
>
m_listeners
=
Lists
.
newCopyOnWriteArrayList
();
static
{
m_executorService
=
Executors
.
newCachedThreadPool
(
ApolloThreadFactory
.
create
(
"ConfigFile"
,
true
));
}
public
AbstractConfigFile
(
String
namespace
,
ConfigRepository
configRepository
)
{
m_configRepository
=
configRepository
;
...
...
@@ -45,6 +62,8 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
return
m_namespace
;
}
protected
abstract
void
update
(
Properties
newProperties
);
@Override
public
synchronized
void
onRepositoryChange
(
String
namespace
,
Properties
newProperties
)
{
if
(
newProperties
.
equals
(
m_configProperties
.
get
()))
{
...
...
@@ -53,9 +72,51 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
Properties
newConfigProperties
=
new
Properties
();
newConfigProperties
.
putAll
(
newProperties
);
m_configProperties
.
set
(
newConfigProperties
);
String
oldValue
=
getContent
();
update
(
newProperties
);
String
newValue
=
getContent
();
PropertyChangeType
changeType
=
PropertyChangeType
.
MODIFIED
;
if
(
oldValue
==
null
)
{
changeType
=
PropertyChangeType
.
ADDED
;
}
else
if
(
newValue
==
null
)
{
changeType
=
PropertyChangeType
.
DELETED
;
}
this
.
fireConfigChange
(
new
ConfigFileChangeEvent
(
m_namespace
,
oldValue
,
newValue
,
changeType
));
Tracer
.
logEvent
(
"Apollo.Client.ConfigChanges"
,
m_namespace
);
}
@Override
public
void
addChangeListener
(
ConfigFileChangeListener
listener
)
{
if
(!
m_listeners
.
contains
(
listener
))
{
m_listeners
.
add
(
listener
);
}
}
private
void
fireConfigChange
(
final
ConfigFileChangeEvent
changeEvent
)
{
for
(
final
ConfigFileChangeListener
listener
:
m_listeners
)
{
m_executorService
.
submit
(
new
Runnable
()
{
@Override
public
void
run
()
{
String
listenerName
=
listener
.
getClass
().
getName
();
Transaction
transaction
=
Tracer
.
newTransaction
(
"Apollo.ConfigFileChangeListener"
,
listenerName
);
try
{
listener
.
onChange
(
changeEvent
);
transaction
.
setStatus
(
Transaction
.
SUCCESS
);
}
catch
(
Throwable
ex
)
{
transaction
.
setStatus
(
ex
);
Tracer
.
logError
(
ex
);
logger
.
error
(
"Failed to invoke config file change listener {}"
,
listenerName
,
ex
);
}
finally
{
transaction
.
complete
();
}
}
});
}
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PlainTextConfigFile.java
浏览文件 @
942fffdf
package
com.ctrip.framework.apollo.internals
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
java.util.Properties
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
abstract
class
PlainTextConfigFile
extends
AbstractConfigFile
{
public
PlainTextConfigFile
(
String
namespace
,
ConfigRepository
configRepository
)
{
super
(
namespace
,
configRepository
);
}
...
...
@@ -25,4 +27,9 @@ public abstract class PlainTextConfigFile extends AbstractConfigFile {
}
return
m_configProperties
.
get
().
containsKey
(
ConfigConsts
.
CONFIG_FILE_CONTENT_KEY
);
}
@Override
protected
void
update
(
Properties
newProperties
)
{
m_configProperties
.
set
(
newProperties
);
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PropertiesConfigFile.java
浏览文件 @
942fffdf
...
...
@@ -25,6 +25,12 @@ public class PropertiesConfigFile extends AbstractConfigFile {
m_contentCache
=
new
AtomicReference
<>();
}
@Override
protected
void
update
(
Properties
newProperties
)
{
m_configProperties
.
set
(
newProperties
);
m_contentCache
.
set
(
null
);
}
@Override
public
String
getContent
()
{
if
(
m_contentCache
.
get
()
==
null
)
{
...
...
@@ -60,9 +66,4 @@ public class PropertiesConfigFile extends AbstractConfigFile {
return
ConfigFileFormat
.
Properties
;
}
@Override
public
synchronized
void
onRepositoryChange
(
String
namespace
,
Properties
newProperties
)
{
super
.
onRepositoryChange
(
namespace
,
newProperties
);
m_contentCache
.
set
(
null
);
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/model/ConfigFileChangeEvent.java
0 → 100644
浏览文件 @
942fffdf
package
com.ctrip.framework.apollo.model
;
import
com.ctrip.framework.apollo.enums.PropertyChangeType
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ConfigFileChangeEvent
{
private
final
String
namespace
;
private
final
String
oldValue
;
private
final
String
newValue
;
private
final
PropertyChangeType
changeType
;
/**
* Constructor.
*
* @param namespace the namespace of the config file change event
* @param oldValue the value before change
* @param newValue the value after change
* @param changeType the change type
*/
public
ConfigFileChangeEvent
(
String
namespace
,
String
oldValue
,
String
newValue
,
PropertyChangeType
changeType
)
{
this
.
namespace
=
namespace
;
this
.
oldValue
=
oldValue
;
this
.
newValue
=
newValue
;
this
.
changeType
=
changeType
;
}
public
String
getNamespace
()
{
return
namespace
;
}
public
String
getOldValue
()
{
return
oldValue
;
}
public
String
getNewValue
()
{
return
newValue
;
}
public
PropertyChangeType
getChangeType
()
{
return
changeType
;
}
@Override
public
String
toString
()
{
final
StringBuilder
sb
=
new
StringBuilder
(
"ConfigFileChangeEvent{"
);
sb
.
append
(
"namespace='"
).
append
(
namespace
).
append
(
'\''
);
sb
.
append
(
", oldValue='"
).
append
(
oldValue
).
append
(
'\''
);
sb
.
append
(
", newValue='"
).
append
(
newValue
).
append
(
'\''
);
sb
.
append
(
", changeType="
).
append
(
changeType
);
sb
.
append
(
'}'
);
return
sb
.
toString
();
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java
浏览文件 @
942fffdf
...
...
@@ -132,6 +132,11 @@ public class ConfigServiceTest {
public
ConfigFileFormat
getConfigFileFormat
()
{
return
m_configFileFormat
;
}
@Override
public
void
addChangeListener
(
ConfigFileChangeListener
listener
)
{
}
}
public
static
class
MockConfigFactory
implements
ConfigFactory
{
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java
浏览文件 @
942fffdf
...
...
@@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
java.util.Properties
;
import
java.util.Set
;
import
org.junit.Before
;
...
...
@@ -110,6 +111,11 @@ public class DefaultConfigManagerTest {
ConfigRepository
someConfigRepository
=
mock
(
ConfigRepository
.
class
);
return
new
AbstractConfigFile
(
namespace
,
someConfigRepository
)
{
@Override
protected
void
update
(
Properties
newProperties
)
{
}
@Override
public
String
getContent
()
{
return
someConfigContent
;
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java
浏览文件 @
942fffdf
...
...
@@ -6,8 +6,14 @@ import static org.junit.Assert.assertNull;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.ConfigFile
;
import
com.ctrip.framework.apollo.ConfigFileChangeListener
;
import
com.ctrip.framework.apollo.enums.PropertyChangeType
;
import
com.ctrip.framework.apollo.model.ConfigFileChangeEvent
;
import
com.google.common.util.concurrent.SettableFuture
;
import
java.util.Properties
;
import
java.util.concurrent.TimeUnit
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -84,10 +90,27 @@ public class PropertiesConfigFileTest {
Properties
anotherProperties
=
new
Properties
();
anotherProperties
.
setProperty
(
someKey
,
anotherValue
);
final
SettableFuture
<
ConfigFileChangeEvent
>
configFileChangeFuture
=
SettableFuture
.
create
();
ConfigFileChangeListener
someListener
=
new
ConfigFileChangeListener
()
{
@Override
public
void
onChange
(
ConfigFileChangeEvent
changeEvent
)
{
configFileChangeFuture
.
set
(
changeEvent
);
}
};
configFile
.
addChangeListener
(
someListener
);
configFile
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
ConfigFileChangeEvent
changeEvent
=
configFileChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
assertFalse
(
configFile
.
getContent
().
contains
(
String
.
format
(
"%s=%s"
,
someKey
,
someValue
)));
assertTrue
(
configFile
.
getContent
().
contains
(
String
.
format
(
"%s=%s"
,
someKey
,
anotherValue
)));
assertEquals
(
someNamespace
,
changeEvent
.
getNamespace
());
assertTrue
(
changeEvent
.
getOldValue
().
contains
(
String
.
format
(
"%s=%s"
,
someKey
,
someValue
)));
assertTrue
(
changeEvent
.
getNewValue
().
contains
(
String
.
format
(
"%s=%s"
,
someKey
,
anotherValue
)));
assertEquals
(
PropertyChangeType
.
MODIFIED
,
changeEvent
.
getChangeType
());
}
@Test
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java
浏览文件 @
942fffdf
...
...
@@ -6,8 +6,13 @@ import static org.junit.Assert.assertNull;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.ConfigFileChangeListener
;
import
com.ctrip.framework.apollo.enums.PropertyChangeType
;
import
com.ctrip.framework.apollo.model.ConfigFileChangeEvent
;
import
com.google.common.util.concurrent.SettableFuture
;
import
java.util.Properties
;
import
java.util.concurrent.TimeUnit
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -85,9 +90,97 @@ public class XmlConfigFileTest {
Properties
anotherProperties
=
new
Properties
();
anotherProperties
.
setProperty
(
key
,
anotherValue
);
final
SettableFuture
<
ConfigFileChangeEvent
>
configFileChangeFuture
=
SettableFuture
.
create
();
ConfigFileChangeListener
someListener
=
new
ConfigFileChangeListener
()
{
@Override
public
void
onChange
(
ConfigFileChangeEvent
changeEvent
)
{
configFileChangeFuture
.
set
(
changeEvent
);
}
};
configFile
.
addChangeListener
(
someListener
);
configFile
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
ConfigFileChangeEvent
changeEvent
=
configFileChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
assertEquals
(
anotherValue
,
configFile
.
getContent
());
assertEquals
(
someNamespace
,
changeEvent
.
getNamespace
());
assertEquals
(
someValue
,
changeEvent
.
getOldValue
());
assertEquals
(
anotherValue
,
changeEvent
.
getNewValue
());
assertEquals
(
PropertyChangeType
.
MODIFIED
,
changeEvent
.
getChangeType
());
}
@Test
public
void
testOnRepositoryChangeWithContentAdded
()
throws
Exception
{
Properties
someProperties
=
new
Properties
();
String
key
=
ConfigConsts
.
CONFIG_FILE_CONTENT_KEY
;
String
someValue
=
"someValue"
;
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
XmlConfigFile
configFile
=
new
XmlConfigFile
(
someNamespace
,
configRepository
);
assertEquals
(
null
,
configFile
.
getContent
());
Properties
anotherProperties
=
new
Properties
();
anotherProperties
.
setProperty
(
key
,
someValue
);
final
SettableFuture
<
ConfigFileChangeEvent
>
configFileChangeFuture
=
SettableFuture
.
create
();
ConfigFileChangeListener
someListener
=
new
ConfigFileChangeListener
()
{
@Override
public
void
onChange
(
ConfigFileChangeEvent
changeEvent
)
{
configFileChangeFuture
.
set
(
changeEvent
);
}
};
configFile
.
addChangeListener
(
someListener
);
configFile
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
ConfigFileChangeEvent
changeEvent
=
configFileChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someNamespace
,
changeEvent
.
getNamespace
());
assertEquals
(
null
,
changeEvent
.
getOldValue
());
assertEquals
(
someValue
,
changeEvent
.
getNewValue
());
assertEquals
(
PropertyChangeType
.
ADDED
,
changeEvent
.
getChangeType
());
}
@Test
public
void
testOnRepositoryChangeWithContentDeleted
()
throws
Exception
{
Properties
someProperties
=
new
Properties
();
String
key
=
ConfigConsts
.
CONFIG_FILE_CONTENT_KEY
;
String
someValue
=
"someValue"
;
someProperties
.
setProperty
(
key
,
someValue
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
XmlConfigFile
configFile
=
new
XmlConfigFile
(
someNamespace
,
configRepository
);
assertEquals
(
someValue
,
configFile
.
getContent
());
Properties
anotherProperties
=
new
Properties
();
final
SettableFuture
<
ConfigFileChangeEvent
>
configFileChangeFuture
=
SettableFuture
.
create
();
ConfigFileChangeListener
someListener
=
new
ConfigFileChangeListener
()
{
@Override
public
void
onChange
(
ConfigFileChangeEvent
changeEvent
)
{
configFileChangeFuture
.
set
(
changeEvent
);
}
};
configFile
.
addChangeListener
(
someListener
);
configFile
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
ConfigFileChangeEvent
changeEvent
=
configFileChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
assertEquals
(
null
,
configFile
.
getContent
());
assertEquals
(
someNamespace
,
changeEvent
.
getNamespace
());
assertEquals
(
someValue
,
changeEvent
.
getOldValue
());
assertEquals
(
null
,
changeEvent
.
getNewValue
());
assertEquals
(
PropertyChangeType
.
DELETED
,
changeEvent
.
getChangeType
());
}
@Test
...
...
apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/api/ApolloConfigDemo.java
浏览文件 @
942fffdf
...
...
@@ -5,10 +5,12 @@ import com.google.common.base.Charsets;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.ConfigChangeListener
;
import
com.ctrip.framework.apollo.ConfigFile
;
import
com.ctrip.framework.apollo.ConfigFileChangeListener
;
import
com.ctrip.framework.apollo.ConfigService
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.model.ConfigChange
;
import
com.ctrip.framework.apollo.model.ConfigChangeEvent
;
import
com.ctrip.framework.apollo.model.ConfigFileChangeEvent
;
import
com.ctrip.framework.foundation.Foundation
;
import
org.slf4j.Logger
;
...
...
@@ -48,6 +50,12 @@ public class ApolloConfigDemo {
publicConfig
.
addChangeListener
(
changeListener
);
applicationConfigFile
=
ConfigService
.
getConfigFile
(
"application"
,
ConfigFileFormat
.
Properties
);
xmlConfigFile
=
ConfigService
.
getConfigFile
(
"datasources"
,
ConfigFileFormat
.
XML
);
xmlConfigFile
.
addChangeListener
(
new
ConfigFileChangeListener
()
{
@Override
public
void
onChange
(
ConfigFileChangeEvent
changeEvent
)
{
logger
.
info
(
changeEvent
.
toString
());
}
});
}
private
String
getConfig
(
String
key
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录