(1 votes, average: 1.00 out of 5)
Loading...
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.
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.
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); } } }
0
0
votes
Article Rating