Skip to content

Commit

Permalink
Add logic to handle tomcat 10 to 11 'int' to 'long' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Feb 22, 2025
1 parent bfe9d3e commit 5b1e89c
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions psi-probe-core/src/main/java/psiprobe/tools/ApplicationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,45 @@ public static void collectApplicationServletStats(Context context, Application a
if (container instanceof StandardWrapper) {
StandardWrapper sw = (StandardWrapper) container;
svltCount++;
reqCount += sw.getRequestCount();
errCount += sw.getErrorCount();

// Get Request Count (bridge between tomcat 10 and 11 using int vs long
Object requestCount = null;
try {
requestCount = MethodUtils.invokeMethod(sw, "getRequestCount");
if (requestCount instanceof Long) {
// tomcat 11+
reqCount += (long) requestCount;
} else {
// tomcat 10
reqCount += (int) requestCount;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("Unable to find getRequestCount", e);
}

// Get Error Count (bridge between tomcat 10 and 11 using int vs long
try {
Object errorCount = MethodUtils.invokeMethod(sw, "getErrorCount");
if (errorCount instanceof Long) {
// Tomcat 11+
errCount += (long) errorCount;
} else {
// Tomcat 10
errCount += (int) errorCount;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("Unable to find getErrorCount", e);
}

procTime += sw.getProcessingTime();
if (sw.getRequestCount() > 0) {
minTime = Math.min(minTime, sw.getMinTime());
if (requestCount != null) {
if (requestCount instanceof Long && (long) requestCount > 0) {
// Tomcat 11+
minTime = Math.min(minTime, sw.getMinTime());
} else if (requestCount instanceof Integer && (int) requestCount > 0) {
// Tomcat 10
minTime = Math.min(minTime, sw.getMinTime());
}
}
maxTime = Math.max(maxTime, sw.getMaxTime());
}
Expand Down Expand Up @@ -371,12 +405,40 @@ private static ServletInfo getServletInfo(Wrapper wrapper, String contextName) {
if (wrapper instanceof StandardWrapper) {
StandardWrapper sw = (StandardWrapper) wrapper;
si.setAllocationCount(sw.getCountAllocated());
si.setErrorCount(sw.getErrorCount());

// Get Error Count (bridge between tomcat 10 and 11 using int vs long
try {
Object errorCount = MethodUtils.invokeMethod(sw, "getErrorCount");
if (errorCount instanceof Long) {
// Tomcat 11+
si.setErrorCount((long) errorCount);
} else {
// Tomcat 10
si.setErrorCount((int) errorCount);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("Unable to find getErrorCount", e);
}

si.setLoadTime(sw.getLoadTime());
si.setMaxTime(sw.getMaxTime());
si.setMinTime(sw.getMinTime() == Long.MAX_VALUE ? 0 : sw.getMinTime());
si.setProcessingTime(sw.getProcessingTime());
si.setRequestCount(sw.getRequestCount());

// Get Request Count (bridge between tomcat 10 and 11 using int vs long
try {
Object requestCount = MethodUtils.invokeMethod(sw, "getRequestCount");
if (requestCount instanceof Long) {
// Tomcat 11+
si.setRequestCount((long) requestCount);
} else {
// Tomcat 10
si.setRequestCount((int) requestCount);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("Unable to find getRequestCount", e);
}

}
return si;
}
Expand Down

0 comments on commit 5b1e89c

Please sign in to comment.