From 89cd0ba420b86ee3ef281b8614e77ff23ef5f5bd Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 2 Sep 2022 21:26:13 +0800 Subject: [PATCH] Optimize code format --- .../hippo4j/common/toolkit/JSONUtilTest.java | 18 ++- .../pom.xml | 94 ++++++----- .../etcd/ConfigEtcdExampleApplication.java | 12 +- .../config/etcd/config/ThreadPoolConfig.java | 38 ++--- .../etcd/controller/TestController.java | 25 ++- .../src/main/resources/application.properties | 6 +- hippo4j-example/pom.xml | 4 +- .../refresher/EtcdRefresherHandler.java | 153 ++++++++---------- 8 files changed, 164 insertions(+), 186 deletions(-) diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java index 62ea2f17..a0170f22 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java @@ -53,12 +53,14 @@ public class JSONUtilTest { @Test public void assertParseObjectTypeReference() { - Assert.assertNull(JSONUtil.parseObject(null, new TypeReference>() {})); - Assert.assertNull(JSONUtil.parseObject(" ", new TypeReference>() {})); + Assert.assertNull(JSONUtil.parseObject(null, new TypeReference>() { + })); + Assert.assertNull(JSONUtil.parseObject(" ", new TypeReference>() { + })); Assert.assertEquals( - EXPECTED_FOO_ARRAY, - JSONUtil.parseObject(EXPECTED_FOO_JSON_ARRAY, new TypeReference>() {}) - ); + EXPECTED_FOO_ARRAY, + JSONUtil.parseObject(EXPECTED_FOO_JSON_ARRAY, new TypeReference>() { + })); } @Test @@ -66,9 +68,8 @@ public class JSONUtilTest { Assert.assertNull(JSONUtil.parseArray(null, Foo.class)); Assert.assertNull(JSONUtil.parseArray(" ", Foo.class)); Assert.assertEquals( - EXPECTED_FOO_ARRAY, - JSONUtil.parseArray(EXPECTED_FOO_JSON_ARRAY, Foo.class) - ); + EXPECTED_FOO_ARRAY, + JSONUtil.parseArray(EXPECTED_FOO_JSON_ARRAY, Foo.class)); } @EqualsAndHashCode @@ -76,6 +77,7 @@ public class JSONUtilTest { @NoArgsConstructor @Data private static class Foo { + private Integer id; private String name; private Foo foo; diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml index 8ac150a8..c31e7f3c 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml @@ -1,49 +1,47 @@ - - - hippo4j-example - cn.hippo4j - ${revision} - - 4.0.0 - - hippo4j-config-etcd-spring-boot-starter-example - - - 11 - 11 - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-logging - - - - cn.hippo4j - hippo4j-config-spring-boot-starter - ${revision} - - - - io.etcd - jetcd-core - ${jetcd.version} - - - - cn.hippo4j - hippo4j-example-core - ${revision} - - - - \ No newline at end of file + + 4.0.0 + + cn.hippo4j + hippo4j-example + ${revision} + + hippo4j-config-etcd-spring-boot-starter-example + + + 11 + 11 + true + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-logging + + + + cn.hippo4j + hippo4j-config-spring-boot-starter + ${revision} + + + + io.etcd + jetcd-core + ${jetcd.version} + + + + cn.hippo4j + hippo4j-example-core + ${revision} + + + diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java index ad793091..3d670e1b 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java @@ -23,15 +23,15 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** - *@author : wh - *@date : 2022/9/2 19:06 - *@description: + * @author : wh + * @date : 2022/9/2 19:06 + * @description: */ @EnableDynamicThreadPool @SpringBootApplication(scanBasePackages = "cn.hippo4j.example.config") public class ConfigEtcdExampleApplication { - public static void main(String[] args) { - SpringApplication.run(ConfigEtcdExampleApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ConfigEtcdExampleApplication.class, args); + } } diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java index 5a57c743..71e6d943 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java @@ -17,36 +17,30 @@ package cn.hippo4j.example.config.etcd.config; -import java.util.concurrent.ThreadPoolExecutor; - import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.support.ThreadPoolBuilder; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.concurrent.ThreadPoolExecutor; + /** - *@author : wh - *@date : 2022/9/2 19:26 - *@description: + * @author : wh + * @date : 2022/9/2 19:26 + * @description: */ @Configuration public class ThreadPoolConfig { - - @Bean - @DynamicThreadPool - public ThreadPoolExecutor messageConsumeDynamicExecutor() { - String threadPoolId = "message-consume"; - ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder() - .threadFactory(threadPoolId) - .threadPoolId(threadPoolId) - .dynamicPool() - .build(); - return messageConsumeDynamicExecutor; - } - - - - + @Bean + @DynamicThreadPool + public ThreadPoolExecutor messageConsumeDynamicExecutor() { + String threadPoolId = "message-consume"; + ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder() + .threadFactory(threadPoolId) + .threadPoolId(threadPoolId) + .dynamicPool() + .build(); + return messageConsumeDynamicExecutor; + } } diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java index 947bb610..e073fdb3 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java @@ -17,30 +17,27 @@ package cn.hippo4j.example.config.etcd.controller; -import java.util.concurrent.ThreadPoolExecutor; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.concurrent.ThreadPoolExecutor; + /** - *@author : wh - *@date : 2022/9/2 19:18 - *@description: + * @author : wh + * @date : 2022/9/2 19:18 + * @description: */ @RestController @RequestMapping public class TestController { + @Autowired + private ThreadPoolExecutor messageConsumeDynamicExecutor; - @Autowired - private ThreadPoolExecutor messageConsumeDynamicExecutor; - - - @GetMapping("test") - public void test() { - System.out.println(messageConsumeDynamicExecutor.getMaximumPoolSize()); - } - + @GetMapping("test") + public void test() { + System.out.println(messageConsumeDynamicExecutor.getMaximumPoolSize()); + } } diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties index d5c19eca..1abd7216 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties @@ -1,8 +1,8 @@ server.port=8888 spring.application.name=etcd -spring.dynamic.thread-pool.etcd.endpoints = http://127.0.0.1:2379 -spring.dynamic.thread-pool.etcd.key = /thread +spring.dynamic.thread-pool.etcd.endpoints= http://127.0.0.1:2379 +spring.dynamic.thread-pool.etcd.key= /thread spring.dynamic.thread-pool.executors[0].thread-pool-id=message-consume spring.dynamic.thread-pool.executors[0].core-pool-size=3 spring.dynamic.thread-pool.executors[0].maximum-pool-size=4 @@ -20,4 +20,4 @@ spring.dynamic.thread-pool.executors[0].notify.interval=8 spring.dynamic.thread-pool.executors[0].notify.receives=111 spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT -spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff \ No newline at end of file +spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff diff --git a/hippo4j-example/pom.xml b/hippo4j-example/pom.xml index 55448c1c..93dab68b 100644 --- a/hippo4j-example/pom.xml +++ b/hippo4j-example/pom.xml @@ -22,8 +22,8 @@ hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rabbitmq-example hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example hippo4j-spring-boot-starter-adapter-rocketmq-example - hippo4j-config-etcd-spring-boot-starter-example - + hippo4j-config-etcd-spring-boot-starter-example + true diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java index e99eb463..a8ef886f 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java @@ -17,100 +17,87 @@ package cn.hippo4j.config.springboot.starter.refresher; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import java.util.Objects; - import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.StringUtil; -import io.etcd.jetcd.ByteSequence; -import io.etcd.jetcd.Client; -import io.etcd.jetcd.ClientBuilder; -import io.etcd.jetcd.KeyValue; -import io.etcd.jetcd.Watch; +import io.etcd.jetcd.*; import io.etcd.jetcd.kv.GetResponse; import io.etcd.jetcd.watch.WatchEvent; import io.etcd.jetcd.watch.WatchResponse; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.ApplicationContext; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Objects; /** - *@author : wh - *@date : 2022/8/30 17:59 - *@description: + * @author : wh + * @date : 2022/8/30 17:59 + * @description: */ @Slf4j public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { - private ApplicationContext applicationContext; - - private Client client; - - private static final String ENDPOINTS = "endpoints"; - - private static final String USER = "user"; - - private static final String PASSWORD = "password"; - - private static final String CHARSET = "charset"; - - private static final String AUTHORITY = "authority"; - - private static final String KEY = "key"; - - @Override - public void afterPropertiesSet() throws Exception { - Map etcd = bootstrapConfigProperties.getEtcd(); - String user = etcd.get(USER); - String password = etcd.get(PASSWORD); - String endpoints = etcd.get(ENDPOINTS); - String authority = etcd.get(AUTHORITY); - String key = etcd.get(KEY); - Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); - - ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(",")); - //todo - if (Objects.isNull(client)) { - client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset)) - .password(ByteSequence.from(password, charset)).authority(authority) - .build() : clientBuilder.build(); - } - - // todo Currently only supports json - GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get(); - KeyValue keyValue = getResponse.getKvs().get(0); - if (Objects.isNull(keyValue)) { - return; - } - client.getWatchClient().watch(ByteSequence.from(key, charset), new Watch.Listener() { - - @Override - public void onNext(WatchResponse response) { - WatchEvent watchEvent = response.getEvents().get(0); - WatchEvent.EventType eventType = watchEvent.getEventType(); - // todo Currently only supports json - if (Objects.equals(eventType, WatchEvent.EventType.PUT)) { - KeyValue keyValue1 = watchEvent.getKeyValue(); - String value = keyValue1.getValue().toString(charset); - Map map = JSONUtil.parseObject(value, Map.class); - dynamicRefresh(keyValue1.getKey().toString(charset), map); - } - - } - - @Override - public void onError(Throwable throwable) { - log.error("dynamic thread pool etcd config watcher exception ", throwable); - } - - @Override - public void onCompleted() { - log.info("dynamic thread pool etcd config key refreshed, config key {}", key); - } - }); - - } - + private Client client; + + private static final String ENDPOINTS = "endpoints"; + + private static final String USER = "user"; + + private static final String PASSWORD = "password"; + + private static final String CHARSET = "charset"; + + private static final String AUTHORITY = "authority"; + + private static final String KEY = "key"; + + @Override + public void afterPropertiesSet() throws Exception { + Map etcd = bootstrapConfigProperties.getEtcd(); + String user = etcd.get(USER); + String password = etcd.get(PASSWORD); + String endpoints = etcd.get(ENDPOINTS); + String authority = etcd.get(AUTHORITY); + String key = etcd.get(KEY); + Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); + ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(",")); + // todo + if (Objects.isNull(client)) { + client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset)) + .password(ByteSequence.from(password, charset)).authority(authority) + .build() : clientBuilder.build(); + } + // todo Currently only supports json + GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get(); + KeyValue keyValue = getResponse.getKvs().get(0); + if (Objects.isNull(keyValue)) { + return; + } + client.getWatchClient().watch(ByteSequence.from(key, charset), new Watch.Listener() { + + @Override + public void onNext(WatchResponse response) { + WatchEvent watchEvent = response.getEvents().get(0); + WatchEvent.EventType eventType = watchEvent.getEventType(); + // todo Currently only supports json + if (Objects.equals(eventType, WatchEvent.EventType.PUT)) { + KeyValue keyValue1 = watchEvent.getKeyValue(); + String value = keyValue1.getValue().toString(charset); + Map map = JSONUtil.parseObject(value, Map.class); + dynamicRefresh(keyValue1.getKey().toString(charset), map); + } + } + + @Override + public void onError(Throwable throwable) { + log.error("Dynamic thread pool etcd config watcher exception ", throwable); + } + + @Override + public void onCompleted() { + log.info("Dynamic thread pool etcd config key refreshed, config key {}", key); + } + }); + } } \ No newline at end of file