001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.management.mbean;
018
019import java.util.concurrent.BlockingQueue;
020import java.util.concurrent.ThreadPoolExecutor;
021import java.util.concurrent.TimeUnit;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.api.management.ManagedResource;
025import org.apache.camel.api.management.mbean.ManagedThreadsMBean;
026import org.apache.camel.model.ProcessorDefinition;
027import org.apache.camel.processor.ThreadsProcessor;
028
029/**
030 * @version 
031 */
032@ManagedResource(description = "Managed Threads")
033public class ManagedThreads extends ManagedProcessor implements ManagedThreadsMBean {
034    private final ThreadsProcessor processor;
035
036    public ManagedThreads(CamelContext context, ThreadsProcessor processor, ProcessorDefinition<?> definition) {
037        super(context, processor, definition);
038        this.processor = processor;
039    }
040
041    @Override
042    public Boolean isCallerRunsWhenRejected() {
043        return processor.isCallerRunsWhenRejected();
044    }
045
046    @Override
047    public String getRejectedPolicy() {
048        if (processor.getRejectedPolicy() != null) {
049            return processor.getRejectedPolicy().name();
050        } else {
051            return null;
052        }
053    }
054
055    @Override
056    public int getCorePoolSize() {
057        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
058            return ((ThreadPoolExecutor) processor.getExecutorService()).getCorePoolSize();
059        } else {
060            return 0;
061        }
062    }
063
064    @Override
065    public int getPoolSize() {
066        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
067            return ((ThreadPoolExecutor) processor.getExecutorService()).getPoolSize();
068        } else {
069            return 0;
070        }
071    }
072
073    @Override
074    public int getMaximumPoolSize() {
075        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
076            return ((ThreadPoolExecutor) processor.getExecutorService()).getMaximumPoolSize();
077        } else {
078            return 0;
079        }
080    }
081
082    @Override
083    public int getLargestPoolSize() {
084        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
085            return ((ThreadPoolExecutor) processor.getExecutorService()).getLargestPoolSize();
086        } else {
087            return 0;
088        }
089    }
090
091    @Override
092    public int getActiveCount() {
093        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
094            return ((ThreadPoolExecutor) processor.getExecutorService()).getActiveCount();
095        } else {
096            return 0;
097        }
098    }
099
100    @Override
101    public long getTaskCount() {
102        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
103            return ((ThreadPoolExecutor) processor.getExecutorService()).getTaskCount();
104        } else {
105            return 0;
106        }
107    }
108
109    @Override
110    public long getCompletedTaskCount() {
111        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
112            return ((ThreadPoolExecutor) processor.getExecutorService()).getCompletedTaskCount();
113        } else {
114            return 0;
115        }
116    }
117
118    @Override
119    public long getTaskQueueSize() {
120        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
121            BlockingQueue queue = ((ThreadPoolExecutor) processor.getExecutorService()).getQueue();
122            return queue != null ? queue.size() : 0;
123        } else {
124            return 0;
125        }
126    }
127
128    @Override
129    public long getKeepAliveTime() {
130        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
131            return ((ThreadPoolExecutor) processor.getExecutorService()).getKeepAliveTime(TimeUnit.SECONDS);
132        } else {
133            return 0;
134        }
135    }
136
137    @Override
138    public boolean isAllowCoreThreadTimeout() {
139        if (processor.getExecutorService() instanceof ThreadPoolExecutor) {
140            return ((ThreadPoolExecutor) processor.getExecutorService()).allowsCoreThreadTimeOut();
141        } else {
142            return false;
143        }
144    }
145
146}