From 59f9fc0151ded391cd76697eb37bb9721d46d0dc Mon Sep 17 00:00:00 2001 From: Alonexc Date: Mon, 15 Apr 2024 11:19:24 +0800 Subject: [PATCH] optimisation --- .../core/function/SDK/SDKManager.java | 11 +++- .../function/SDK/config/CreateEtcdConfig.java | 3 +- .../SDK/config/CreateRedisConfig.java | 3 +- .../operation/EtcdSDKCreateOperationTest.java | 50 ++++++------------- .../RedisSDKCreateOperationTest.java | 3 +- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java index a84274b4..e8e722ea 100644 --- a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java +++ b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java @@ -46,10 +46,17 @@ */ public class SDKManager { - private static final SDKManager INSTANCE = new SDKManager(); + private static volatile SDKManager INSTANCE = null; - public static SDKManager getInstance() { + public static synchronized SDKManager getInstance() { + if (INSTANCE == null) { + synchronized (SDKManager.class) { + if (INSTANCE == null) { + INSTANCE = new SDKManager(); + } + } + } return INSTANCE; } diff --git a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java index 9a3e1d64..2f116e06 100644 --- a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java +++ b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java @@ -30,7 +30,8 @@ public class CreateEtcdConfig implements CreateSDKConfig { private String etcdServerAddress; - private Long connectTime; + @Builder.Default() + private int connectTime = 10; @Override public String getUniqueKey() { diff --git a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateRedisConfig.java b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateRedisConfig.java index ec23c483..fb42f880 100644 --- a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateRedisConfig.java +++ b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateRedisConfig.java @@ -32,7 +32,8 @@ public class CreateRedisConfig implements CreateSDKConfig { private String password; - private Integer timeOut; + @Builder.Default + private int timeOut = 10; @Override public String getUniqueKey() { diff --git a/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java b/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java index 04c2c189..13afca1b 100644 --- a/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java +++ b/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java @@ -19,17 +19,12 @@ import org.apache.eventmesh.dashboard.core.function.SDK.config.CreateEtcdConfig; -import java.nio.charset.StandardCharsets; import java.util.AbstractMap.SimpleEntry; -import java.util.List; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.KV; -import io.etcd.jetcd.KeyValue; -import io.etcd.jetcd.kv.GetResponse; import lombok.extern.slf4j.Slf4j; @@ -38,39 +33,24 @@ public class EtcdSDKCreateOperationTest { private final EtcdSDKOperation etcdSDKOperation = new EtcdSDKOperation(); - private static final String key = "/test/foo"; - - private static final String value = "test"; - private static final String url = "http://localhost:2379"; @Test void testCreateClient() { - // final CreateEtcdConfig etcdConfig = CreateEtcdConfig.builder() - // .etcdServerAddress(url) - // .connectTime(10L) - // .build(); - // // etcdConfig.setEtcdServerAddress(url); - // SimpleEntry simpleEntry = null; - // try { - // simpleEntry = etcdSDKOperation.createClient(etcdConfig); - // Assertions.assertEquals(url, simpleEntry.getKey()); - // simpleEntry.getValue().put(bytesOf(key), bytesOf(value)); - // final GetResponse response = simpleEntry.getValue().get(bytesOf(key)).get(); - // final List keyValues = response.getKvs(); - // log.info("get key = {} , value = {} from etcd success", - // keyValues.get(0).getKey().toString(StandardCharsets.UTF_8), - // keyValues.get(0).getValue().toString(StandardCharsets.UTF_8)); - // simpleEntry.getValue().close(); - // } catch (Exception e) { - // log.error("create etcd client failed", e); - // if (simpleEntry != null) { - // simpleEntry.getValue().close(); - // } - // } - } - - private static ByteSequence bytesOf(String val) { - return ByteSequence.from(val, StandardCharsets.UTF_8); + final CreateEtcdConfig etcdConfig = CreateEtcdConfig.builder() + .etcdServerAddress(url) + .connectTime(5) + .build(); + SimpleEntry simpleEntry = null; + try { + simpleEntry = etcdSDKOperation.createClient(etcdConfig); + Assertions.assertEquals(url, simpleEntry.getKey()); + simpleEntry.getValue().close(); + } catch (Exception e) { + log.error("create etcd client failed", e); + if (simpleEntry != null) { + simpleEntry.getValue().close(); + } + } } } diff --git a/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/RedisSDKCreateOperationTest.java b/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/RedisSDKCreateOperationTest.java index 050c6b07..2129d74f 100644 --- a/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/RedisSDKCreateOperationTest.java +++ b/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/RedisSDKCreateOperationTest.java @@ -32,7 +32,7 @@ @Slf4j class RedisSDKCreateOperationTest { - private RedisSDKOperation redisClientCreateOperation = new RedisSDKOperation(); + private final RedisSDKOperation redisClientCreateOperation = new RedisSDKOperation(); @Test void testCreateClient() { @@ -41,7 +41,6 @@ void testCreateClient() { .password("") .timeOut(5) .build(); - // createClientConfig.setRedisUrl("redis://localhost:6379"); SimpleEntry> simpleEntry = null; try { simpleEntry = redisClientCreateOperation.createClient(createClientConfig);