@@ -69,6 +69,8 @@ public class AdyenHttpClient implements ClientInterface {
6969
7070 private static final String CHARSET = "UTF-8" ;
7171 private Proxy proxy ;
72+ private volatile CloseableHttpClient sharedHttpClient ;
73+ private final Object lock = new Object ();
7274
7375 public Proxy getProxy () {
7476 return proxy ;
@@ -78,6 +80,29 @@ public void setProxy(Proxy proxy) {
7880 this .proxy = proxy ;
7981 }
8082
83+ @ Override
84+ public void close () throws IOException {
85+ synchronized (lock ) {
86+ if (sharedHttpClient != null ) {
87+ sharedHttpClient .close ();
88+ sharedHttpClient = null ;
89+ }
90+ }
91+ }
92+
93+ private CloseableHttpClient getOrCreateHttpClient (Config config ) {
94+ CloseableHttpClient client = sharedHttpClient ;
95+ if (client != null ) {
96+ return client ;
97+ }
98+ synchronized (lock ) {
99+ if (sharedHttpClient == null ) {
100+ sharedHttpClient = createCloseableHttpClient (config );
101+ }
102+ return sharedHttpClient ;
103+ }
104+ }
105+
81106 @ Override
82107 public String request (String endpoint , String requestBody , Config config )
83108 throws IOException , HTTPClientException {
@@ -125,20 +150,19 @@ public String request(
125150 ApiConstants .HttpMethod httpMethod ,
126151 Map <String , String > params )
127152 throws IOException , HTTPClientException {
128- try ( CloseableHttpClient httpclient = createCloseableHttpClient (config )) {
129- HttpUriRequestBase httpRequest =
130- createRequest (
131- endpoint , requestBody , config , isApiKeyRequired , requestOptions , httpMethod , params );
153+ CloseableHttpClient httpclient = getOrCreateHttpClient (config );
154+ HttpUriRequestBase httpRequest =
155+ createRequest (
156+ endpoint , requestBody , config , isApiKeyRequired , requestOptions , httpMethod , params );
132157
133- // Execute request with a custom response handler
134- AdyenResponse response = httpclient .execute (httpRequest , new AdyenResponseHandler ());
158+ // Execute request with a custom response handler
159+ AdyenResponse response = httpclient .execute (httpRequest , new AdyenResponseHandler ());
135160
136- if (response .getStatus () < 200 || response .getStatus () >= 300 ) {
137- throw new HTTPClientException (
138- response .getStatus (), "HTTP Exception" , response .getHeaders (), response .getBody ());
139- }
140- return response .getBody ();
161+ if (response .getStatus () < 200 || response .getStatus () >= 300 ) {
162+ throw new HTTPClientException (
163+ response .getStatus (), "HTTP Exception" , response .getHeaders (), response .getBody ());
141164 }
165+ return response .getBody ();
142166 }
143167
144168 HttpUriRequestBase createRequest (
0 commit comments