/**
 * 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.cassandra.concurrent;

import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanServer;
import javax.management.ObjectName;

/**
 * This is a wrapper class for the <i>ScheduledThreadPoolExecutor</i>. It provides an implementation
 * for the <i>afterExecute()</i> found in the <i>ThreadPoolExecutor</i> class to log any unexpected 
 * Runtime Exceptions.
 */

public class JMXEnabledThreadPoolExecutor extends DebuggableThreadPoolExecutor implements JMXEnabledThreadPoolExecutorMBean
{
    private final String mbeanName;

    public JMXEnabledThreadPoolExecutor(String threadPoolName)
    {
        this(1, 1, Integer.MAX_VALUE, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory(threadPoolName));
    }

    public JMXEnabledThreadPoolExecutor(int corePoolSize,
                                        int maximumPoolSize,
                                        long keepAliveTime,
                                        TimeUnit unit,
                                        BlockingQueue<Runnable> workQueue,
                                        NamedThreadFactory threadFactory)
    {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
        super.prestartAllCoreThreads();

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        mbeanName = "org.apache.cassandra.concurrent:type=" + threadFactory.id;
        try
        {
            mbs.registerMBean(this, new ObjectName(mbeanName));
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    private void unregisterMBean()
    {
        try
        {
            ManagementFactory.getPlatformMBeanServer().unregisterMBean(new ObjectName(mbeanName));
        }
        catch (Exception ex)
        {
            // don't let it get in the way, but notify.
            logger.error(ex.getMessage(), ex);
        }
    }

    @Override
    public void shutdown()
    {
        unregisterMBean();
        super.shutdown();
    }

    @Override
    public List<Runnable> shutdownNow()
    {
        unregisterMBean();
        return super.shutdownNow();
    }

    /**
     * Get the number of completed tasks
     */
    public long getCompletedTasks()
    {
        return getCompletedTaskCount();
    }

    /**
     * Get the number of tasks waiting to be executed
     */
    public long getPendingTasks()
    {
        return getTaskCount() - getCompletedTaskCount();
    }
}

@@ -1071,55 +1071,9 @@
 ent.
-MBeanServer;%0Aimport javax.management.ObjectName
+*
 ;%0A%0A/
@@ -2743,17 +2743,16 @@
 eption e
-x
 )%0A      
@@ -2771,147 +2771,275 @@
     
-// don't let it get in the way, but notify.%0A            logger.error(ex.getMessage(), ex);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void s
+throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public synchronized void shutdown()%0A    %7B%0A        // synchronized, because there is no way to access super.mainLock, which would be%0A        // the preferred way to make this threadsafe%0A        if (!isS
 hutd
@@ -3035,39 +3035,48 @@
 f (!isShutdown()
-%0A
+)%0A   
     
+ 
 %7B%0A
+    
         unregist
@@ -3078,32 +3078,42 @@
 egisterMBean();%0A
+        %7D%0A
         super.sh
@@ -3154,16 +3154,29 @@
  public 
+synchronized 
 List%3CRun
@@ -3194,32 +3194,216 @@
 downNow()%0A    %7B%0A
+        // synchronized, because there is no way to access super.mainLock, which would be%0A        // the preferred way to make this threadsafe%0A        if (!isShutdown())%0A        %7B%0A    
         unregist
@@ -3405,32 +3405,42 @@
 egisterMBean();%0A
+        %7D%0A
         return s

fix allowing unregisterMBean to be called more than once. patch by jbellis