From 7881766c3efdb661cd2944f192cfffdf325910ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=B2=E4=B8=9C=E6=9C=88?= Date: Sat, 10 Jun 2023 23:41:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E4=B8=8D=E5=90=8C=E7=AB=99?= =?UTF-8?q?=E7=82=B9=E8=B4=A6=E5=8F=B7=E5=A4=9A=E7=BA=BF=E7=A8=8Bsession?= =?UTF-8?q?=E7=B4=8A=E4=B9=B1=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/zstack/sdk/ZSClient.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/sdk/src/main/java/org/zstack/sdk/ZSClient.java b/sdk/src/main/java/org/zstack/sdk/ZSClient.java index a24f1f334b..efb45c150f 100755 --- a/sdk/src/main/java/org/zstack/sdk/ZSClient.java +++ b/sdk/src/main/java/org/zstack/sdk/ZSClient.java @@ -45,14 +45,14 @@ public class ZSClient { .toFormatter(Locale.ENGLISH); } - private static ZSConfig config; + private static ThreadLocal config = new ThreadLocal<>(); public static ZSConfig getConfig() { - return config; + return config.get(); } public static void configure(ZSConfig c) { - config = c; + config.set(c); if (c.readTimeout != null || c.writeTimeout != null) { OkHttpClient.Builder b = new OkHttpClient.Builder(); @@ -215,8 +215,8 @@ public class ZSClient { reqBuilder.addHeader(Constants.HEADER_API_TIMEOUT, action.apiTimeout.toString()); } - if (config.webHook != null) { - reqBuilder.addHeader(Constants.HEADER_WEBHOOK, config.webHook); + if (config.get().webHook != null) { + reqBuilder.addHeader(Constants.HEADER_WEBHOOK, config.get().webHook); } try { @@ -232,7 +232,7 @@ public class ZSClient { Request request = reqBuilder.build(); try { - if (config.webHook != null) { + if (config.get().webHook != null) { waittingApis.put(jobUuid, this); } @@ -245,7 +245,7 @@ public class ZSClient { return writeApiResult(response); } else if (response.code() == 202) { - if (config.webHook != null) { + if (config.get().webHook != null) { return webHookResult(); } else { return pollResult(response); @@ -312,11 +312,11 @@ public class ZSClient { QueryAction qaction = (QueryAction) action; HttpUrl.Builder urlBuilder = new HttpUrl.Builder().scheme("http") - .host(config.hostname) - .port(config.port); + .host(config.get().hostname) + .port(config.get().port); - if (config.contextPath != null) { - urlBuilder.addPathSegments(config.contextPath); + if (config.get().contextPath != null) { + urlBuilder.addPathSegments(config.get().contextPath); } urlBuilder.addPathSegment("v1") @@ -373,11 +373,11 @@ public class ZSClient { private void fillNonQueryApiRequestBuilder(Request.Builder reqBuilder) throws Exception { HttpUrl.Builder builder = new HttpUrl.Builder() .scheme("http") - .host(config.hostname) - .port(config.port); + .host(config.get().hostname) + .port(config.get().port); - if (config.contextPath != null) { - builder.addPathSegments(config.contextPath); + if (config.get().contextPath != null) { + builder.addPathSegments(config.get().contextPath); } // HttpUrl will add an extra / to the path segment @@ -500,7 +500,7 @@ public class ZSClient { " doesn't return the polling location url", action.getClass().getSimpleName())); } - String configHost = String.format("%s:%s", config.getHostname(), config.getPort()); + String configHost = String.format("%s:%s", config.get().getHostname(), config.get().getPort()); if (!pollingUrl.contains(configHost)) { String splitRegex = "/zstack/v1/api-jobs"; pollingUrl = String.format("http://%s%s%s", configHost, splitRegex ,pollingUrl.split(splitRegex)[1]); @@ -685,12 +685,12 @@ public class ZSClient { private long getTimeout(){ Long timeout = (Long)action.getNonAPIParameterValue("timeout", false); - return timeout == ACTION_DEFAULT_TIMEOUT ? config.defaultPollingTimeout : timeout; + return timeout == ACTION_DEFAULT_TIMEOUT ? config.get().defaultPollingTimeout : timeout; } private long getInterval(){ Long interval = (Long) action.getNonAPIParameterValue("pollingInterval", false); - return interval == ACTION_DEFAULT_POLLINGINTERVAL ? config.defaultPollingInterval : interval; + return interval == ACTION_DEFAULT_POLLINGINTERVAL ? config.get().defaultPollingInterval : interval; } } @@ -709,4 +709,4 @@ public class ZSClient { errorIfNotConfigured(); return new Api(action).call(); } -} \ No newline at end of file +} -- Gitee