定期的にjolokiaからデータを抜いてファイル出力するデーモンプログラムを作る必要に迫られたので作ってみた。
とりあえず、ソースぺたり。
package jp.shibadog.daemon;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.management.MalformedObjectNameException;
import org.jolokia.client.J4pClient;
import org.jolokia.client.exception.J4pException;
import org.jolokia.client.request.J4pReadRequest;
import org.jolokia.client.request.J4pReadResponse;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
public class DaemonTool {
public static SimpleDateFormat sdf1 = new SimpleDateFormat(
"yyyy/MM/dd:HH:mm:ss");
@Option(name = "-c", aliases = "--config", usage = "コンフィグファイルの指定")
private String confPath = "";
public static void main(String[] args) {
DaemonTool dt = new DaemonTool();
dt.execute(args);
}
private void execute(String[] args) {
if (!init(args)) {
System.exit(1);
}
if (this.confPath != null && !"".equals(this.confPath)) {
JSONParser parser = new JSONParser();
String s = null;
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
this.confPath)));
StringBuilder sb = new StringBuilder();
String tmp = br.readLine();
while (tmp != null) {
sb.append(tmp);
tmp = br.readLine();
}
s = sb.toString();
System.out.println(s);
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
Object obj;
try {
obj = parser.parse(s);
JSONObject json = (JSONObject) obj;
System.out.println("request-url=" + json.get("requestUrl"));
} catch (ParseException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
}
}
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
J4pReadRequest req;
try {
req = new J4pReadRequest("java.lang:type=Memory", "HeapMemoryUsage");
req.setPath("used");
String urlRoot = "http://localhost:7001/jolokia-war-1.3.1";
String hostname = getHostName(urlRoot);
service.scheduleAtFixedRate(new ExecuteMBeanGetter(hostname, urlRoot, req),
0, 5, TimeUnit.SECONDS);
} catch (MalformedObjectNameException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} finally {
}
}
private boolean init(String[] args) {
boolean ret = false;
CmdLineParser parser = new CmdLineParser(this);
try {
// parse options
parser.parseArgument(args);
System.out.println("Starting application...");
System.out.println("Configuration : " + confPath);
// System.out.println("Server mode : " + isServer);
// System.out.println("Destination : " + dst);
// your code
ret = true;
} catch (CmdLineException e) {
// print usage.
System.out.println(e);
parser.printUsage(System.out);
System.exit(1);
}
return ret;
}
public String getHostName(String urlRoot) {
String res = null;
J4pClient j4pClient = new J4pClient(urlRoot);
try {
J4pReadRequest req = new J4pReadRequest("java.lang:type=Runtime", "Name");
J4pReadResponse resp = j4pClient.execute(req);
String tmp = resp.getValue();
res = tmp.substring(tmp.indexOf("@") + 1);
} catch (J4pException | MalformedObjectNameException e) {
System.out.println(e);
e.printStackTrace();
}
return res;
}
class ExecuteMBeanGetter implements Runnable {
private J4pReadRequest req;
private String urlRoot;
private String hostname;
public ExecuteMBeanGetter(String hostname, String urlRoot2, J4pReadRequest req2) {
setUrlRoot(urlRoot2);
setReq(req2);
setHostname(hostname);
}
@Override
public void run() {
J4pClient j4pClient = new J4pClient(getUrlRoot());
try {
J4pReadResponse resp = j4pClient.execute(getReq());
System.out
.println(DaemonTool.sdf1.format(resp.getRequestDate())
+ "," + getHostname() + "," + resp.getValue());
} catch (J4pException e) {
System.out.println(e);
e.printStackTrace();
}
}
public J4pReadRequest getReq() {
return req;
}
private void setReq(J4pReadRequest req) {
this.req = req;
}
public String getUrlRoot() {
return urlRoot;
}
private void setUrlRoot(String urlRoot) {
this.urlRoot = urlRoot;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
}
}