Optimize code format

pull/639/head
chen.ma 3 years ago
parent b8059899ad
commit 89cd0ba420

@ -53,12 +53,14 @@ public class JSONUtilTest {
@Test @Test
public void assertParseObjectTypeReference() { public void assertParseObjectTypeReference() {
Assert.assertNull(JSONUtil.parseObject(null, new TypeReference<List<Foo>>() {})); Assert.assertNull(JSONUtil.parseObject(null, new TypeReference<List<Foo>>() {
Assert.assertNull(JSONUtil.parseObject(" ", new TypeReference<List<Foo>>() {})); }));
Assert.assertNull(JSONUtil.parseObject(" ", new TypeReference<List<Foo>>() {
}));
Assert.assertEquals( Assert.assertEquals(
EXPECTED_FOO_ARRAY, EXPECTED_FOO_ARRAY,
JSONUtil.parseObject(EXPECTED_FOO_JSON_ARRAY, new TypeReference<List<Foo>>() {}) JSONUtil.parseObject(EXPECTED_FOO_JSON_ARRAY, new TypeReference<List<Foo>>() {
); }));
} }
@Test @Test
@ -66,9 +68,8 @@ public class JSONUtilTest {
Assert.assertNull(JSONUtil.parseArray(null, Foo.class)); Assert.assertNull(JSONUtil.parseArray(null, Foo.class));
Assert.assertNull(JSONUtil.parseArray(" ", Foo.class)); Assert.assertNull(JSONUtil.parseArray(" ", Foo.class));
Assert.assertEquals( Assert.assertEquals(
EXPECTED_FOO_ARRAY, EXPECTED_FOO_ARRAY,
JSONUtil.parseArray(EXPECTED_FOO_JSON_ARRAY, Foo.class) JSONUtil.parseArray(EXPECTED_FOO_JSON_ARRAY, Foo.class));
);
} }
@EqualsAndHashCode @EqualsAndHashCode
@ -76,6 +77,7 @@ public class JSONUtilTest {
@NoArgsConstructor @NoArgsConstructor
@Data @Data
private static class Foo { private static class Foo {
private Integer id; private Integer id;
private String name; private String name;
private Foo foo; private Foo foo;

@ -1,49 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>hippo4j-example</artifactId> <groupId>cn.hippo4j</groupId>
<groupId>cn.hippo4j</groupId> <artifactId>hippo4j-example</artifactId>
<version>${revision}</version> <version>${revision}</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <artifactId>hippo4j-config-etcd-spring-boot-starter-example</artifactId>
<artifactId>hippo4j-config-etcd-spring-boot-starter-example</artifactId> <properties>
<maven.compiler.source>11</maven.compiler.source>
<properties> <maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source> <maven.deploy.skip>true</maven.deploy.skip>
<maven.compiler.target>11</maven.compiler.target> </properties>
</properties>
<dependencies>
<dependencies> <dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
</dependency>
<dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId>
<artifactId>spring-boot-starter-logging</artifactId> </dependency>
</dependency>
<dependency>
<dependency> <groupId>cn.hippo4j</groupId>
<groupId>cn.hippo4j</groupId> <artifactId>hippo4j-config-spring-boot-starter</artifactId>
<artifactId>hippo4j-config-spring-boot-starter</artifactId> <version>${revision}</version>
<version>${revision}</version> </dependency>
</dependency>
<dependency>
<dependency> <groupId>io.etcd</groupId>
<groupId>io.etcd</groupId> <artifactId>jetcd-core</artifactId>
<artifactId>jetcd-core</artifactId> <version>${jetcd.version}</version>
<version>${jetcd.version}</version> </dependency>
</dependency>
<dependency>
<dependency> <groupId>cn.hippo4j</groupId>
<groupId>cn.hippo4j</groupId> <artifactId>hippo4j-example-core</artifactId>
<artifactId>hippo4j-example-core</artifactId> <version>${revision}</version>
<version>${revision}</version> </dependency>
</dependency> </dependencies>
</dependencies> </project>
</project>

@ -23,15 +23,15 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
*@author : wh * @author : wh
*@date : 2022/9/2 19:06 * @date : 2022/9/2 19:06
*@description: * @description:
*/ */
@EnableDynamicThreadPool @EnableDynamicThreadPool
@SpringBootApplication(scanBasePackages = "cn.hippo4j.example.config") @SpringBootApplication(scanBasePackages = "cn.hippo4j.example.config")
public class ConfigEtcdExampleApplication { public class ConfigEtcdExampleApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ConfigEtcdExampleApplication.class, args); SpringApplication.run(ConfigEtcdExampleApplication.class, args);
} }
} }

@ -17,36 +17,30 @@
package cn.hippo4j.example.config.etcd.config; package cn.hippo4j.example.config.etcd.config;
import java.util.concurrent.ThreadPoolExecutor;
import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.DynamicThreadPool;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder; import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ThreadPoolExecutor;
/** /**
*@author : wh * @author : wh
*@date : 2022/9/2 19:26 * @date : 2022/9/2 19:26
*@description: * @description:
*/ */
@Configuration @Configuration
public class ThreadPoolConfig { public class ThreadPoolConfig {
@Bean
@Bean @DynamicThreadPool
@DynamicThreadPool public ThreadPoolExecutor messageConsumeDynamicExecutor() {
public ThreadPoolExecutor messageConsumeDynamicExecutor() { String threadPoolId = "message-consume";
String threadPoolId = "message-consume"; ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder()
ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder() .threadFactory(threadPoolId)
.threadFactory(threadPoolId) .threadPoolId(threadPoolId)
.threadPoolId(threadPoolId) .dynamicPool()
.dynamicPool() .build();
.build(); return messageConsumeDynamicExecutor;
return messageConsumeDynamicExecutor; }
}
} }

@ -17,30 +17,27 @@
package cn.hippo4j.example.config.etcd.controller; package cn.hippo4j.example.config.etcd.controller;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ThreadPoolExecutor;
/** /**
*@author : wh * @author : wh
*@date : 2022/9/2 19:18 * @date : 2022/9/2 19:18
*@description: * @description:
*/ */
@RestController @RestController
@RequestMapping @RequestMapping
public class TestController { public class TestController {
@Autowired
private ThreadPoolExecutor messageConsumeDynamicExecutor;
@Autowired @GetMapping("test")
private ThreadPoolExecutor messageConsumeDynamicExecutor; public void test() {
System.out.println(messageConsumeDynamicExecutor.getMaximumPoolSize());
}
@GetMapping("test")
public void test() {
System.out.println(messageConsumeDynamicExecutor.getMaximumPoolSize());
}
} }

@ -1,8 +1,8 @@
server.port=8888 server.port=8888
spring.application.name=etcd spring.application.name=etcd
spring.dynamic.thread-pool.etcd.endpoints = http://127.0.0.1:2379 spring.dynamic.thread-pool.etcd.endpoints= http://127.0.0.1:2379
spring.dynamic.thread-pool.etcd.key = /thread spring.dynamic.thread-pool.etcd.key= /thread
spring.dynamic.thread-pool.executors[0].thread-pool-id=message-consume 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].core-pool-size=3
spring.dynamic.thread-pool.executors[0].maximum-pool-size=4 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.executors[0].notify.receives=111
spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT
spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff

@ -22,8 +22,8 @@
<module>hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rabbitmq-example</module> <module>hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rabbitmq-example</module>
<module>hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example</module> <module>hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example</module>
<module>hippo4j-spring-boot-starter-adapter-rocketmq-example</module> <module>hippo4j-spring-boot-starter-adapter-rocketmq-example</module>
<module>hippo4j-config-etcd-spring-boot-starter-example</module> <module>hippo4j-config-etcd-spring-boot-starter-example</module>
</modules> </modules>
<properties> <properties>
<maven.deploy.skip>true</maven.deploy.skip> <maven.deploy.skip>true</maven.deploy.skip>

