Skip to content

Instantly share code, notes, and snippets.

@mchirico
Last active December 11, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchirico/4579783 to your computer and use it in GitHub Desktop.
Save mchirico/4579783 to your computer and use it in GitHub Desktop.
The following is an example of the ExecutorService to make system calls.
package dev.sysadmin.test;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CmdTdev {
private static int myid;
public CmdTdev() {
myid++;
}
public void perform() {
System.out.println("id " + myid + " start sleep");
new Sleep(5);
System.out.println("id " + myid + " after sleep");
}
public static void main(String[] args) {
CmdTdev c = new CmdTdev();
int scaleFactor = 2;
int cpus = Runtime.getRuntime().availableProcessors();
int maxThreads = cpus * scaleFactor;
maxThreads = (maxThreads > 0 ? maxThreads : 1);
System.out.println("cpus: " + cpus);
System.out.println("maxThreads: " + maxThreads);
ExecutorService executorService = new ThreadPoolExecutor(maxThreads, // core
// thread
// pool
// size
maxThreads, // maximum thread pool size
1, // time to wait before resizing pool
TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(maxThreads,
true), new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 5; ++i) {
executorService.submit(new Runnable() {
@Override
public void run() {
try {
// if it's a file, process it
new CmdW().perform(new String[] { "imac","rh","rh2","ss","tv","pi" });
} catch (Exception ex) {
// error management logic
}
}
});
}
executorService.shutdown();
try {
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
// pool didn't terminate after the first try
executorService.shutdownNow();
}
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
// pool didn't terminate after the second try
}
} catch (InterruptedException ex) {
executorService.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
package dev.sysadmin.test;
/*
* String s = "ps -ef "+i+"|grep 'RSite:'|sort";
CmdW cmd = new CmdW(new String[] { "/bin/bash","-c",s, });
*
*
*/
import java.util.*;
import java.io.*;
class StrGrab0 extends Thread {
private InputStream is;
private String type;
private List<String> lines = new ArrayList<String>();
private Collection<String> c = Collections.synchronizedCollection(lines);
private String delims = new String("[ ,\t]+");
StrGrab0(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void pr() {
synchronized (c) {
Iterator<String> i = c.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
public void clear() {
lines.clear();
}
public List<String> getList() {
return lines;
}
public void setDelims(String delims) {
this.delims = delims;
}
public String[][] getTokens() {
Object[] data = lines.toArray();
// String delims = "[ ,\t]+";
String[][] tokens;
tokens = new String[data.length][];
for (int i = 0; i < data.length; ++i) {
tokens[i] = data[i].toString().split(delims);
}
return tokens;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
synchronized (c) {
while ((line = br.readLine()) != null) {
c.add(line.toString());
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class CmdW {
// Process proc = rt.exec(new String[]
// {"ssh","rh","df","-h","|","grep","-v","'^Filesystem'","|","awk '{print $5,$6}'"});
private StrGrab0 errorGrab;
private StrGrab0 outputGrab;
private int exitVal;
private String osName;
public CmdW() {
}
CmdW(String[] cmd) {
try {
osName = System.getProperty("os.name");
// System.out.println("System: "+osName);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
this.errorGrab = new StrGrab0(proc.getErrorStream(), "ERROR");
this.outputGrab = new StrGrab0(proc.getInputStream(), "OUTPUT");
// kick them off
this.errorGrab.start();
this.outputGrab.start();
// any error???
this.exitVal = proc.waitFor();
// System.out.println("ExitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
public void pr() {
outputGrab.pr();
}
public String[][] getTokens() {
return outputGrab.getTokens();
}
public void setDelims(String delims) {
outputGrab.setDelims(delims);
}
public void prE() {
errorGrab.pr();
}
public void perform(String[] servers) {
for (String i : servers) {
System.out.println("\nServer:" + i);
String scmd = "date >> /tmp/CmdW";
CmdW c = new CmdW(new String[] { "/usr/bin/ssh", "-q", "-o",
"ConnectTimeout=1", "-o", "NumberOfPasswordPrompts=0", i,
scmd });
c.pr();
String[][] s = c.getTokens();
if (s.length >= 1) {
System.out.println(s.length);
if (s[1].length >= 1) {
System.out.println(s[1].length);
System.out.println(s[1][1]);
}
}
}
}
public static void main(String args[]) {
for (String i : new String[] { "mAW" }) {
System.out.println("\nServer:" + i);
String scmd = "date >> /tmp/CmdW";
CmdW c = new CmdW(new String[] { "/usr/bin/ssh", "-q", "-o",
"ConnectTimeout=1", "-o", "NumberOfPasswordPrompts=0", i,
scmd });
c.pr();
String[][] s = c.getTokens();
if (s.length >= 0) {
System.out.println(s.length);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment