Java.net. socketexception: Connection Reset is finally solved.

Source: Internet
Author: User
Tags connection reset

Java.net. socketexception: Connection Reset has finally been resolved-seo blog | seotcs System

Java.net. socketexception: Connection Reset is finally solved.

50

ArticlePublished by Chen Fu,: 4,304 followers

The SEO score has been updated since seotcs November.AlgorithmOne of the problems that have plagued me for a long time has emerged. Java's data job often reports the following errors during execution:

"2011-12-03 18:00:32 defaulthttpclient [info] I/O exception (java.net. socketexception) caught when processing request: Connection reset by peer: Socket write error
2011-12-03 18:00:32 defaulthttpclient [info] retrying request "...

For this reason, I searched some Chinese and English websites, searched every corner I could find, and found the principle of this situation. This Java exception may occur on both the client and server, there are two reasons for this exception:

1. If the socket at one end is closed (or closed actively, or closed due to abnormal exit), the other end still sends data, the first packet sent causes this exception (connect reset by peer ).

2. One End exits, but the connection is not closed when it exits. If the other end reads data from the connection, this exception (Connection Reset) is thrown ). In short, it is caused by read and write operations after the connection is disconnected.

So I simply think that by setting the timeout time of some sockets, we can solve the problem:

However, this is still the case after the settings.

This problem has been plagued for several days and is being considered and compared every day to find out the cause.CodeI can't help thinking, why is there no error in batch query ranking data with the same number of keywords and frequent errors recently? Is our server IP Address blocked by the requested interface website? This is not a good reason. It must beProgramThe connection is not properly released somewhere in!

Under the guidance of this idea, after several days of continuous fighting and practice, we finally found the essence of the problem, that is, the timer method! This is the case. In the past few days, I have manually triggered some Batch Tasks and found that Java's java.net. socketexception: The Connection reset error will always be thrown, and the screen flushing is very powerful. I carefully compared the timer code.

Then, I suddenly realized, right! There is a problem here. Let me analyze it myself:

The value returned by a function is a critical value. However, in my timer method, if the returned value is a critical value, it will force it to continue to execute that method within 10 seconds, and this method is to get a pageSource codeEach execution of this method consumes dozens of milliseconds, that is, a socket connection is established during this time, however, since it always returns the critical value, this method will establish a socket connection continuously within 10 seconds to obtain data. If this method is executed for about 80 ms each time (after testing, the execution time of each such method is about 80 milliseconds). In 10 seconds, 10*1000/80 = 125 socket connections are established, that is, 12.5 socket connections are established every second, in addition, because this is a filtering program, multiple critical values will appear together consecutively. Therefore, within a few seconds, the number of socket connections to the same website page will soar to several hundred or even thousands, resulting in a high number of requests waiting for processing:

At the beginning, why does the timer method be used to execute a method multiple times? The reason is to obtain a stable value of data. But now, the negative impact brought by this method is very high, however, after a few days of comprehensive analysis and testing, we finally found the culprit. After the problem was solved, we suddenly lost our mind, you can go to bed with peace of mind...

Related Article

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.