1

"http://16.100.106.4/xmldata?item=all"特定の URLが機能しているかどうかを確認する必要がありますか? 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 に答える