Skip to content

Commit b2df423

Browse files
committed
[#3] Add api /app/env & Modify api /app/status
1 parent 9949b66 commit b2df423

File tree

5 files changed

+208
-125
lines changed

5 files changed

+208
-125
lines changed

src/main/java/org/code13k/heets/app/Env.java

Lines changed: 109 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8+
import java.io.IOException;
9+
import java.io.InputStream;
810
import java.net.InetAddress;
11+
import java.net.URL;
12+
import java.util.Enumeration;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.jar.Attributes;
16+
import java.util.jar.JarFile;
17+
import java.util.jar.Manifest;
918

1019
public class Env {
1120
// Logger
@@ -14,7 +23,6 @@ public class Env {
1423
// Data
1524
private String mHostname = "";
1625
private String mIP = "";
17-
private int mProcessorCount = 0;
1826
private String mVersionString = "";
1927
private String mJarFilename = "";
2028

@@ -56,30 +64,15 @@ public void init() {
5664
mLogger.error("Failed to get IP", e);
5765
}
5866

59-
// Processor Count
60-
try {
61-
mProcessorCount = Runtime.getRuntime().availableProcessors();
62-
} catch (Exception e) {
63-
mLogger.error("Filed to get processor count", e);
64-
}
65-
6667
// Version
6768
try {
68-
mVersionString = Util.getApplicationVersion();
69+
mVersionString = parseApplicationVersion();
6970
} catch (Exception e) {
7071
mLogger.error("Failed to get version", e);
7172
}
7273

7374
// Jar File Name
74-
String javaClassPath = System.getProperty("java.class.path");
75-
String jarFilename = "";
76-
if (StringUtils.isBlank(javaClassPath) == false) {
77-
String[] temp = StringUtils.split(javaClassPath, "/");
78-
if (temp.length > 0) {
79-
jarFilename = temp[temp.length - 1];
80-
}
81-
}
82-
mJarFilename = jarFilename;
75+
mJarFilename = parseJarFilename();
8376

8477
// End
8578
logging();
@@ -89,35 +82,31 @@ public void init() {
8982
* Logging
9083
*/
9184
public void logging() {
92-
// Begin
85+
Map<String, Object> values = values();
9386
mLogger.info("------------------------------------------------------------------------");
9487
mLogger.info("Application Environments");
9588
mLogger.info("------------------------------------------------------------------------");
96-
97-
// Hostname
98-
mLogger.info("Hostname = " + mHostname);
99-
100-
// IP
101-
mLogger.info("IP = " + mIP);
102-
103-
// Processor Count
104-
mLogger.info("Processor count = " + mProcessorCount);
105-
106-
// Version
107-
mLogger.info("Version = " + mVersionString);
108-
109-
// Jar File Name
110-
mLogger.info("Jar filename = " + mJarFilename);
111-
112-
// End
89+
values.forEach((k, v) -> mLogger.info(k + " = " + v));
11390
mLogger.info("------------------------------------------------------------------------");
11491
}
11592

11693
/**
117-
* String of app version
94+
* Get all values
11895
*/
119-
public String getVersionString() {
120-
return mVersionString;
96+
public Map<String, Object> values() {
97+
HashMap<String, Object> result = new HashMap<>();
98+
99+
result.put("hostname", getHostname());
100+
result.put("ip", getIP());
101+
result.put("cpuProcessorCount", getProcessorCount());
102+
result.put("applicationVersion", getVersionString());
103+
result.put("jarFile", getJarFilename());
104+
result.put("javaVersion", getJavaVersion());
105+
result.put("javaVendor", getJavaVendor());
106+
result.put("osVersion", getOsVersion());
107+
result.put("osName", getOsName());
108+
109+
return result;
121110
}
122111

123112
/**
@@ -138,13 +127,92 @@ public String getIP() {
138127
* Processor count of server
139128
*/
140129
public int getProcessorCount() {
141-
return mProcessorCount;
130+
return Runtime.getRuntime().availableProcessors();
131+
}
132+
133+
/**
134+
* String of app version
135+
*/
136+
public String getVersionString() {
137+
return mVersionString;
142138
}
143139

144140
/**
145141
* Filename of Jar
146142
*/
147-
public String getJarFilename(){
143+
public String getJarFilename() {
148144
return mJarFilename;
149145
}
146+
147+
/**
148+
* Get java version
149+
*/
150+
public String getJavaVersion() {
151+
return System.getProperty("java.version");
152+
}
153+
154+
/**
155+
* Get java vendor
156+
*/
157+
public String getJavaVendor() {
158+
return System.getProperty("java.vendor");
159+
}
160+
161+
/**
162+
* Get OS name
163+
*/
164+
public String getOsName() {
165+
return System.getProperty("os.name");
166+
}
167+
168+
/**
169+
* Get OS version
170+
*/
171+
public String getOsVersion() {
172+
return System.getProperty("os.version");
173+
}
174+
175+
/**
176+
* Get app version from manifest info
177+
*/
178+
private String parseApplicationVersion() {
179+
Enumeration resourceEnum;
180+
try {
181+
resourceEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
182+
while (resourceEnum.hasMoreElements()) {
183+
try {
184+
URL url = (URL) resourceEnum.nextElement();
185+
InputStream is = url.openStream();
186+
if (is != null) {
187+
Manifest manifest = new Manifest(is);
188+
Attributes attr = manifest.getMainAttributes();
189+
String version = attr.getValue("Implementation-Version");
190+
if (version != null) {
191+
return version;
192+
}
193+
}
194+
} catch (Exception e) {
195+
// Nothing
196+
}
197+
}
198+
} catch (IOException e1) {
199+
// Nothing
200+
}
201+
return null;
202+
}
203+
204+
/**
205+
* Get jar file name from system property
206+
*/
207+
private String parseJarFilename() {
208+
String javaClassPath = System.getProperty("java.class.path");
209+
String jarFilename = "";
210+
if (StringUtils.isBlank(javaClassPath) == false) {
211+
String[] temp = StringUtils.split(javaClassPath, "/");
212+
if (temp.length > 0) {
213+
jarFilename = temp[temp.length - 1];
214+
}
215+
}
216+
return jarFilename;
217+
}
150218
}

src/main/java/org/code13k/heets/app/Status.java

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package org.code13k.heets.app;
22

3-
import io.netty.channel.group.ChannelGroup;
43
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
65

6+
import java.lang.management.ManagementFactory;
7+
import java.lang.management.OperatingSystemMXBean;
8+
import java.lang.management.ThreadInfo;
9+
import java.lang.management.ThreadMXBean;
10+
import java.text.DecimalFormat;
11+
import java.text.NumberFormat;
712
import java.text.SimpleDateFormat;
8-
import java.util.Date;
9-
import java.util.Timer;
10-
import java.util.TimerTask;
13+
import java.util.*;
1114

15+
/**
16+
* Status
17+
*/
1218
public class Status {
1319
// Logger
1420
private static final Logger mLogger = LoggerFactory.getLogger(Status.class);
@@ -71,6 +77,25 @@ public void logging() {
7177
mLogger.info(sb.toString());
7278
}
7379

80+
/**
81+
* Get all values
82+
*/
83+
public Map<String, Object> values() {
84+
HashMap<String, Object> result = new HashMap<>();
85+
86+
// Common
87+
HashMap<Long, String> threadInfo = getThreadInfo();
88+
result.put("threadInfo", threadInfo);
89+
result.put("threadCount", threadInfo.size());
90+
result.put("startedDate", getAppStartedDateString());
91+
result.put("currentDate", getCurrentDateString());
92+
result.put("runningTimeHour", getAppRunningTimeHour());
93+
result.put("cpuUsage", getCpuUsage());
94+
result.put("vmMemoryUsage", getVmMemoryUsage());
95+
96+
return result;
97+
}
98+
7499
/**
75100
* Get application started time
76101
*/
@@ -107,4 +132,54 @@ public int getAppRunningTimeHour() {
107132
int runningTimeHour = runningTimeMin / 60;
108133
return runningTimeHour;
109134
}
135+
136+
/**
137+
* Get CPU usage
138+
*/
139+
public double getCpuUsage() {
140+
// CPU Usage
141+
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
142+
double cpuUsage = operatingSystemMXBean.getSystemLoadAverage();
143+
return new Double(new DecimalFormat("#.##").format(cpuUsage));
144+
}
145+
146+
/**
147+
* Get memory usage of VM
148+
*/
149+
public HashMap<String, String> getVmMemoryUsage() {
150+
// VM Memory Usage
151+
Runtime runtime = Runtime.getRuntime();
152+
NumberFormat format = NumberFormat.getInstance();
153+
long vmMemoryMax = runtime.maxMemory();
154+
long vmMemoryAllocated = runtime.totalMemory();
155+
long vmMemoryFree = runtime.freeMemory();
156+
String vmMemoryMaxString = format.format(vmMemoryMax / 1024 / 1024) + "M";
157+
String vmMemoryAllocatedString = format.format(vmMemoryAllocated / 1024 / 1024) + "M";
158+
String vmMemoryFreeString = format.format(vmMemoryFree / 1024 / 1024) + "M";
159+
String vmMemoryTotalFreeString = format.format((vmMemoryFree + (vmMemoryMax - vmMemoryAllocated)) / 1024 / 1024) + "M";
160+
161+
// Result
162+
HashMap<String, String> result = new HashMap<>();
163+
result.put("max", vmMemoryMaxString);
164+
result.put("allocated", vmMemoryAllocatedString);
165+
result.put("free", vmMemoryFreeString);
166+
result.put("totalFree", vmMemoryTotalFreeString);
167+
return result;
168+
}
169+
170+
/**
171+
* Get thread info
172+
*/
173+
public HashMap<Long, String> getThreadInfo() {
174+
NumberFormat format = NumberFormat.getInstance();
175+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
176+
long[] threadIds = threadMXBean.getAllThreadIds();
177+
HashMap<Long, String> threadResult = new HashMap<>();
178+
for (long threadId : threadIds) {
179+
ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId);
180+
String threadInfoValue = threadInfo.getThreadName() + ", " + format.format(threadMXBean.getThreadCpuTime(threadId) / 1000000000) + "sec";
181+
threadResult.put(threadId, threadInfoValue);
182+
}
183+
return threadResult;
184+
}
110185
}

src/main/java/org/code13k/heets/business/ClusteredCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ClusteredCache {
2222
private static final Logger mLogger = LoggerFactory.getLogger(ClusteredCache.class);
2323

2424
// Const
25-
private static final String NAME = "Code13k-Heets-Expired-Cached-Map-Data";
25+
private static final String NAME = "Code13k-Heets-Cache-Data";
2626

2727
// Data
2828
private IMap<String, CacheData> mData = null;

src/main/java/org/code13k/heets/service/api/ApiHttpServer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,21 @@ private void logging(HttpServerOptions httpServerOptions, Router router) {
8181
* Set app router
8282
*/
8383
private void setAppRouter(Router router) {
84+
// GET /app/env
85+
router.route().method(HttpMethod.GET).path("/app/env").handler(routingContext -> {
86+
routingContext.request().endHandler(new Handler<Void>() {
87+
@Override
88+
public void handle(Void event) {
89+
responseHttpOK(routingContext, mAppAPI.env());
90+
}
91+
});
92+
});
8493
// GET /app/status
8594
router.route().method(HttpMethod.GET).path("/app/status").handler(routingContext -> {
8695
routingContext.request().endHandler(new Handler<Void>() {
8796
@Override
8897
public void handle(Void event) {
89-
responseHttpOK(routingContext, mAppAPI.info());
98+
responseHttpOK(routingContext, mAppAPI.status());
9099
}
91100
});
92101
});

0 commit comments

Comments
 (0)