While capturing local system stats I needed a way to run parallel captures
The code below is still a work in progress how to can/will be done, more on that is explained below.
For example:
I am trying to capture CPU, Mem every second, but at the same I am also capturing my db response time with a real query that takes 10+ seconds.
In the example above both captures CPU Mem & DB response need to run at the same time but may and will complete in different intervals.
In such a case I will have to use the ExecutorService module.
I am still working out the details.
Note: The below url’s wil be replaced by process once completed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package dualrun; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * * @author Eli Kleinman */ public class DualRun { private static final int MYTHREADS = 1; public static void main(String[] args) throws Exception { ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS); String[] hostList = {"http://url1.com", "http://url2.com", "http://url3.com", "http://url4.com", "http://url5.com"}; for (int i = 0; i < hostList.length; i++) { String url = hostList[i]; Runnable worker = new MyRunnable(url); executor.execute(worker); } executor.shutdown(); // Wait until all threads are finish while (!executor.isTerminated()) { } System.out.println("\nFinished all threads"); } public static class MyRunnable implements Runnable { private final String url; MyRunnable(String url) { this.url = url; } @Override public void run() { String result = ""; int code = 200; try { URL siteURL = new URL(url); HttpURLConnection connection = (HttpURLConnection) siteURL .openConnection(); connection.setRequestMethod("GET"); connection.connect(); code = connection.getResponseCode(); if (code == 200) { System.out.println("code: "+code); result = "Green\t"; } else { } } catch (Exception e) { System.out.println("code: empty"); result = "->Red<-\t"; } System.out.println(url + "\t\tStatus:" + result); } } } |
Leave a Reply