HTTP connection Pooling

Source: Internet
Author: User

     <context:property-placeholder location= "classpath:conf/framework/httpclient.properties"/><!--define Connection Manager-- ><bean id= "Httpclientconnectionmanager" class= " Org.apache.http.impl.conn.PoolingHttpClientConnectionManager "destroy-method=" close "><!--maximum number of connections-->< Property Name= "Maxtotal" value= "${http.maxtotal}"/><!--set the number of concurrency per host address--><property name= " Defaultmaxperroute "value=" ${http.defaultmaxperroute} "/></bean><!--httpclient Object Builder--><bean id= "Httpclientbuilder" class= "Org.apache.http.impl.client.HttpClientBuilder" ><!--setting up Connection Manager--><property Name= "ConnectionManager" ref= "httpclientconnectionmanager"/></bean><!--definition HttpClient Object--><bean Id= "HttpClient" class= "org.apache.http.impl.client.CloseableHttpClient" factory-bean= "Httpclientbuilder" Factory-method= "Build" scope= "prototype" ></bean><!--definition Cleanup Invalid connection--><bean class= " Com.avcon.platform.dledc.res.service.IdleConnectionEvictor "destroy-method=" sHutdown "><constructor-arg index=" 0 "ref=" Httpclientconnectionmanager "/></bean><bean id=" Requestconfigbuilder "class=" Org.apache.http.client.config.RequestConfig.Builder "><!--the maximum time to create a connection-->< Property Name= "ConnectTimeout" value= "${http.connecttimeout}"/><!--get the maximum connection time from the connection pool--><property name= " Connectionrequesttimeout "value=" ${http.connectionrequesttimeout} "/><!--data transfer for maximum time--><property name=" Sockettimeout "value=" ${http.sockettimeout} "/><!--to test if the connection is available before submitting the request--><property Name=" Staleconnectioncheckenabled "value=" ${http.staleconnectioncheckenabled} "/></bean><!--define request Parameters-- <bean id= "Requestconfig" class= "Org.apache.http.client.config.RequestConfig" factory-bean= " Requestconfigbuilder "factory-method=" "Build" ></bean>

@Servicepublic class Httpclientservice {@Autowiredprivate closeablehttpclient httpClient; @Autowiredprivate Requestconfig requestconfig;/** * Execute GET request * * @param URL * @return * @throws IOException * @throws clientprotocolexceptio n */public string doget (string url) throws Clientprotocolexception, IOException {//create HTTP GET request HttpGet HttpGet = new http Get (URL); httpget.setconfig (This.requestconfig); Closeablehttpresponse response = null;try {//execute request response = Httpclient.execute (httpget);//Determine if the return status is 200if ( Response.getstatusline (). Getstatuscode () = = () {return entityutils.tostring (response.getentity (), "UTF-8");}} Finally {if (response! = null) {Response.close ();}} return null;} /** * GET request with parameters * * @param URL * @param params * @return * @throws urisyntaxexception * @throws IOException * @throws Cl Ientprotocolexception */public string doget (string url, map<string, string> params) throws Clientprotocolexception, IOException, urisyntaxexception {uribuilder uribuilder = new UriBuilder (URL); for (String Key:params.keySet ()) {Uribuilder.addparameter (key, Params.get (key));} Return This.doget (Uribuilder.build (). toString ());} /** * Perform POST request * * @param URL * @param params * @return * @throws ioexception */public httpresult doPost (String URL, map& Lt String, string> params) throws IOException {//create HTTP POST request HttpPost HttpPost = new HttpPost (URL); Httppost.setconfig (t His.requestconfig); if (params! = null) {//Set 2 post parameters, one is scope, one is qlist<namevaluepair> parameters = new ArrayList <NameValuePair> (); for (String Key:params.keySet ()) {Parameters.Add (New Basicnamevaluepair (Key, Params.get (key )));} Construct a form-form entity urlencodedformentity formentity = new Urlencodedformentity (Parameters, "UTF-8");// Set the request entity to the HttpPost object Httppost.setentity (formentity);} Closeablehttpresponse response = null;try {//execute request response = Httpclient.execute (HttpPost); return new Httpresult ( Response.getstatusline (). Getstatuscode (), Entityutils.tostring (Response.getentity (), "UTF-8")); finally {if (responSE = null) {Response.close ();}}} /** * Execute POST request * * @param URL * @return * @throws ioexception */public httpresult doPost (String url) throws IOException { Return this.dopost (URL, null);} /** * Submit JSON data * * @param URL * @param JSON * @return * @throws clientprotocolexception * @throws ioexception */public Ht Tpresult dopostjson (string URL, string json) throws Clientprotocolexception, IOException {//create HTTP POST request HttpPost Httppo st = new HttpPost (URL); httppost.setconfig (this.requestconfig); if (json! = NULL) {//Constructs a form-form entity stringentity stringentity = new Stringentity (JSON, Contenttype.application_json);//Set the request entity to the HttpPost object Httppost.setentity ( stringentity);} Closeablehttpresponse response = null;try {//execute request response = This.httpClient.execute (HttpPost); return new Httpresult ( Response.getstatusline (). Getstatuscode (), Entityutils.tostring (Response.getentity (), "UTF-8")); Finally {if (response! = null) {Response.close ();}}}

  

HTTP connection Pooling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.