Added uptime parallelizing.
This commit is contained in:
parent
5b1ce71dd8
commit
893b54e013
1 changed files with 42 additions and 1 deletions
|
@ -21,10 +21,15 @@ package fr.devinsy.statoolinfos.uptime;
|
|||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.statoolinfos.util.Chrono;
|
||||
|
||||
/**
|
||||
* The Class UptimeChecker.
|
||||
*/
|
||||
|
@ -134,14 +139,50 @@ public class UptimeSurveyor
|
|||
result.add(new Uptime(url));
|
||||
}
|
||||
|
||||
Chrono chrono = new Chrono().start();
|
||||
|
||||
//
|
||||
System.out.println("Survey of " + result.size());
|
||||
/* MonoThread version.
|
||||
for (Uptime uptime : result)
|
||||
{
|
||||
int code = survey(uptime.getURL());
|
||||
uptime.update(code);
|
||||
System.out.println("Uptime: " + uptime.getStatus().toString() + " " + uptime.getURL().toString());
|
||||
System.out.println("Uptime: " + uptime.getStatus().toString() + " " +
|
||||
uptime.getURL().toString());
|
||||
}
|
||||
*/
|
||||
|
||||
ExecutorService pool = Executors.newFixedThreadPool(20);
|
||||
for (Uptime uptime : result)
|
||||
{
|
||||
pool.execute(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
int code = survey(uptime.getURL());
|
||||
uptime.update(code);
|
||||
System.out.println(Thread.currentThread().getId() + " Uptime: " + uptime.getStatus().toString() + " " + uptime.getURL().toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pool.shutdown();
|
||||
while (!pool.isTerminated())
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("Wait for URL survey.");
|
||||
pool.awaitTermination(5, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("URL Survey time: " + chrono.toString());
|
||||
|
||||
//
|
||||
return result;
|
||||
|
|
Loading…
Reference in a new issue