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 2010/12/15 16:21:50 UTC

svn commit: r1049587 - in /james/server/trunk: container-spring/src/main/config/examples/ container-spring/src/main/config/james/ container-spring/src/main/config/james/context/ fetchmail/src/main/java/org/apache/james/fetchmail/ util/src/main/java/org...

Author: norman
Date: Wed Dec 15 15:21:49 2010
New Revision: 1049587

URL: http://svn.apache.org/viewvc?rev=1049587&view=rev
Log:
Use a JMX enabled ScheduledThreadPool in FetchMail so we can get some more stats. See JAMES-1057

Added:
    james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutor.java
    james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutorMBean.java
Modified:
    james/server/trunk/container-spring/src/main/config/examples/fetchmail.xml
    james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml
    james/server/trunk/container-spring/src/main/config/james/fetchmail.xml
    james/server/trunk/fetchmail/src/main/java/org/apache/james/fetchmail/FetchScheduler.java

Modified: james/server/trunk/container-spring/src/main/config/examples/fetchmail.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/config/examples/fetchmail.xml?rev=1049587&r1=1049586&r2=1049587&view=diff
==============================================================================
--- james/server/trunk/container-spring/src/main/config/examples/fetchmail.xml (original)
+++ james/server/trunk/container-spring/src/main/config/examples/fetchmail.xml Wed Dec 15 15:21:49 2010
@@ -38,6 +38,13 @@
 <!-- fetch task's name parameter described below. -->
    
 <fetchmail enabled="false">
+    <!-- The number of threads to use for the scheduled execution -->
+    <threads>5</threads>
+    
+    <!-- The JMX Name to use -->
+    <jmxName>fetchmail</jmxName>
+    
+    
     <!-- You can have as many fetch tasks as you want, but each must have a -->
     <!-- unique name by which it is identified. -->
     <!-- Each task runs at the specified <interval>, tasks may run concurrently. -->      

Modified: james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml?rev=1049587&r1=1049586&r2=1049587&view=diff
==============================================================================
--- james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml (original)
+++ james/server/trunk/container-spring/src/main/config/james/context/james-server-context.xml Wed Dec 15 15:21:49 2010
@@ -36,13 +36,6 @@
     <bean id="filesystem" class="org.apache.james.container.spring.SpringFileSystem" />
 
     <!-- 
-      Scheduler
-     -->
-    <bean id="scheduler" class="java.util.concurrent.Executors" factory-method="newScheduledThreadPool">
-        <constructor-arg value="20"/>
-    </bean>
-
-    <!-- 
       Configuration Provider
      -->
     <bean class="org.apache.james.container.spring.lifecycle.CommonsConfigurableBeanPostProcessor">

Modified: james/server/trunk/container-spring/src/main/config/james/fetchmail.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/config/james/fetchmail.xml?rev=1049587&r1=1049586&r2=1049587&view=diff
==============================================================================
--- james/server/trunk/container-spring/src/main/config/james/fetchmail.xml (original)
+++ james/server/trunk/container-spring/src/main/config/james/fetchmail.xml Wed Dec 15 15:21:49 2010
@@ -21,4 +21,5 @@
 <!-- See http://james.apache.org/server/3/config.html for usage -->
 
 <fetchmail enabled="false">
+    <threads>5</threads>
 </fetchmail>        

Modified: james/server/trunk/fetchmail/src/main/java/org/apache/james/fetchmail/FetchScheduler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/fetchmail/src/main/java/org/apache/james/fetchmail/FetchScheduler.java?rev=1049587&r1=1049586&r2=1049587&view=diff
==============================================================================
--- james/server/trunk/fetchmail/src/main/java/org/apache/james/fetchmail/FetchScheduler.java (original)
+++ james/server/trunk/fetchmail/src/main/java/org/apache/james/fetchmail/FetchScheduler.java Wed Dec 15 15:21:49 2010
@@ -41,6 +41,7 @@ import org.apache.james.lifecycle.LogEna
 import org.apache.james.queue.api.MailQueue;
 import org.apache.james.queue.api.MailQueueFactory;
 import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.concurrent.JMXEnabledScheduledThreadPoolExecutor;
 
 /**
  *  A class to instantiate and schedule a set of mail fetching tasks
@@ -88,11 +89,6 @@ public class FetchScheduler implements F
         this.queueFactory = queueFactory;
     }
 
-    @Resource(name="scheduler")
-    public void setScheduledExecutorService(ScheduledExecutorService scheduler) {
-        this.scheduler = scheduler;
-    }
-
     
     @Resource(name="dnsservice")
     public void setDNSService(DNSService dns) {
@@ -105,10 +101,18 @@ public class FetchScheduler implements F
         this.urepos = urepos;
     }
     
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.lifecycle.LogEnabled#setLog(org.apache.commons.logging.Log)
+     */
     public final void setLog(Log logger) {
         this.logger = logger;
     }
     
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.lifecycle.Configurable#configure(org.apache.commons.configuration.HierarchicalConfiguration)
+     */
     public final void configure(HierarchicalConfiguration config) throws ConfigurationException{
         this.conf = config;
     }
@@ -121,6 +125,11 @@ public class FetchScheduler implements F
         enabled = conf.getBoolean("[@enabled]", false);
         if (enabled)
         {
+            int numThreads = conf.getInt("threads", 5);
+            String jmxName = conf.getString("jmxName","fetchmail");
+            String jmxPath = "org.apache.james:type=component,name=" + jmxName + ",sub-type=threadpool";
+            
+            scheduler = new JMXEnabledScheduledThreadPoolExecutor(numThreads, jmxPath, "scheduler");
             queue = queueFactory.getQueue(MailQueueFactory.SPOOL);
 
             List<HierarchicalConfiguration> fetchConfs = conf.configurationsAt("fetch");

Added: james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutor.java
URL: http://svn.apache.org/viewvc/james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutor.java?rev=1049587&view=auto
==============================================================================
--- james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutor.java (added)
+++ james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutor.java Wed Dec 15 15:21:49 2010
@@ -0,0 +1,165 @@
+/****************************************************************
+ * 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.util.concurrent;
+
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+public class JMXEnabledScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements JMXEnabledScheduledThreadPoolExecutorMBean{
+
+    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 JMXEnabledScheduledThreadPoolExecutor(int corePoolSize, NamedThreadFactory threadFactory, String jmxPath) {
+        super(corePoolSize, threadFactory);
+
+        this.jmxPath = jmxPath;
+        registerMBean();
+    }
+
+
+    public JMXEnabledScheduledThreadPoolExecutor(int corePoolSize, String jmxPath, String name) {
+        super(corePoolSize, new NamedThreadFactory(name));
+
+        this.jmxPath = jmxPath;
+        registerMBean();
+    }
+    
+    protected void beforeExecute(Thread t, Runnable r) {
+        super.beforeExecute(t, r);
+        inProgress.add(r);
+        startTime.set(System.currentTimeMillis());
+    }
+
+    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();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#
+     * getTotalTasks()
+     */
+    public synchronized int getTotalTasks() {
+        return totalTasks;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#
+     * getAverageTaskTime()
+     */
+    public synchronized double getAverageTaskTime() {
+        return (totalTasks == 0) ? 0 : totalTime / totalTasks;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#
+     * getActiveThreads()
+     */
+    public int getActiveThreads() {
+        return getPoolSize();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#
+     * getActiveTasks()
+     */
+    public int getActiveTasks() {
+        return getActiveCount();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.util.concurrent.JMXEnabledThreadPoolExecutorMBean#
+     * getQueuedTasks()
+     */
+    public int getQueuedTasks() {
+        return getQueue().size();
+    }
+}

Added: james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutorMBean.java
URL: http://svn.apache.org/viewvc/james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutorMBean.java?rev=1049587&view=auto
==============================================================================
--- james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutorMBean.java (added)
+++ james/server/trunk/util/src/main/java/org/apache/james/util/concurrent/JMXEnabledScheduledThreadPoolExecutorMBean.java Wed Dec 15 15:21:49 2010
@@ -0,0 +1,23 @@
+/****************************************************************
+ * 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.util.concurrent;
+
+public interface JMXEnabledScheduledThreadPoolExecutorMBean extends JMXEnabledThreadPoolExecutorMBean{
+
+}



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