Last active
April 2, 2016 16:47
-
-
Save Viacheslav77/ac71715c8ac92e1c7054 to your computer and use it in GitHub Desktop.
В файле хранится список серверов. Надо проверить какие из серверов доступны в данный момент и создать отчет в формате сервер=статус.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ServersCalls; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.Map; | |
| public class TestPingPage extends Thread { | |
| private String path; | |
| private static Map <String,String> map; | |
| public TestPingPage(String path, Map <String,String> map){ | |
| this.path=path; | |
| this.map=map; | |
| } | |
| public void run(){ | |
| String ping = null; | |
| URL u = null; | |
| try { | |
| u = new URL("http://" + path); | |
| } catch (MalformedURLException e) { | |
| e.printStackTrace(); | |
| } | |
| HttpURLConnection uc = null; | |
| try { | |
| uc = (HttpURLConnection) u.openConnection(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| try { if(uc.getResponseCode() == 200 || uc.getResponseCode() == 301){ | |
| Process p = Runtime.getRuntime().exec( "ping " + path ); | |
| InputStreamReader isr = new InputStreamReader( p.getInputStream() ); | |
| BufferedReader br = new BufferedReader( isr ); | |
| StringBuilder line = new StringBuilder(); | |
| while ( br.readLine() != null ) | |
| line.append( br.readLine()); | |
| String [] tmp1 = line.toString().split(" "); | |
| ping = tmp1[tmp1.length-2] + " msec"; | |
| } | |
| else | |
| ping = "Service is unavailable"; | |
| } catch (Exception ex) { | |
| ping = "Service is unavailable"; | |
| } | |
| map.put(path,ping); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ---------------------------------------- | |
| Multi-threaded solution. Start Threads : | |
| Begin.....................End | |
| ---------------------------------------- | |
| 3232.rrr | |
| = Service is unavailable | |
| ---------------------------------------- | |
| i.ua | |
| = 2 msec | |
| ---------------------------------------- | |
| google.com | |
| = 17 msec | |
| ---------------------------------------- | |
| yahoo.com | |
| = 163 msec | |
| ---------------------------------------- | |
| d22d.333 | |
| = Service is unavailable | |
| ---------------------------------------- | |
| meta.ua | |
| = 2 msec | |
| ---------------------------------------- | |
| ex.ua | |
| = 2 msec | |
| ---------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ServersCalls; | |
| /* | |
| В файле хранится список серверов. Надо проверить какие из серверов доступны в данный | |
| момент и создать отчет в формате сервер=статус. | |
| */ | |
| public class Main { | |
| public static void main(String[] args) { | |
| ServiceFixException.Begin("d:\\1\\FileList.txt"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ServersCalls; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.function.BiConsumer; | |
| public class ServiceFixException { | |
| private static Map <String,String> map = Collections.synchronizedMap(new HashMap<String,String>()); | |
| //Метод запускает последовательно метод формирования списка ресурсов и проверку их на доступность | |
| public static void Begin (String pathFileList){ | |
| testPage (getListFile(pathFileList) ); | |
| } | |
| // Метод запускает в многопоточном режиме проверку доступности ресурсов по данному списку ссылок на ресурсы | |
| public static void testPage (String [] pathList ){ | |
| System.out.print("----------------------------------------\nMulti-threaded solution." + " Start Threads :\n Begin"); | |
| ArrayList<TestPingPage> listThreat=new ArrayList<>(); | |
| for(int i=0; i < pathList.length; i++){ | |
| listThreat.add(new TestPingPage(pathList[i],map)); | |
| listThreat.get(i).start(); | |
| } | |
| for (TestPingPage t: listThreat){ | |
| System.out.print("..."); | |
| try { | |
| t.join(); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| System.out.println("End\n----------------------------------------" ); | |
| map.forEach(new BiConsumer <String, String> (){ | |
| public void accept(String path, String ping) { | |
| System.out.println(path); | |
| System.out.println(" = " + ping + "\n----------------------------------------"); | |
| } | |
| }); | |
| } | |
| //Метод считывает из файла текст со список северов и преобразовывает в массив серверов | |
| public static String [] getListFile(String pathFileList){ | |
| byte[] buf = null; | |
| if(!new File(pathFileList).isFile()) | |
| return "www.yahoo.com".split(","); | |
| try(FileInputStream inFile = new FileInputStream(pathFileList)){ | |
| buf = new byte[inFile.available()]; | |
| inFile.read(buf); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| return new String(buf).split("\n"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment