From 76d0db1096d26d54d868002b2ce6a37d77f71e93 Mon Sep 17 00:00:00 2001 From: Ashan Rathnaweera Date: Tue, 19 Nov 2024 11:42:46 +0530 Subject: [PATCH 1/2] Refactor HTTP client usage to ensure proper connection handling --- .../store/TransactionRecordStoreImpl.java | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/counter/src/main/java/org/wso2/integration/transaction/counter/store/TransactionRecordStoreImpl.java b/counter/src/main/java/org/wso2/integration/transaction/counter/store/TransactionRecordStoreImpl.java index e88e23e..b93f70a 100644 --- a/counter/src/main/java/org/wso2/integration/transaction/counter/store/TransactionRecordStoreImpl.java +++ b/counter/src/main/java/org/wso2/integration/transaction/counter/store/TransactionRecordStoreImpl.java @@ -22,12 +22,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.wso2.integration.transaction.counter.record.TransactionRecord; import java.io.IOException; @@ -40,57 +40,61 @@ public class TransactionRecordStoreImpl implements TransactionRecordStore { private static final Log LOG = LogFactory.getLog(TransactionRecordStoreImpl.class); - private static HttpClient httpClient; - private static String ENDPOINT; - private static String encodedCredentials; + private CloseableHttpClient httpClient; + private String ENDPOINT; + private String encodedCredentials; @Override public void init(String endpoint, String username, String password) { - ENDPOINT = endpoint; String credentials = username + ":" + password; encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)); - URL url = null; try { - url = new URL(endpoint); + new URL(endpoint); } catch (MalformedURLException e) { - throw new RuntimeException(e); + throw new RuntimeException("Invalid endpoint URL: " + endpoint, e); } - httpClient = HttpClientBuilder.create().build(); + + httpClient = HttpClients.createDefault(); } @Override public boolean commit(ArrayList transactionRecordList, int maxRetryCount) { - Gson gson = new Gson(); String jsonPayload = gson.toJson(transactionRecordList); HttpPost httpPost = new HttpPost(ENDPOINT); - HttpEntity stringEntity = new StringEntity(jsonPayload , ContentType.APPLICATION_JSON); - httpPost.setEntity(stringEntity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); httpPost.setHeader("Authorization", "Basic " + encodedCredentials); int retryCount = 0; boolean retry; + do { try { retry = false; - HttpResponse result = httpClient.execute(httpPost); - int statusCode = result.getStatusLine().getStatusCode(); - if (statusCode < 200 || statusCode >= 300) { - throw new IOException("Status Code: " + statusCode); + + HttpEntity stringEntity = new StringEntity(jsonPayload, ContentType.APPLICATION_JSON); + httpPost.setEntity(stringEntity); + + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode < 200 || statusCode >= 300) { + throw new IOException("Unexpected response status: " + statusCode); + } } + } catch (IOException ex) { retryCount++; if (retryCount < maxRetryCount) { retry = true; - LOG.warn("Failed to persist transaction count records to remote endpoint. Retrying after 1s"); + LOG.warn("Failed to persist transaction count records to remote endpoint. Retrying after 1s", ex); try { Thread.sleep(1000); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); LOG.error("Could not persist following transaction count records: " + jsonPayload, e); } } else { @@ -106,6 +110,13 @@ public boolean commit(ArrayList transactionRecordList, int ma @Override public void clenUp() { - // To be implemented + try { + if (httpClient != null) { + httpClient.close(); + } + } catch (IOException e) { + LOG.error("Error while closing the HttpClient", e); + } } } + From 1cc76d42245e33d949658592882e64175fc01eb3 Mon Sep 17 00:00:00 2001 From: Ashan Rathnaweera Date: Tue, 19 Nov 2024 12:15:45 +0530 Subject: [PATCH 2/2] Bump minor version --- counter/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/counter/pom.xml b/counter/pom.xml index b002412..80a7e0e 100644 --- a/counter/pom.xml +++ b/counter/pom.xml @@ -19,7 +19,7 @@ 4.0.0 org.wso2.integration.transaction.counter - 1.1.0 + 1.2.0 transaction-count-handler bundle WSO2 Integration Transaction Counting Handler