java - How to use HttpComponentsClientHttpRequestFactory with RestTemplate efficiently? -
java - How to use HttpComponentsClientHttpRequestFactory with RestTemplate efficiently? -
i using resttemplate along mill httpcomponentsclienthttprequestfactory in 1 of projects. in project, need create http url phone call server running restful service returns response json string.
below code -
public class getuserclientdata { public string getdata(keyholder keys) { homecoming new httprequestaccess(keys).makehttprequest(); } } below class wraps httpclient part -
public class httprequestaccess { // should static? private resttemplate resttemplate; private keyholder keys; private int timeout; public httprequestaccess(keyholder keys){ this(keys.gettimeoutvalue()); // setting timeout resttemplate this.keys = keys; } public httprequestaccess(int timeout) { this.timeout = timeout; resttemplate = new resttemplate(clienthttprequestfactory()); } private clienthttprequestfactory clienthttprequestfactory() { // not expensive every time creating new object? httpcomponentsclienthttprequestfactory mill = new httpcomponentsclienthttprequestfactory(); factory.setreadtimeout(timeout); factory.setconnecttimeout(timeout); homecoming factory; } public string makehttprequest() { string response = null; seek { // logic here string url = generateurl(); response = resttemplate.getforobject(url, string.class); // logic here } grab (restclientexception ex) { // log exception , stuff } grab (exception ex) { // log exception } homecoming response; } } should resttemplate , httpcomponentsclienthttprequestfactory static here in httprequestaccess class if see correctly, recreating whole connection pool each request in resttemplate not right way guess because each mill has connection , thread pool , pretty heavy object guess.
in general best way utilize resttemplate along mill httpcomponentsclienthttprequestfactory in multithreading environment? guess resttemplate thread safe don't think httpcomponentsclienthttprequestfactory thread safe. right me if wrong? running library under heavy load.
i using spring-web-3.2.8.release version.
in 1 of projects, have created static instance of httpcomponentsclienthttprequestfactory , pass every resttemplate. though, in here, suggested have global instance of resttemplate.
maybe irrelevant, 1 of import point pass httpclients.createdefault() httpcomponentsclienthttprequestfactory while constructing since default, mill uses scheme properties create httpclient mill , may cause lots of pain in real server environment. may pass custom httpclient.
java multithreading spring thread-safety resttemplate
Comments
Post a Comment