WebClient a solution to the problem of socket closed in multi-threaded and agent-use situations [Htmlunit]

Source: Internet
Author: User

With WebClient's built-in browser, you can perform page fetching, sometimes you may need to set up an agent,
WebClient WebClient = new WebClient (browserversion.x);
Webclient.setproxyconfig (Proxyconfig pc);
In a single-threaded scenario, there is no problem with the WebClient created: the client-to-proxy connection can be set up and shut down in sequence.

Consider a situation in which multiple threads are accessing WebClient concurrently and may report the following exception:

[Thread-7] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-closing Connection [httproute[{}->http:// 192.168.5.29:3128->http://58.223.139.151:8080]][null]
........
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-total connections kept alive : 0
2012-04-19 10:31:32,926 [Thread-8] DEBUG Org.apache.http.impl.conn.tsccm.connpoolbyroute-total issued connections:0
2012-04-19 10:31:32,926 [Thread-8] DEBUG Org.apache.http.impl.conn.tsccm.connpoolbyroute-total Allocated connection: 0 out of 20
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-no free connections [ Httproute[{}->http://192.168.5.29:3128->http://58.223.139.151:8080]][null]
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-available capacity:2 out of 2 [httproute[{}->http://192.168.5.29:3128->http://58.223.139.151:8080]] [NULL]
2012-04-19 10:31:32,926 [Thread-8] DEBUG org.apache.http.impl.conn.tsccm.connpoolbyroute-creating New Connection [ HTTPROUTE[{}->HTTP://192.168.5.29:3128->HTTP://58.223.139.151:8080]]
2012-04-19 10:31:32,926 [Thread-6] DEBUG Org.apache.http.impl.client.defaulthttpclient-socket closed
Java.net.SocketException:socket closed
At java.net.SocketInputStream.socketRead0 (Native Method)
At Java.net.SocketInputStream.read (Unknown Source)
At Org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer (abstractsessioninputbuffer.java:130)
At Org.apache.http.impl.io.SocketInputBuffer.fillBuffer (socketinputbuffer.java:127)
At Org.apache.http.impl.io.AbstractSessionInputBuffer.readLine (abstractsessioninputbuffer.java:233)
At Org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine (loggingsessioninputbuffer.java:100)
At Org.apache.http.impl.conn.DefaultResponseParser.parseHead (defaultresponseparser.java:98)
At Org.apache.http.impl.io.AbstractMessageParser.parse (abstractmessageparser.java:210)
At Org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader (Abstracthttpclientconnection.java : 271)
At Org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader (defaultclientconnection.java:227)
At Org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader (abstractclientconnadapter.java:209 )
At Org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse (httprequestexecutor.java:292)
At Org.apache.http.protocol.HttpRequestExecutor.execute (httprequestexecutor.java:126)
At Org.apache.http.impl.client.DefaultRequestDirector.execute (defaultrequestdirector.java:483)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:641)
At Org.apache.http.impl.client.AbstractHttpClient.execute (abstracthttpclient.java:597)
At Com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse (httpwebconnection.java:134)
At Com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection (webclient.java:1406)
At Com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection (webclient.java:1460)
At Com.gargoylesoftware.htmlunit.WebClient.loadWebResponse (webclient.java:1325)

At Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:304)

At Com.gargoylesoftware.htmlunit.WebClient.getPage (webclient.java:370)

Exception information display: Thread-6 When using WebClient, the socket closed exception is detected, view the above exception, there is a socket (http://192.168.5.29:3128->http:// 58.223.139.151:8080) was thread-7 closed, Thread-8 created a new socket, maybe a point in time, the socket was closed, causing thread-6 to report the socket closed exception.
You can avoid these problems by using threadlocal to create separate WebClient objects for different threads:

[Java]View Plaincopy
    1. Each thread maintains a separate WebClient object that prevents the thread from sharing a browser that interferes with each other
    2. private Threadlocal<webclient> client = new threadlocal<webclient> () {
    3. protected synchronized WebClient InitialValue () {
    4. WebClient WebClient = new WebClient (version);
    5. //Set parameters for WebClient
    6. Webclient.set ...;
    7. return webClient;
    8. }
    9. };
    10. public void Setwebclient (WebClient WC) {
    11. Client.set (WC);
    12. }
    13. Public WebClient getwebclient () {
    14. return Client.get ();
    15. }

WebClient a solution to the problem of socket closed in multi-threaded and agent-use situations [Htmlunit]

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.