Skip to content

Commit 7ca1cf6

Browse files
authored
ApiClient Pooling and Socket Exhaustion Prevention (#962)
* Add ApiClient pooling for AAS and Submodel repository and service clients * Prevent ApiClient from creating a new HttpClient if one already exists * Refactor ApiClient initialization so that only factory methods are used * Enhance ApiClient pooling by updating existing clients with new ObjectMapper * Update ObjectMapper instantiation in SubmodelServiceApiFactory
1 parent 42d183b commit 7ca1cf6

18 files changed

Lines changed: 646 additions & 13 deletions

File tree

basyx.aasrepository/basyx.aasrepository-client/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/client/AuthorizedConnectedAasRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
package org.eclipse.digitaltwin.basyx.aasrepository.client;
2727

28-
import org.eclipse.digitaltwin.basyx.aasrepository.client.internal.AssetAdministrationShellRepositoryApi;
28+
import org.eclipse.digitaltwin.basyx.aasrepository.client.internal.AssetAdministrationShellRepositoryApiFactory;
2929
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
3030
import org.eclipse.digitaltwin.basyx.aasservice.client.AuthorizedConnectedAasService;
3131
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
@@ -41,7 +41,7 @@ public class AuthorizedConnectedAasRepository extends ConnectedAasRepository {
4141
private TokenManager tokenManager;
4242

4343
public AuthorizedConnectedAasRepository(String repoUrl, TokenManager tokenManager) {
44-
super(repoUrl, new AssetAdministrationShellRepositoryApi(repoUrl, tokenManager));
44+
super(repoUrl, AssetAdministrationShellRepositoryApiFactory.create(repoUrl, tokenManager));
4545
this.tokenManager = tokenManager;
4646
}
4747

basyx.aasrepository/basyx.aasrepository-client/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/client/ConnectedAasRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.eclipse.digitaltwin.aas4j.v3.model.SpecificAssetId;
3939
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
4040
import org.eclipse.digitaltwin.basyx.aasrepository.client.internal.AssetAdministrationShellRepositoryApi;
41+
import org.eclipse.digitaltwin.basyx.aasrepository.client.internal.AssetAdministrationShellRepositoryApiFactory;
4142
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
4243
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
4344
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException;
@@ -67,7 +68,7 @@ public class ConnectedAasRepository implements AasRepository {
6768
*/
6869
public ConnectedAasRepository(String repoUrl) {
6970
this.aasRepoUrl = repoUrl;
70-
this.repoApi = new AssetAdministrationShellRepositoryApi(repoUrl);
71+
this.repoApi = AssetAdministrationShellRepositoryApiFactory.create(repoUrl);
7172
}
7273

7374
public ConnectedAasRepository(String repoUrl, AssetAdministrationShellRepositoryApi assetAdministrationShellRepositoryApi) {

basyx.aasrepository/basyx.aasrepository-client/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/client/internal/AssetAdministrationShellRepositoryApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation;
4242
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
4343
import org.eclipse.digitaltwin.basyx.client.internal.ApiClient;
44+
import org.eclipse.digitaltwin.basyx.client.internal.ApiClientPool;
4445
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
4546
import org.eclipse.digitaltwin.basyx.client.internal.ApiResponse;
4647
import org.eclipse.digitaltwin.basyx.client.internal.Pair;
@@ -100,6 +101,10 @@ public AssetAdministrationShellRepositoryApi(ApiClient apiClient) {
100101
memberVarReadTimeout = apiClient.getReadTimeout();
101102
}
102103

104+
public void setTokenManager(TokenManager tokenManager) {
105+
this.tokenManager = tokenManager;
106+
}
107+
103108
protected ApiException getApiException(String operationId, HttpResponse<String> response) throws IOException {
104109
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
105110
return new ApiException(response.statusCode(), message, response.headers(), response.body());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2026 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
27+
package org.eclipse.digitaltwin.basyx.aasrepository.client.internal;
28+
29+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonMapperFactory;
30+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.SimpleAbstractTypeResolverFactory;
31+
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
32+
import org.eclipse.digitaltwin.basyx.client.internal.ApiClient;
33+
import org.eclipse.digitaltwin.basyx.client.internal.ApiClientPool;
34+
35+
import com.fasterxml.jackson.databind.ObjectMapper;
36+
37+
/**
38+
* Factory class for creating instances of {@link AssetAdministrationShellRepositoryApi} using an {@link ApiClientPool}.
39+
*
40+
* @author koort
41+
*/
42+
public class AssetAdministrationShellRepositoryApiFactory {
43+
44+
private AssetAdministrationShellRepositoryApiFactory() {
45+
}
46+
47+
/**
48+
* Creates a new instance of {@link AssetAdministrationShellRepositoryApi} with default configuration.
49+
*
50+
* @param repositoryBaseUri the base URI of the AAS repository
51+
* @return a new AssetAdministrationShellRepositoryApi instance
52+
*/
53+
public static AssetAdministrationShellRepositoryApi create(String repositoryBaseUri) {
54+
return create(repositoryBaseUri, null, null);
55+
}
56+
57+
/**
58+
* Creates a new instance of {@link AssetAdministrationShellRepositoryApi} with the specified ObjectMapper.
59+
*
60+
* @param repositoryBaseUri the base URI of the AAS repository
61+
* @param objectMapper the ObjectMapper
62+
* @return a new AssetAdministrationShellRepositoryApi instance
63+
*/
64+
public static AssetAdministrationShellRepositoryApi create(String repositoryBaseUri, ObjectMapper objectMapper) {
65+
return create(repositoryBaseUri, objectMapper, null);
66+
}
67+
68+
/**
69+
* Creates a new instance of {@link AssetAdministrationShellRepositoryApi} with the specified TokenManager.
70+
*
71+
* @param repositoryBaseUri the base URI of the AAS repository
72+
* @param tokenManager the TokenManager
73+
* @return a new AssetAdministrationShellRepositoryApi instance
74+
*/
75+
public static AssetAdministrationShellRepositoryApi create(String repositoryBaseUri, TokenManager tokenManager) {
76+
return create(repositoryBaseUri, null, tokenManager);
77+
}
78+
79+
/**
80+
* Creates a new instance of {@link AssetAdministrationShellRepositoryApi} with the specified ObjectMapper and TokenManager.
81+
*
82+
* @param repositoryBaseUri the base URI of the AAS repository
83+
* @param objectMapper the ObjectMapper
84+
* @param tokenManager the TokenManager
85+
* @return a new AssetAdministrationShellRepositoryApi instance
86+
*/
87+
public static AssetAdministrationShellRepositoryApi create(String repositoryBaseUri, ObjectMapper objectMapper, TokenManager tokenManager) {
88+
ObjectMapper mapper = objectMapper != null ? objectMapper : new JsonMapperFactory().create(new SimpleAbstractTypeResolverFactory().create());
89+
90+
ApiClient apiClient = ApiClientPool.getInstance().getOrCreateAasRepoApiClient(repositoryBaseUri, mapper);
91+
92+
AssetAdministrationShellRepositoryApi repoApi = new AssetAdministrationShellRepositoryApi(apiClient);
93+
94+
if (tokenManager != null) {
95+
repoApi.setTokenManager(tokenManager);
96+
}
97+
98+
return repoApi;
99+
}
100+
}

basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/AuthorizedConnectedAasService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
package org.eclipse.digitaltwin.basyx.aasservice.client;
2727

28-
import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApi;
28+
import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApiFactory;
2929
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
3030

3131
/**
@@ -43,7 +43,7 @@ public class AuthorizedConnectedAasService extends ConnectedAasService {
4343
* @param tokenManager
4444
*/
4545
public AuthorizedConnectedAasService(String repoUrl, TokenManager tokenManager) {
46-
super(new AssetAdministrationShellServiceApi(repoUrl, tokenManager));
46+
super(AssetAdministrationShellServiceApiFactory.create(repoUrl, tokenManager));
4747
}
4848

4949
}

basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/ConnectedAasService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
3636
import org.eclipse.digitaltwin.basyx.aasservice.AasService;
3737
import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApi;
38+
import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApiFactory;
3839
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
3940
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingSubmodelReferenceException;
4041
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
@@ -56,7 +57,7 @@ public class ConnectedAasService implements AasService {
5657
private AssetAdministrationShellServiceApi serviceApi;
5758

5859
public ConnectedAasService(String aasServiceUrl) {
59-
this.serviceApi = new AssetAdministrationShellServiceApi(aasServiceUrl);
60+
this.serviceApi = AssetAdministrationShellServiceApiFactory.create(aasServiceUrl);
6061
}
6162

6263
public ConnectedAasService(AssetAdministrationShellServiceApi assetAdministrationShellServiceApi) {

basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/internal/AssetAdministrationShellServiceApi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public AssetAdministrationShellServiceApi(ApiClient apiClient) {
112112
memberVarReadTimeout = apiClient.getReadTimeout();
113113
}
114114

115+
public void setTokenManager(TokenManager tokenManager) {
116+
this.tokenManager = tokenManager;
117+
}
118+
115119
protected ApiException getApiException(String operationId, HttpResponse<String> response) throws IOException {
116120
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
117121
return new ApiException(response.statusCode(), message, response.headers(), response.body());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2026 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
27+
package org.eclipse.digitaltwin.basyx.aasservice.client.internal;
28+
29+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonMapperFactory;
30+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.SimpleAbstractTypeResolverFactory;
31+
import org.eclipse.digitaltwin.basyx.client.internal.ApiClient;
32+
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
33+
import org.eclipse.digitaltwin.basyx.client.internal.ApiClientPool;
34+
35+
import com.fasterxml.jackson.databind.ObjectMapper;
36+
37+
/**
38+
* Factory class for creating instances of {@link AssetAdministrationShellServiceApi} using an {@link ApiClientPool}.
39+
*
40+
* @author koort
41+
*/
42+
public class AssetAdministrationShellServiceApiFactory {
43+
44+
private AssetAdministrationShellServiceApiFactory() {
45+
}
46+
47+
/**
48+
* Creates a new instance of {@link AssetAdministrationShellServiceApi} with default configuration.
49+
*
50+
* @param aasServiceUrl the base URL of the AAS service
51+
* @return a new AssetAdministrationShellServiceApi instance
52+
*/
53+
public static AssetAdministrationShellServiceApi create(String aasServiceUrl) {
54+
return create(aasServiceUrl, null, null);
55+
}
56+
57+
/**
58+
* Creates a new instance of {@link AssetAdministrationShellServiceApi} with the specified TokenManager.
59+
*
60+
* @param aasServiceUrl the base URL of the AAS service
61+
* @param tokenManager the TokenManager
62+
* @return a new AssetAdministrationShellServiceApi instance
63+
*/
64+
public static AssetAdministrationShellServiceApi create(String aasServiceUrl, TokenManager tokenManager) {
65+
return create(aasServiceUrl, null, tokenManager);
66+
}
67+
68+
/**
69+
* Creates a new instance of {@link AssetAdministrationShellServiceApi} with the specified ObjectMapper.
70+
*
71+
* @param aasServiceUrl the base URL of the AAS service
72+
* @param objectMapper the ObjectMapper
73+
* @return a new AssetAdministrationShellServiceApi instance
74+
*/
75+
public static AssetAdministrationShellServiceApi create(String aasServiceUrl, ObjectMapper objectMapper) {
76+
return create(aasServiceUrl, objectMapper, null);
77+
}
78+
79+
/**
80+
* Creates a new instance of {@link AssetAdministrationShellServiceApi} with the specified ObjectMapper and TokenManager.
81+
*
82+
* @param aasServiceUrl the base URL of the AAS service
83+
* @param objectMapper the ObjectMapper
84+
* @param tokenManager the TokenManager
85+
* @return a new AssetAdministrationShellServiceApi instance
86+
*/
87+
public static AssetAdministrationShellServiceApi create(String aasServiceUrl, ObjectMapper objectMapper, TokenManager tokenManager) {
88+
ObjectMapper mapper = objectMapper != null ? objectMapper : new JsonMapperFactory().create(new SimpleAbstractTypeResolverFactory().create());
89+
90+
ApiClient apiClient = ApiClientPool.getInstance().getOrCreateAasServiceApiClient(aasServiceUrl, mapper);
91+
92+
AssetAdministrationShellServiceApi serviceApi = new AssetAdministrationShellServiceApi(apiClient);
93+
94+
if (tokenManager != null) {
95+
serviceApi.setTokenManager(tokenManager);
96+
}
97+
98+
return serviceApi;
99+
}
100+
}

basyx.common/basyx.client/src/main/java/org/eclipse/digitaltwin/basyx/client/internal/ApiClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class ApiClient {
8383
private Consumer<HttpResponse<String>> asyncResponseInterceptor;
8484
private Duration readTimeout;
8585
private Duration connectTimeout;
86+
private HttpClient httpClient;
8687

8788
private static String valueToString(Object value) {
8889
if (value == null) {
@@ -190,6 +191,7 @@ public ApiClient() {
190191
connectTimeout = null;
191192
responseInterceptor = null;
192193
asyncResponseInterceptor = null;
194+
this.httpClient = this.builder.build();
193195
}
194196

195197
/**
@@ -208,6 +210,7 @@ public ApiClient(HttpClient.Builder builder, ObjectMapper mapper, String baseUri
208210
connectTimeout = null;
209211
responseInterceptor = null;
210212
asyncResponseInterceptor = null;
213+
this.httpClient = this.builder.build();
211214
}
212215

213216
protected ObjectMapper createDefaultObjectMapper() {
@@ -249,6 +252,7 @@ public void updateBaseUri(String baseUri) {
249252
*/
250253
public ApiClient setHttpClientBuilder(HttpClient.Builder builder) {
251254
this.builder = builder;
255+
this.httpClient = null;
252256
return this;
253257
}
254258

@@ -260,7 +264,10 @@ public ApiClient setHttpClientBuilder(HttpClient.Builder builder) {
260264
* @return The HTTP client.
261265
*/
262266
public HttpClient getHttpClient() {
263-
return builder.build();
267+
if (httpClient == null) {
268+
httpClient = builder.build();
269+
}
270+
return httpClient;
264271
}
265272

266273
/**

0 commit comments

Comments
 (0)