https://qiita.com/sotoiwa/items/15cac258ed90d7861bdf
https://qiita.com/rinaxsumomo/items/9d0776f8a3e506fdebc4
Libertyの性能情報を取得する記事を見て、やってみたら、ハマったことをメモ。
- Jsoup で basic認識のやり方がわからない。
- Jsoup で https にアクセスするとエラーになる。
- Gson で配列の解析方法がわからない。
上記3つは、以下のソースで実現できる。
特に難しいことやってないので、説明は省く。
SSLHelper はこちらから:
https://stackoverflow.com/questions/40742380/how-to-resolve-jsoup-error-unable-to-find-valid-certification-path-to-requested
SSLHelper.java
package liberty;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
public class SSLHelper {
static public Connection getConnection(String url) {
return Jsoup.connect(url).sslSocketFactory(SSLHelper.socketFactory());
}
static private SSLSocketFactory socketFactory() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory result = sslContext.getSocketFactory();
return result;
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Failed to create a SSL socket factory", e);
}
}
}
LibertyJvmStats.java
package liberty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import org.jsoup.nodes.Document;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class LibertyJvmStats {
public static void main(String[] args) throws IOException {
String username = "jmxadmin";
String password = "password";
String login = username + ":" + password;
String base64login = new String(Base64.getEncoder().encode(login.getBytes()));
// https://localhost:9443/IBMJMXConnectorREST/mbeans/WebSphere%3atype%3dThreadPoolStats%2cname%3dDefault%20Executor/attributes
// https://localhost:9443/IBMJMXConnectorREST/mbeans/WebSphere%3Aname%3Ddefault_host%2FIBMJMXConnectorREST%2Ctype%3DSessionStats/attributes
Document document = SSLHelper.getConnection("https://localhost:9443/IBMJMXConnectorREST/mbeans/WebSphere%3Atype%3DJvmStats/attributes") //
.header("Authorization", "Basic " + base64login) //
.ignoreContentType(true) //
.ignoreHttpErrors(false) //
.get();
Gson gson = new Gson();
List<LibertyJvmStats_Bean> list = gson.fromJson(document.text(), TypeToken.getParameterized(ArrayList.class, LibertyJvmStats_Bean.class).getType());
list.forEach(val -> {
switch (val.name) {
case "UpTime":
long time = Long.parseLong(val.value.value) / 1000 / 60;
System.out.println(String.format("%12s : %d min", val.name, time));
break;
case "Heap":
case "FreeMemory":
case "UsedMemory":
long value = Long.parseLong(val.value.value) / 1024 / 1024;
System.out.println(String.format("%12s : %d mb", val.name, value));
break;
default:
System.out.println(String.format("%12s : %s", val.name, val.value.value));
}
});
}
private class LibertyJvmStats_Bean {
public String name;
public LibertyJvmStats_Bean_value value;
public class LibertyJvmStats_Bean_value {
public String value;
public String type;
}
}
}
ちなみに、https://localhost:9443/IBMJMXConnectorREST/mbeans/WebSphere%3Atype%3DJvmStats/attributes
の戻り値はこちら:
[
{
"name": "ProcessCPU",
"value": {
"value": "0.0",
"type": "java.lang.Double"
}
},
{
"name": "Heap",
"value": {
"value": "543162368",
"type": "java.lang.Long"
}
},
{
"name": "FreeMemory",
"value": {
"value": "348815256",
"type": "java.lang.Long"
}
},
{
"name": "GcTime",
"value": {
"value": "244",
"type": "java.lang.Long"
}
},
{
"name": "UsedMemory",
"value": {
"value": "194347112",
"type": "java.lang.Long"
}
},
{
"name": "GcCount",
"value": {
"value": "18",
"type": "java.lang.Long"
}
},
{
"name": "UpTime",
"value": {
"value": "625122",
"type": "java.lang.Long"
}
}
]