简体   繁体   中英

How to fix/avoid java.io.IOException: Socket read failed?

We are having an application which makes frequent ajax calls to the servlet. We are getting the below exception inconsistently.

20:39:15.016 com.abc.xyzhome.util.XYZLog:XYZDashboardServlet::buildXYZDashboardRequest::Exception:
java.io.IOException: Socket read failed
        at org.apache.coyote.ajp.AjpProcessor.read(AjpProcessor.java:313)
        at org.apache.coyote.ajp.AjpProcessor.readMessage(AjpProcessor.java:364)
        at org.apache.coyote.ajp.AjpProcessor.receive(AjpProcessor.java:331)
        at org.apache.coyote.ajp.AbstractAjpProcessor.refillReadBuffer(AbstractAjpProcessor.java:664)
        at org.apache.coyote.ajp.AbstractAjpProcessor$SocketInputBuffer.doRead(AbstractAjpProcessor.java:1142)
        at org.apache.coyote.Request.doRead(Request.java:422)
        at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:290)
        at org.apache.catalina.connector.InputBuffer.realReadChars(InputBuffer.java:353)
        at org.apache.tomcat.util.buf.CharChunk.substract(CharChunk.java:439)
        at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:416)
        at org.apache.catalina.connector.CoyoteReader.read(CoyoteReader.java:108)
        at org.apache.catalina.connector.CoyoteReader.readLine(CoyoteReader.java:163)
        at com.abc.xyzhome.XYZDashboardServlet.buildXYZDashboardRequest(XYZDashboardServlet.java:456)

This is exception is occurring sometimes when we are trying to read data from the inputStream. PFB, the place where this exception is occurring.

        try {
             BufferedReader reader = request.getReader();
             while ((line = reader.readLine()) != null)
                jb.append(line);
        }  catch (IOException e) {
            e.printStackTrace();
        }

We are getting the BufferedReader object from the HttpServletRequest . The Exception is occurring in reader.readLine() .

Our application is hosted in Tomcat 7.0.54. PFB, the angularJS ajax call we are making from the client for this particular request.

$http({
      method: 'POST',
      url: 'XYZDashboard',
      headers: {'Content-Type': 'application/json'},
      data: {'xyzStatus':fav_value,'xyzRequestType':'getDataForXYZDashboard'},
      cache: false,
    }).success(function (data) 
    {
        //Some code here
    });

The Content type is JSON. Please let me know on how to avoid this exception. Thanks.

EDIT: I have tried BufferedReader ready() to check whether the stream is ready to be read but it is always returning false. I read in a blog that in Tomcat, ready() always returns false. Please advice.

Wrap your code in a try / catch that throws IOException

try {
    BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null)
} catch (IOException e1) {
    e1.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM