1

我需要检查一个特定的 url"http://16.100.106.4/xmldata?item=all"是否有效?现在的事情是,如果 url 不起作用,我使用下面的代码,然后连接超时等待大约 20 秒,然后给出异常。现在,我必须检查大约 20000 个 IP 的 URL,我不能等待那么长时间。线程可以是一种选择,但我也不确定我应该去多少线程。我希望在几秒钟内完成整个操作。

public static boolean exists(String URLName){
     boolean available = false;

          try{
                final  URLConnection connection = (URLConnection) new URL(URLName).openConnection();
                connection.connect();

                System.out.println("Service " + URLName + " available, yeah!");
                available = true;
            } catch(final MalformedURLException e){
                throw new IllegalStateException("Bad URL: " + available, e);
            } catch(final Exception e){
                // System.out.print("Service " + available + " unavailable, oh no!", e);
                available = false;
            }
            return available;
  } 
4

2 回答 2

0

您可以将URLConnection#setConnectTimeout设置为您希望发生连接的最小值。

更新尝试如下:

 final HttpURLConnection connection = (HttpURLConnection) new URL(URLName)
                .openConnection();
        connection.setReadTimeout(2000);
        connection.setConnectTimeout(2000);
        connection.setRequestMethod("HEAD");
        int responseCode = connection.getResponseCode();
        connection.getInputStream().read();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            return false;
        }
        return true;
于 2012-09-26T06:56:07.493 回答
0

我会像这样作为生产者/消费者问题来解决它:

public class URLValidator {

  private final CompletionService<URLValidationResult> service;

  public URLValidator(ExecutorService exec) {
    this.service = new CompletionService<URLValidationResult>(exec);
  }

  // submits a url for validation
  public void submit(String url) {
     service.submit(new Callable<URLValidationResult>() {
         public URLValidationResult call() {
           return validate(url);
         }          
     });
  }

  // retrieves next available result. this method blocks
  // if no results are available and is responsive to interruption.
  public Future<URLValidationResult> next() {
     try {
       return service.take();
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
     }
  }

  private URLValidationResult validate(String url) {
      // Apply your url validation logic here (i.e open a timed connection
      // to the url using setConnectTimeout(millis)) and return a 
      // URLValidationResult instance encapsulating the url and
      // validation status
  }

}

需要提交 url 进行验证的线程将使用该submit(String url)方法。此方法将依次向完成服务提交异步执行的任务。处理验证结果的线程将使用该next()方法。此方法返回一个代表提交任务的 Future。您可以这样处理返回的未来:

URLValidator validator = // the validator instance....

// retrieve the next available result
Future<URLValidationResult> future = validator.next();

URLValidationResult result = null;
try {
  result = future.get();
} catch (ExecutionException e) {
  // you submitted validation task has thrown an error,
  // handle it here.
}

// do something useful with the result
process(result);
于 2012-09-26T07:34:15.867 回答