You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/10/12 17:03:52 UTC

svn commit: r1182416 - /james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/

Author: norman
Date: Wed Oct 12 15:03:52 2011
New Revision: 1182416

URL: http://svn.apache.org/viewvc?rev=1182416&view=rev
Log:
Expose stats for the ExecutionHandler ThreadPool via JMX. See JMAES-1331

Added:
    james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutor.java   (with props)
    james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean.java   (with props)
Modified:
    james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/AbstractConfigurableAsyncServer.java

Modified: james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/AbstractConfigurableAsyncServer.java
URL: http://svn.apache.org/viewvc/james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/AbstractConfigurableAsyncServer.java?rev=1182416&r1=1182415&r2=1182416&view=diff
==============================================================================
--- james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/AbstractConfigurableAsyncServer.java (original)
+++ james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/AbstractConfigurableAsyncServer.java Wed Oct 12 15:03:52 2011
@@ -51,7 +51,6 @@ import org.jboss.netty.channel.ChannelPi
 import org.jboss.netty.channel.ChannelUpstreamHandler;
 import org.jboss.netty.channel.group.ChannelGroup;
 import org.jboss.netty.handler.execution.ExecutionHandler;
-import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
 import org.slf4j.Logger;
 
 /**
@@ -114,6 +113,7 @@ public abstract class AbstractConfigurab
 
     private MBeanServer mbeanServer;
 
+    
     @Resource(name = "filesystem")
     public final void setFileSystem(FileSystem filesystem) {
         this.fileSystem = filesystem;
@@ -473,14 +473,18 @@ public abstract class AbstractConfigurab
         return connectionLimit;
     }
 
+    protected String getThreadPoolJMXPath() {
+        return "org.apache.james:type=server,name=" + jmxName + ",sub-type=threadpool";
+    }
+    
     @Override
     protected Executor createBossExecutor() {
-        return JMXEnabledThreadPoolExecutor.newCachedThreadPool("org.apache.james:type=server,name=" + jmxName + ",sub-type=threadpool", "boss");
+        return JMXEnabledThreadPoolExecutor.newCachedThreadPool(getThreadPoolJMXPath(), "boss");
     }
 
     @Override
     protected Executor createWorkerExecutor() {
-        return JMXEnabledThreadPoolExecutor.newCachedThreadPool("org.apache.james:type=server,name=" + jmxName + ",sub-type=threadpool", "worker");
+        return JMXEnabledThreadPoolExecutor.newCachedThreadPool(getThreadPoolJMXPath(), "worker");
     }
 
     /**
@@ -570,7 +574,7 @@ public abstract class AbstractConfigurab
      * @return ehandler
      */
     protected ExecutionHandler createExecutionHander() {
-        return new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(maxExecutorThreads, 0, 0));
+        return new ExecutionHandler(new JMXEnabledOrderedMemoryAwareThreadPoolExecutor(maxExecutorThreads, 0, 0, getThreadPoolJMXPath(), "executor"));
     }
 
     /**

Added: james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutor.java
URL: http://svn.apache.org/viewvc/james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutor.java?rev=1182416&view=auto
==============================================================================
--- james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutor.java (added)
+++ james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutor.java Wed Oct 12 15:03:52 2011
@@ -0,0 +1,163 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.protocols.lib.netty;
+
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.james.util.concurrent.NamedThreadFactory;
+import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
+
+/**
+ * {@link OrderedMemoryAwareThreadPoolExecutor} subclass which expose statistics via JMX
+ * 
+ *
+ */
+public class JMXEnabledOrderedMemoryAwareThreadPoolExecutor extends OrderedMemoryAwareThreadPoolExecutor implements JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean{
+
+    private String jmxPath;
+    private final List<Runnable> inProgress = Collections.synchronizedList(new ArrayList<Runnable>());
+    private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
+    private long totalTime;
+    private int totalTasks;
+    private MBeanServer mbeanServer;
+    private String mbeanName;
+    
+    public JMXEnabledOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, String jmxPath, String name) {
+        super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, 30, TimeUnit.SECONDS, new NamedThreadFactory(name));
+        this.jmxPath = jmxPath;
+        registerMBean();
+    }
+    
+
+
+
+
+    @Override
+    protected void beforeExecute(Thread t, Runnable r) {
+        super.beforeExecute(t, r);
+        inProgress.add(r);
+        startTime.set(System.currentTimeMillis());
+    }
+
+    @Override
+    protected void afterExecute(Runnable r, Throwable t) {
+        long time = System.currentTimeMillis() - startTime.get().longValue();
+        synchronized (this) {
+            totalTime += time;
+            ++totalTasks;
+        }
+        inProgress.remove(r);
+        super.afterExecute(r, t);
+    }
+
+    private void registerMBean() {
+        if (jmxPath != null) {
+            mbeanServer = ManagementFactory.getPlatformMBeanServer();
+            mbeanName = jmxPath + ",threadpool=" + ((NamedThreadFactory) getThreadFactory()).getName();
+            try {
+                mbeanServer.registerMBean(this, new ObjectName(mbeanName));
+            } catch (Exception e) {
+                throw new RuntimeException("Unable to register mbean", e);
+            }
+        }
+    }
+
+    private void unregisterMBean() {
+        if (jmxPath != null) {
+            try {
+                mbeanServer.unregisterMBean(new ObjectName(mbeanName));
+
+            } catch (Exception e) {
+                throw new RuntimeException("Unable to unregister mbean", e);
+            }
+        }
+    }
+
+    @Override
+    public synchronized void shutdown() {
+        // synchronized, because there is no way to access super.mainLock, which
+        // would be
+        // the preferred way to make this threadsafe
+        if (!isShutdown()) {
+            unregisterMBean();
+        }
+        super.shutdown();
+    }
+
+    @Override
+    public synchronized List<Runnable> shutdownNow() {
+        // synchronized, because there is no way to access super.mainLock, which
+        // would be
+        // the preferred way to make this threadsafe
+        if (!isShutdown()) {
+            unregisterMBean();
+        }
+        return super.shutdownNow();
+    }
+
+    /**
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#getTotalTasks()
+     */
+    public synchronized int getTotalTasks() {
+        return totalTasks;
+    }
+
+    /**
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#getAverageTaskTime()
+     */
+    public synchronized double getAverageTaskTime() {
+        return (totalTasks == 0) ? 0 : totalTime / totalTasks;
+    }
+
+    /**
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#getActiveThreads()
+     */
+    public int getActiveThreads() {
+        return getPoolSize();
+    }
+
+    /**
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#getActiveTasks()
+     */
+    public int getActiveTasks() {
+        return getActiveCount();
+    }
+
+    /**
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#getQueuedTasks()
+     */
+    public int getQueuedTasks() {
+        return getQueue().size();
+    }
+
+    /**
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#getMaximalThreads()
+     */
+    public int getMaximalThreads() {
+        return getMaximumPoolSize();
+    }
+
+}

Propchange: james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean.java
URL: http://svn.apache.org/viewvc/james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean.java?rev=1182416&view=auto
==============================================================================
--- james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean.java (added)
+++ james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean.java Wed Oct 12 15:03:52 2011
@@ -0,0 +1,25 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.protocols.lib.netty;
+
+import org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean;
+
+public interface JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean extends JMXEnabledThreadPoolExecutorMBean{
+
+}

Propchange: james/server/trunk/protocols-library/src/main/java/org/apache/james/protocols/lib/netty/JMXEnabledOrderedMemoryAwareThreadPoolExecutorMBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org