java - BufferedReader.readLine() hangs sometimes -


in application, there separate thread, ran scheduledexecutorservice.scheduleatfixedrate() every minute, parses rss feeds multiple websites. using apache httpclient receive xml.

sample code:

inputstream inputstream = httpclient.get(url);     string xml = inputstreamtostring(inputstream, encoding, websitename);  public static string inputstreamtostring(inputstream inputstream, string encoding, string websitename)  {      bufferedreader bufferedreader = null;     printwriter printwriter = null;     stringbuilder stringbuilder = new stringbuilder();      int letter;     try      {         bufferedreader = new bufferedreader(new inputstreamreader(inputstream, encoding));         printwriter = new printwriter(new file("src/doclog/"                  + websitename + "_"                  + new simpledateformat("mm_dd_yyyy_hh_mm_ss").format(new date(system.currenttimemillis()))                  + "_" + encoding + ".txt"), encoding);         while((letter = bufferedreader.read()) != -1)          {             char character = (char) letter;             printwriter.print(character);                            stringbuilder.append(character);         }     }      catch(ioexception e)      {         throw new runtimeexception(e);     }          {         try          {             if(bufferedreader != null)              {                 bufferedreader.close();             }             if(printwriter != null)              {                 printwriter.close();             }         }          catch(ioexception e)          {             e.printstacktrace();         }     }     system.out.println("string built");     return stringbuilder.tostring(); } 

and httpclient class:

public class httpclient  {    private static final httpclient client = httpclientbuilder.create().build();      public static inputstream get(string url)    {            try        {            httpget request = new httpget(url);              httpresponse response = client.execute(request);            system.out.println("response code: " + response.getstatusline().tostring());             return response.getentity().getcontent();        }        catch(ioexception | illegalargumentexception e)        {            throw new runtimeexception(e);        }    } } 

as title says, there chance bufferedreader.readline() hang forever. i've seen answers on topic, , suggest check if bufferedreader.ready() returns true. problem there websites, return false in bufferedreader.ready(), while processing them, parse fine.

how can prevent thread hanging on bufferedreader.readline()?

if matters, response.getstatusline().tostring() returns http/1.1 200 ok

edit

i found out bufferedreader.ready() true when hang happens.

edit 2

bufferedreader.read() hangs well. strange hang happens when dealing 1 single website, , it's occurrence absolutely random. application either working 15 hours, receiving hundreds of non-problematic responses, or hang in 10 minutes after launch. i've started write characters of every single update separate file, , found out nothing special happens. xml reading stops forever in middle of document, last characters <p dir="ltr"&g. updated code.

also, it's noteworthy mention there can't unhandled exceptions, because @ highest level of scheduledexecutorservice.scheduleatfixedrate() runnable catch throwable, , print it's stacktrace.

the ready() method returns true telling there characters available reading. problem readline() blocks until finds end-of-line in input.

public string readline() throws ioexception

reads line of text. line considered terminated 1 of line feed ('\n'), carriage return ('\r'), or carriage return followed linefeed.

as reading stream there no guarantee data come in @ line boundaries readline() call blocks.

you can use read method not block, have check eol yourself.

public int read(char[] cbuf, int off, int len) throws ioexception

reads characters portion of array.

this method implements general contract of corresponding read method of reader class. additional convenience, attempts read many characters possible repeatedly invoking read method of underlying stream. iterated read continues until 1 of following conditions becomes true:

the specified number of characters have been read, read method of underlying stream returns -1, indicating end-of-file, or ready method of underlying stream returns false, indicating further input requests block.  

if first read on underlying stream returns -1 indicate end-of-file method returns -1. otherwise method returns number of characters read.

also have reconstruct line characters read. not ss convenient reading entire line @ once way must done.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -