How to timeout when reading from a URL ?

I have the following method that gets html from a URL :
StringBuffer Read_From_Url(String URL_Or_File_Path,boolean Print_Stack_Trace)
    StringBuffer sb=new StringBuffer();
    BufferedReader in=null;
    try
        in=new BufferedReader(new InputStreamReader(new URL(URL_Or_File_Path).openStream()));
        String inputLine;
        while ((inputLine=in.readLine()) != null) sb.append(inputLine+"\n");
        in.close();
    catch (Exception e) { if (Print_Stack_Trace) e.printStackTrace(); }
    finally
      try
        if (in!=null)
          in.close();
          in=null;
      catch (Exception ex) { if (Print_Stack_Trace) ex.printStackTrace(); }
    return sb;
  }It usually works fine, but for for some urls, it will be stuck forever, for instance try this one : www.conform.com.cn
I tried to read that url from a browser, it will keep the browser in the fetch mode forever, never return a page.
My question is : how to tell my method to timeout after 30 seconds if it can't get anything from that url ?
Frank

If you RTFM, you will see
http://java.sun.com/javase/6/docs/api/java/net/URLConnection.html#setReadTimeout(int)
there's also one for ConnectTimeout
Also, I have created a class called Timeout that calls a TimeOutCmd after the alloted time has expired. Generally, my timeout command is to close the stream I'm waiting on, which will release the thread and cause an IOException
package tjacobs.io;
public class TimeOut implements Runnable {
     private long mWaitTime;
     private boolean mRunning = true;
     private Thread mMyThread;
     private TimeOutCmd mTimeOutCmd;
     public static final int DEFAULT_WAIT_TIME = 30 * 1000; // 30 Seconds
     public static final int NO_TIMEOUT = -1;
     public static interface TimeOutCmd {
          public void timeOut();
     public TimeOut(TimeOutCmd cmd) {
          this(cmd, DEFAULT_WAIT_TIME);
     public TimeOut(TimeOutCmd cmd, int timeToWait) {
          mWaitTime = timeToWait;
          mTimeOutCmd = cmd;
     public void stop() {
          mRunning = false;
          mTimeOutCmd.timeOut();
          if (mMyThread != null) mMyThread.interrupt();
      * reset the TimeOut
     public void tick() {
          if (mMyThread != null)
               mMyThread.interrupt();
     public void run () {
          mMyThread = Thread.currentThread();
          while (true) {
               try {
                    Thread.sleep(mWaitTime);
                    stop();
               catch (InterruptedException ex) {
                    if (!mRunning) {
                         return;
}

Similar Messages

  • Socket Timeout when reading from post stream

    I get the following error when a servlet tries to read an object from the post stream.
    Is there a way to set any timeout values on, say a socket, or static variable on
    any weblogic SocketImpl..?
    Appreciate any help.
    Thanks-
    Rama
    java.io.InterruptedIOException: Read timed out
    at java.net.SocketInputStream.socketRead(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:86)
    at weblogic.servlet.internal.PostInputStream.read(PostInputStream.java:124)
    at weblogic.servlet.internal.ServletInputStreamImpl.read(ServletInputStreamImpl.java:97)
    at java.io.ObjectInputStream.read(ObjectInputStream.java:1835)

    Was successful in setting the duration parameter in the HTTP tab of the server.
    However, when accessing the app (servlet) via iPlanet proxy, it still fails. I have
    to check on similar settings on that side.
    Do you have hints for me ?
    Thanks for your posting, Dave.
    Rama
    "Dave Martin" <[email protected]> wrote:
    >
    Rama:
    Try changing the Post Timeout Secs option under the HTTP tab for your server
    in the
    WebLogic Admin console. You could bump out the Duration on the HTTP connection
    while
    you're at it. Not sure if these will have a side effect on what you want
    to change,
    but worth a shot.
    "Rama Raghavan" <[email protected]> wrote:
    I get the following error when a servlet tries to read an object from the
    post stream.
    Is there a way to set any timeout values on, say a socket, or static variable
    on
    any weblogic SocketImpl..?
    Appreciate any help.
    Thanks-
    Rama
    java.io.InterruptedIOException: Read timed out
    at java.net.SocketInputStream.socketRead(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:86)
    at weblogic.servlet.internal.PostInputStream.read(PostInputStream.java:124)
    at weblogic.servlet.internal.ServletInputStreamImpl.read(ServletInputStreamImpl.java:97)
    at java.io.ObjectInputStream.read(ObjectInputStream.java:1835)

  • Lightroom encountered an error when reading from its preview cache and needs to quit.  Says Lightromm will attempt to fix the problem the next time it launches, but on every re-launch I get the same message.  Any clue as to how to fix this problem?

    I recently updated my HP laptop's OS from WIndows8 to WIndows8.1.  When I went attempted to open Lightroom5 I got the following message:
    "Lightroom encountered an error when reading from its preview cache and needs to quit. "
    "Lightroom will attempt to fix the problem the next time it launches"
    Every time I relaunch I  get the same message.  I'd like to understand what might be causing the problem so I can remedy and get back to my photo editing. Any clue as to how to fix this problem would be greatly appreciated.
    JPM

    reset your preferences, Resetting (or 'Trashing') the Lightroom Preferences file - Lightroom Forums

  • "Lightroom encountered an error when reading from its preview cache and needs to quit. How do I fix this

    Recently updated to Windows8.1 got the message:  "Lightroom encountered an error when reading from its preview cache and needs to quit.  Lightroom will attempt to fix the error the next time it launches"  Get the same message on multiple launches.  Anyone know how to fix this?

    Re: "Lightroom encountered and error when reading from its preview cache and needs to quit"

  • Tried to open lr 5 in cc. Receive error message "LR encountered an error when reading from its preview cache and needs to quit". How do I fix the problem?

    Tried to open LR 5 in cc. Receive error message "LR 5 encountered an error when reading from its preview cache and needs to quit". How do I fix the problem?

    Hi, Have a look under the "MORE LIKE THIS" on the right there should be an answer in one of those threads.

  • Lightroom crashes at start up with the error message "Lightroom encountered an error when reading from its preview cache and needs to quit".  Please advise on how to fix this error.  Thanks

    Lightroom crashes at start up with the error message "Lightroom encountered an error when reading from its preview cache and needs to quit".  Please advise on how to fix this error.  Thanks

    You probably need to delete your preview cache.  See here  
    Why And How To Clear Your Lightroom Cache - Lightroom Fanatic
    Preference and other file locations in Lightroom 5

  • How new objects are instantiated when reading from object stream?

    Hi,
    How the serializable API instantiates new objects when reading from stream?
    First it needs to create a new instance of a class and then fill its' fields with a serialized data. But not all serializable classes have empty constructor (as it have java beans) to make simple call of Class.newInstance(). Is there any way to create a virgin instance of any class?
    Ant�n

    I found the way, but it is not a solution:
    Constructor serializableConstructor = sun.reflect.ReflectionFactory.getReflectionFactory().newConstructorForSerialization( Class instanceClass, Constructor superClassEmptyConstructor );
    Object obj = serializableConstructor.newInstance();This "magic" method creates an empty constructor for a serializable class from empty constructor of any superclass. The limitation is that a first non-serializable superclass must have an empty constructor.
    As this magic method locates in a package sun.** I think this will not work with other JREs.

  • LR encountered an error when reading from its preview cache and needs to quit.

    I have the 30 day trial version of Lightroom.  I was able to use it for one week, but the past 1.5-2 weeks, it has been locked up and giving this error message.  How do I resolve the problem?  And how do I reset the 30 day free trial?  I would like to have time using the product, before deciding on the annual contract.
    "LR encountered an error when reading from its preview cache and needs to quit.  Lightroom will attempt to fix this problem the next time it launches."
    Thank you.

    Delete the preview cache from the Lightroom folder in your Pictures folder if you used the default placement. It has the extension .lrdata.
    You cannot reset the trial period.

  • It says Lightroom encountered an error when reading from a previous cache and needs to quit. Light room will attempt to to fix the problem the next time it launches.

    how do i fix this. It says Lightroom encountered an error when reading from a previous cache and needs to quit. Light room will attempt to to fix the problem the next time it launches.

    Have a look at this thread.
    Lightroom encountered an error when reading from its preview cache and needs to quit.  Lightroom will attempt to fix this problem the next time it launches"."

  • Upgraded to 5.4 when opening lightroom get a message "encountered an error when reading from its preview cache and need to quite" tried this several time but get the same eror message

    Upgraded to 5.4 when opening lightroom get a message "encountered an error when reading from its preview cache and need to quite" tried this several time but get the same error message.  Says Lightroom will attempt to fix the problem the next time it launches but that is not happening.  How do it fix this?

    Delete the preview cache using your operating system. It is a folder whose name ends with Previews.LRDATA. Do not delete the Lightroom catalog, which ends with .LRCAT

  • When backing up lightroom 4 catalog, get message "lightroom encountered an error when reading from its preview cache and needs to quit" this occurs during optimization stage of backup

    i upgraded from lightroom 4 to lightroom 5 and installed it on a separate hard drive. Initially had no problems util my computer crashed due to the failure of the drive on which Lightroom 5 was installed. Since having my computer rectified, i am unable to use lightroom 5 getting the message "lightroom encountered an error when reading from its preview cache and needs to quit"
    I can still use lightroom 4, however, i get the same message when i try to backup the catalog. This occurs during the optimisation phase of backup.
    I suspect it is related to Lightroom 5 creating a catalog from the Lightroom 4 files and cannot now locate those files or that they have been corrupted.
    Would appreciate any solutions / suggestions.

    You need to delete your preview cache and have Lightroom rebuild it.   Cache location can be found here.
    https://helpx.adobe.com/lightroom/kb/preference-file-locations-lightroom-41.html
    Why And How To Clear Your Lightroom Cache - Lightroom Fanatic

  • My computer crashed with lightroom open, since restart I get a msg LR encounted an error when reading from its preview cache and needs to quit

    Hi All
    My computer crashed with lightroom open, since restart I get a msg 'LR encounted an error when reading from its preview cache and needs to quit'
    I'd appreciate any suggestions
    Many thanks

    Many thanks for your help, at lease the LR is working now. How do I rebuild previews and smart previews or will this happen automatically?
    Many thanks

  • I run windows 7/8 and lightroom 5.7  i am getting an error message and here it is " lightroom encountered and error when reading from its preview cache and needs to quit". I am getting this message repeatly.  What do I need to do to fix it.

    I run windows 7/8 and lightroom 5.7  I am getting an error message and here it is  " lightroom encountered an error when reading from its preview cache and needs t quit"    How do I fix this.

    THANKS
    Hank Wilkinson
    Please visit my web site  <http://www.hankwilkinson.com/> www.hankwilkinson.com for the latest of my photos
    For information on any of these photos please email me or call 612-756-9970

  • Lightroom encountered an error when reading from its preview cache. helping a friend..

    Im not a lightroom user. even though I have some version 4. I use acdsee pro 6. but my friend has the 5.6 CC version but has a language issue so im trying to help me. basically will guide him over the phone.
    he was editing a wedding, the computer crashed. he rebooted, lightroom came with the preview cache error. he rebooted again..same issue. he did a repair but to no avail. he tried installing an older version in parralel but no success.
    what exactly does he need to do. im not a light room user. I use PS CS6 but not LR.
    anybody can help to help him. I did read some posts and it noted about thumbnail previews and removing or erasing/renaming
    1- where is this file located please?
    2-are there many of these ? and how do I know which is the one he needs to choose?
    thank you for your help.

    See answer at Re: "Lightroom encountered and error when reading from its preview cache and needs to quit"

  • Ightroom 5.7 "lightroom encountered an error when reading from its preview cache and needs to quit"

    I got this error message after start in Lightroom 5.7 "lightroom encountered an error when reading from its preview cache and needs to quit". It seems also catalog .lrcat disappears from disk. Also preview catalog .lrdata doesn't exists. So I try to reinstall Lightroom without success, I am still receiving the same error. How I can reset Lightroom to new catalog? I have backup of lrcat file, but I am not able to start Lightroom due to this error.

    It seems solved. I have copied backup .lrcat to Lightroom data directory and I still have received error mentioned above. But when I doubleclick on .lrcat file in Explorer the catalog have been opened without problems.

Maybe you are looking for