Skip to content

Commit

Permalink
optimize: disable listening in the FileConfiguration center in Spring…
Browse files Browse the repository at this point in the history
…boot (apache#3991)
  • Loading branch information
funky-eyes authored Sep 1, 2021
1 parent 33b08cf commit 30c2c75
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changes/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#3954](https://github.com/seata/seata/pull/3954)] 移除对druid依赖中过期方法的调用
- [[#3981](https://github.com/seata/seata/pull/3981)] 优化服务端口的优先级设置
- [[#3982](https://github.com/seata/seata/pull/3982)] 优化 readme 文档和升级POM依赖
- [[#3991](https://github.com/seata/seata/pull/3991)] 关闭spring boot下无用的fileListener


### test:


Expand Down
1 change: 1 addition & 0 deletions changes/en-us/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
- [[#3981](https://github.com/seata/seata/pull/3981)] optimize service port priority
- [[#3982](https://github.com/seata/seata/pull/3982)] optimize readme doc and upgrade some dependencies
- [[#3949](https://github.com/seata/seata/pull/3949)] `nacos-config.py` support default parameters and optional input parameters
- [[#3991](https://github.com/seata/seata/pull/3991)] disable listening in the FileConfiguration center in Springboot

### test:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ public synchronized void addListener(String dataId, ConfigurationChangeListener

@Override
public void onChangeEvent(ConfigurationChangeEvent event) {
while (true) {
Boolean enabled = Boolean.valueOf(System.getProperty("file.listener.enabled", "true"));
while (enabled) {
for (String dataId : dataIdMap.keySet()) {
try {
String currentConfig =
Expand All @@ -416,6 +417,7 @@ public void onChangeEvent(ConfigurationChangeEvent event) {
} catch (InterruptedException e) {
LOGGER.error("fileListener thread sleep error:{}", e.getMessage());
}
enabled = Boolean.valueOf(System.getProperty("file.listener.enabled", "true"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package io.seata.config;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -43,12 +41,18 @@ void addConfigListener() throws InterruptedException {
Configuration fileConfig = ConfigurationFactory.getInstance();
CountDownLatch countDownLatch = new CountDownLatch(1);
boolean value = fileConfig.getBoolean("service.disableGlobalTransaction");
fileConfig.addConfigListener("service.disableGlobalTransaction", (event) -> {
ConfigurationCache.addConfigListener("service.disableGlobalTransaction", (event) -> {
Assertions.assertEquals(Boolean.parseBoolean(event.getNewValue()), !Boolean.parseBoolean(event.getOldValue()));
countDownLatch.countDown();
});
System.setProperty("service.disableGlobalTransaction", String.valueOf(!value));
Assertions.assertTrue(countDownLatch.await(2000, TimeUnit.MILLISECONDS));
countDownLatch.await();
System.setProperty("file.listener.enabled", "false");
System.setProperty("service.disableGlobalTransaction", String.valueOf(value));
Thread.sleep(2000);
boolean currentValue = fileConfig.getBoolean("service.disableGlobalTransaction");
Assertions.assertNotEquals(value, currentValue);
System.setProperty("service.disableGlobalTransaction", String.valueOf(!value));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
* The type spring application context provider
*/
public class SpringApplicationContextProvider implements ApplicationContextAware {

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.setProperty("file.listener.enabled", "false");
ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_APPLICATION_CONTEXT, applicationContext);
}

}

0 comments on commit 30c2c75

Please sign in to comment.