Take into account that executors can be provided, standard and non standard thread...
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 9 Jun 2009 18:58:25 +0000 (18:58 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 9 Jun 2009 18:58:25 +0000 (18:58 +0000)
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
java/org/apache/tomcat/util/net/JIoEndpoint.java
java/org/apache/tomcat/util/net/NioEndpoint.java

index e79b9b2..3191ae1 100644 (file)
 
 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.
      *
index 0f37289..349a172 100644 (file)
 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;
+        }
     }
     
 
index e46a2ac..4339d2f 100644 (file)
@@ -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.
      *