@ -17,100 +17,87 @@
package cn.hippo4j.config.springboot.starter.refresher; 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.JSONUtil;
import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.StringUtil;
import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.*;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.ClientBuilder;
import io.etcd.jetcd.KeyValue;
import io.etcd.jetcd.Watch;
import io.etcd.jetcd.kv.GetResponse; import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.watch.WatchEvent; import io.etcd.jetcd.watch.WatchEvent;
import io.etcd.jetcd.watch.WatchResponse; import io.etcd.jetcd.watch.WatchResponse;
import lombok.extern.slf4j.Slf4j; 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 * @author : wh
*@date : 2022/8/30 17:59 * @date : 2022/8/30 17:59
*@description: * @description:
*/ */
@Slf4j @Slf4j
public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh {
private ApplicationContext applicationContext; private Client client;
private Client client; private static final String ENDPOINTS = "endpoints";
private static final String ENDPOINTS = "endpoints"; private static final String USER = "user";
private static final String USER = "user"; private static final String PASSWORD = "password";
private static final String PASSWORD = "password"; private static final String CHARSET = "charset";
private static final String CHARSET = "charset"; private static final String AUTHORITY = "authority";
private static final String AUTHORITY = "authority"; private static final String KEY = "key";
private static final String KEY = "key"; @Override
public void afterPropertiesSet() throws Exception {
@Override Map<String, String> etcd = bootstrapConfigProperties.getEtcd();
public void afterPropertiesSet() throws Exception { String user = etcd.get(USER);
Map<String, String> etcd = bootstrapConfigProperties.getEtcd(); String password = etcd.get(PASSWORD);
String user = etcd.get(USER); String endpoints = etcd.get(ENDPOINTS);
String password = etcd.get(PASSWORD); String authority = etcd.get(AUTHORITY);
String endpoints = etcd.get(ENDPOINTS); String key = etcd.get(KEY);
String authority = etcd.get(AUTHORITY); Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET));
String key = etcd.get(KEY); ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(","));
Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); // todo
if (Objects.isNull(client)) {
ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(",")); client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset))
//todo .password(ByteSequence.from(password, charset)).authority(authority)
if (Objects.isNull(client)) { .build() : clientBuilder.build();
client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset)) }
.password(ByteSequence.from(password, charset)).authority(authority) // todo Currently only supports json
.build() : clientBuilder.build(); GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get();
} KeyValue keyValue = getResponse.getKvs().get(0);
if (Objects.isNull(keyValue)) {
// todo Currently only supports json return;
GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get(); }
KeyValue keyValue = getResponse.getKvs().get(0); client.getWatchClient().watch(ByteSequence.from(key, charset), new Watch.Listener() {
if (Objects.isNull(keyValue)) {
return; @Override
} public void onNext(WatchResponse response) {
client.getWatchClient().watch(ByteSequence.from(key, charset), new Watch.Listener() { WatchEvent watchEvent = response.getEvents().get(0);
WatchEvent.EventType eventType = watchEvent.getEventType();
@Override // todo Currently only supports json
public void onNext(WatchResponse response) { if (Objects.equals(eventType, WatchEvent.EventType.PUT)) {
WatchEvent watchEvent = response.getEvents().get(0); KeyValue keyValue1 = watchEvent.getKeyValue();
WatchEvent.EventType eventType = watchEvent.getEventType(); String value = keyValue1.getValue().toString(charset);
// todo Currently only supports json Map map = JSONUtil.parseObject(value, Map.class);
if (Objects.equals(eventType, WatchEvent.EventType.PUT)) { dynamicRefresh(keyValue1.getKey().toString(charset), map);
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 @Override
public void onError(Throwable throwable) { public void onCompleted() {
log.error("dynamic thread pool etcd config watcher exception ", throwable); log.info("Dynamic thread pool etcd config key refreshed, config key {}", key);
} }
});
@Override }
public void onCompleted() {
log.info("dynamic thread pool etcd config key refreshed, config key {}", key);
}
});
}
} }
Loading…
Cancel
Save