java - RestTemplate + ConnectionPoolTimeoutException: Timeout waiting for connection from pool -
i got error of sudden in production while application not under load.
the issue happened when code tries send put message using spring rest template
here code how initialing resttemplate
private static final resttemplate resttemplate = new resttemplate(new httpcomponentsclienthttprequestfactory()); { list<httpmessageconverter<?>> messageconverters = new arraylist<httpmessageconverter<?>>(); jaxb2marshaller marshaller = new jaxb2marshaller(); marshaller.setclassestobebound(paymentsession.class); marshallinghttpmessageconverter marshallinghttpmessageconverter = new marshallinghttpmessageconverter(marshaller, marshaller); marshallinghttpmessageconverter.setsupportedmediatypes(arrays.aslist(mediatype.application_xml, mediatype.text_html)); messageconverters.add(marshallinghttpmessageconverter); resttemplate.setmessageconverters(messageconverters); }
call put
try { httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.application_xml); httpentity<paymentsession> httpentity = new httpentity<paymentsession>(session, headers); resttemplate.exchange(baseurl+"/v1/psps", httpmethod.put, httpentity, paymentsession.class); }catch(httpclienterrorexception e){ logger.error("exception..!!",e) }
exception stacktrace
caused by: org.apache.http.conn.connectionpooltimeoutexception: timeout waiting connection pool @ org.apache.http.impl.conn.poolingclientconnectionmanager.leaseconnection(poolingclientconnectionmanager.java:232) @ org.apache.http.impl.conn.poolingclientconnectionmanager$1.getconnection(poolingclientconnectionmanager.java:199) @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:456) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:906) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:805) @ org.springframework.http.client.httpcomponentsclienthttprequest.executeinternal(httpcomponentsclienthttprequest.java:88) @ org.springframework.http.client.abstractbufferingclienthttprequest.executeinternal(abstractbufferingclienthttprequest.java:46) @ org.springframework.http.client.abstractclienthttprequest.execute(abstractclienthttprequest.java:49) @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:509)
i suggest configure httpcomponentsclienthttprequestfactory
instance being passed in constructor of resttemplate
increasing defaultmaxperroute
or maxperroute
specific http route requests timing out, increasing pool size not enough, mentioned in comment, if set poolinghttpclientconnectionmanager.setmaxtotal()
200, httpcomponentsclienthttprequestfactory
uses defaultmaxperroute
of 4, guess in attempt host route (scheme, host, port) not hijack connection pool)
... public poolinghttpclientconnectionmanager poolinghttpclientconnectionmanager() { poolinghttpclientconnectionmanager result = new poolinghttpclientconnectionmanager(); result.setmaxtotal(this.httphostconfiguration.getmaxtotal()); // default max per route used in case it's not set specific route result.setdefaultmaxperroute(this.httphostconfiguration.getdefaultmaxperroute()); // , / or if (collectionutils.isnotempty(this.httphostconfiguration.getmaxperroutes())) { (httphostconfiguration httphostconfig : this.httphostconfiguration.getmaxperroutes()) { httphost host = new httphost(httphostconfig.gethost(), httphostconfig.getport(), httphostconfig.getscheme()); // max per route specific host route result.setmaxperroute(new httproute(host), httphostconfig.getmaxperroute()); } } return result; } ... @configuration @configurationproperties(prefix = "httpconnpool") public class httphostsconfiguration { private integer maxtotal; private integer defaultmaxperroute; private list<httphostconfiguration> maxperroutes; // getters, setters ...
application.yml
httpconnpool: maxtotal: 20 defaultmaxperroute: 20 maxperroutes: - scheme: http host: localhost port: 8800 maxperroute: 20
i blog troubleshooting spring's resttemplate requests timeout requests timing out troubleshooted using jmeter
, shell commands , fixed via configuration settings.
Comments
Post a Comment