From b3edf09be4f7ea908d1adb846a40b0843a676936 Mon Sep 17 00:00:00 2001 From: fhanik Date: Tue, 9 Jun 2009 18:58:25 +0000 Subject: [PATCH] Take into account that executors can be provided, standard and non standard thread pool executors through config or programmatically. Hence we want to expose the correct values for curThread and curThreadBusy git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@783095 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/tomcat/util/net/AprEndpoint.java | 54 +++++++++++++++++++---- java/org/apache/tomcat/util/net/JIoEndpoint.java | 56 ++++++++++++++++++++++-- java/org/apache/tomcat/util/net/NioEndpoint.java | 55 ++++++++++++++++------- 3 files changed, 139 insertions(+), 26 deletions(-) diff --git a/java/org/apache/tomcat/util/net/AprEndpoint.java b/java/org/apache/tomcat/util/net/AprEndpoint.java index e79b9b236..3191ae10e 100644 --- a/java/org/apache/tomcat/util/net/AprEndpoint.java +++ b/java/org/apache/tomcat/util/net/AprEndpoint.java @@ -17,10 +17,12 @@ package org.apache.tomcat.util.net; +import java.lang.reflect.Method; import java.net.InetAddress; import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; @@ -36,6 +38,7 @@ import org.apache.tomcat.jni.SSLContext; import org.apache.tomcat.jni.SSLSocket; import org.apache.tomcat.jni.Socket; import org.apache.tomcat.jni.Status; +import org.apache.tomcat.util.IntrospectionUtils; import org.apache.tomcat.util.res.StringManager; /** @@ -538,27 +541,62 @@ public class AprEndpoint { } } - /** * Return the amount of threads that are managed by the pool. * * @return the amount of threads that are managed by the pool */ public int getCurrentThreadCount() { - return curThreads; + if (executor!=null) { + if (executor instanceof ThreadPoolExecutor) { + return ((ThreadPoolExecutor)executor).getPoolSize(); + } else { + try { + Method m = IntrospectionUtils.findMethod(executor.getClass(), "getPoolSize", new Class[] {}); + if (m!=null) { + return ((Integer)m.invoke(executor, null)).intValue(); + } else { + return -1; + } + }catch (Exception ignore) { + if (log.isDebugEnabled()) + log.debug("Unable to invoke getPoolSize",ignore); + return -2; + } + } + } else { + return curThreads; + } } - /** - * Return the amount of threads currently busy. + * Return the amount of threads that are in use * - * @return the amount of threads currently busy + * @return the amount of threads that are in use */ public int getCurrentThreadsBusy() { - return curThreadsBusy; + if (executor!=null) { + if (executor instanceof ThreadPoolExecutor) { + return ((ThreadPoolExecutor)executor).getActiveCount(); + } else { + try { + Method m = IntrospectionUtils.findMethod(executor.getClass(), "getActiveCount", new Class[] {}); + if (m!=null) { + return ((Integer)m.invoke(executor, null)).intValue(); + } else { + return -1; + } + }catch (Exception ignore) { + if (log.isDebugEnabled()) + log.debug("Unable to invoke getActiveCount",ignore); + return -2; + } + } + } else { + return workers!=null?curThreads - workers.size():0; + } } - - + /** * Return the state of the endpoint. * diff --git a/java/org/apache/tomcat/util/net/JIoEndpoint.java b/java/org/apache/tomcat/util/net/JIoEndpoint.java index 0f3728919..349a172e9 100644 --- a/java/org/apache/tomcat/util/net/JIoEndpoint.java +++ b/java/org/apache/tomcat/util/net/JIoEndpoint.java @@ -18,11 +18,13 @@ package org.apache.tomcat.util.net; import java.io.IOException; +import java.lang.reflect.Method; import java.net.BindException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; @@ -297,12 +299,60 @@ public class JIoEndpoint { return paused; } + /** + * Return the amount of threads that are managed by the pool. + * + * @return the amount of threads that are managed by the pool + */ public int getCurrentThreadCount() { - return curThreads; + if (executor!=null) { + if (executor instanceof ThreadPoolExecutor) { + return ((ThreadPoolExecutor)executor).getPoolSize(); + } else { + try { + Method m = IntrospectionUtils.findMethod(executor.getClass(), "getPoolSize", new Class[] {}); + if (m!=null) { + return ((Integer)m.invoke(executor, null)).intValue(); + } else { + return -1; + } + }catch (Exception ignore) { + if (log.isDebugEnabled()) + log.debug("Unable to invoke getPoolSize",ignore); + return -2; + } + } + } else { + return curThreads; + } } - + + /** + * Return the amount of threads that are in use + * + * @return the amount of threads that are in use + */ public int getCurrentThreadsBusy() { - return workers!=null?curThreads - workers.size():0; + if (executor!=null) { + if (executor instanceof ThreadPoolExecutor) { + return ((ThreadPoolExecutor)executor).getActiveCount(); + } else { + try { + Method m = IntrospectionUtils.findMethod(executor.getClass(), "getActiveCount", new Class[] {}); + if (m!=null) { + return ((Integer)m.invoke(executor, null)).intValue(); + } else { + return -1; + } + }catch (Exception ignore) { + if (log.isDebugEnabled()) + log.debug("Unable to invoke getActiveCount",ignore); + return -2; + } + } + } else { + return workers!=null?curThreads - workers.size():0; + } } diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index e46a2ac76..4339d2f41 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -20,6 +20,7 @@ package org.apache.tomcat.util.net; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.lang.reflect.Method; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -726,32 +727,56 @@ public class NioEndpoint { * @return the amount of threads that are managed by the pool */ public int getCurrentThreadCount() { - final Executor executor = this.executor; if (executor!=null) { - if (executor instanceof java.util.concurrent.ThreadPoolExecutor) { - return ((java.util.concurrent.ThreadPoolExecutor)executor).getPoolSize(); + if (executor instanceof ThreadPoolExecutor) { + return ((ThreadPoolExecutor)executor).getPoolSize(); + } else { + try { + Method m = IntrospectionUtils.findMethod(executor.getClass(), "getPoolSize", new Class[] {}); + if (m!=null) { + return ((Integer)m.invoke(executor, null)).intValue(); + } else { + return -1; + } + }catch (Exception ignore) { + if (log.isDebugEnabled()) + log.debug("Unable to invoke getPoolSize",ignore); + return -2; + } } - } - return 0; + } else { + return -1; + } } - /** - * Return the amount of threads currently busy. + * Return the amount of threads that are in use * - * @return the amount of threads currently busy + * @return the amount of threads that are in use */ public int getCurrentThreadsBusy() { - final Executor executor = this.executor; if (executor!=null) { - if (executor instanceof java.util.concurrent.ThreadPoolExecutor) { - return ((java.util.concurrent.ThreadPoolExecutor)executor).getPoolSize(); + if (executor instanceof ThreadPoolExecutor) { + return ((ThreadPoolExecutor)executor).getActiveCount(); + } else { + try { + Method m = IntrospectionUtils.findMethod(executor.getClass(), "getActiveCount", new Class[] {}); + if (m!=null) { + return ((Integer)m.invoke(executor, null)).intValue(); + } else { + return -1; + } + }catch (Exception ignore) { + if (log.isDebugEnabled()) + log.debug("Unable to invoke getActiveCount",ignore); + return -2; + } } - } - return activeSocketProcessors.get(); + } else { + return -1; + } } - - + /** * Return the state of the endpoint. * -- 2.11.0