?
Solved

how to set connection timeout

Posted on 2005-04-20
9
Medium Priority
?
1,066 Views
Last Modified: 2012-08-13
How to modify my code so that I can implement connection timeout for requests. The execution is getting stalled for some urls.

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.text.html.*;
import javax.swing.text.*;


class Out
{
      
      public  String[] getLinks(String uriStr)
            {
                  // uriStr is an url
                List result = new ArrayList();
              try
                  {
            URL locator = new URL(uriStr);
                  HttpURLConnection connection = (HttpURLConnection)locator.openConnection();
                  connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461)");
                  connection.setFollowRedirects(true);
                  connection.connect();    
                        if(((connection.getContentType()).indexOf("text/html"))==-1)
                        return null;
                  Reader rd = new InputStreamReader(connection.getInputStream());
            EditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
                  doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
            kit.read(rd, doc, 0);
                  // Find all the A elements in the HTML document
            HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
            while (it.isValid())
                        {
                              SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();
                              String link = (String)s.getAttribute(HTML.Attribute.HREF);
                          if (link != null && link.toLowerCase().indexOf("javascript:")==-1)
                              {
                                    URL temp = new URL(locator,link);
                                    result.add(temp.toString());
                              }      // END OF IF
                              it.next();
                        }      // END OF WHILE
                  } catch (FileNotFoundException e) {
                        System.out.println("In Out.java" + uriStr);
                                    e.printStackTrace();
                  } catch (MalformedURLException e) {
                        System.out.println("In Out.java");
                                    e.printStackTrace();
                  } catch (BadLocationException e) {      
                                          System.out.println("In Out.java");
                                    e.printStackTrace();
                  } catch (IOException e) {                  
                                          System.out.println("In Out.java");
                                    e.printStackTrace();
                  }catch (NullPointerException e){
                        System.out.println("In Out.java");
                                    e.printStackTrace();
                  }catch (Exception e){
                        System.out.println("In Out.java");
                                    e.printStackTrace();
                  }
                  return (String[])result.toArray(new String[result.size()]);
                        // RETURN THE SET OF LINKS IN A STRING ARRAY
            }

      public static void main(String[] args)
      {
            try
            {
            if(args.length != 1)
            {
                  System.out.println(" Usage : java Out Url");
                  System.exit(0);
                  }
            Out o = new Out();
            String links[] = o.getLinks(args[0]);      
            if(links == null)
            {
                  System.out.println("The Content Type is Not Html");
                  return;
            }
            System.out.println(links.length);
            for(int i = 0 ; i < links.length ; i++)
                  System.out.println(links[i]);
            }catch(Exception e)
            {
                  System.out.println(" In out Main" + e);
                  e.printStackTrace();
            }
      }

}
0
Comment
Question by:sumantedla
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13822474
0
 
LVL 92

Expert Comment

by:objects
ID: 13822481
try setting the sun.net.client.defaultConnectTimeout property

http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 13822490
add the following at the start of your app:

System.setProperty("sun.net.client.defaultConnectTimeout", "10000");   // 10 seconds
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:andreba
ID: 13822495
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 13822497
System.setProperty("sun.net.client.defaultConnectTimeout", "1000");
System.setProperty("sun.net.client.defaultReadTimeout", "1000");

(One second) This can also be done on the command line:


java -Dsun.net.client.defaultConnectTimeout=1000 YourApp
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13824761
If you are using java 1.5 then you can just call connection.setConnectTimeout(1000);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13831702
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 13831744
(:
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13836193
So, I take it as if you don't have 1.5.
If you do have 1.5 then I advice you to use the settings in the level of the connection as the other settings has a global effect on your application (unless that is what you want).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question