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     */
017    package org.apache.camel.management.mbean;
018    
019    import java.util.concurrent.ThreadPoolExecutor;
020    import java.util.concurrent.TimeUnit;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.api.management.ManagedResource;
024    import org.apache.camel.api.management.mbean.ManagedThreadPoolMBean;
025    import org.apache.camel.spi.ManagementStrategy;
026    
027    /**
028     * @version 
029     */
030    @ManagedResource(description = "Managed ThreadPool")
031    public class ManagedThreadPool implements ManagedThreadPoolMBean {
032    
033        private final CamelContext camelContext;
034        private final ThreadPoolExecutor threadPool;
035        private final String id;
036        private final String sourceId;
037        private final String routeId;
038        private final String threadPoolProfileId;
039    
040        public ManagedThreadPool(CamelContext camelContext, ThreadPoolExecutor threadPool, String id,
041                                 String sourceId, String routeId, String threadPoolProfileId) {
042            this.camelContext = camelContext;
043            this.threadPool = threadPool;
044            this.sourceId = sourceId;
045            this.id = id;
046            this.routeId = routeId;
047            this.threadPoolProfileId = threadPoolProfileId;
048        }
049    
050        public void init(ManagementStrategy strategy) {
051            // do nothing
052        }
053    
054        public CamelContext getContext() {
055            return camelContext;
056        }
057    
058        public ThreadPoolExecutor getThreadPool() {
059            return threadPool;
060        }
061    
062        public String getId() {
063            return id;
064        }
065    
066        public String getSourceId() {
067            return sourceId;
068        }
069    
070        public String getRouteId() {
071            return routeId;
072        }
073    
074        public String getThreadPoolProfileId() {
075            return threadPoolProfileId;
076        }
077    
078        public int getCorePoolSize() {
079            return threadPool.getCorePoolSize();
080        }
081    
082        public void setCorePoolSize(int corePoolSize) {
083            threadPool.setCorePoolSize(corePoolSize);
084        }
085    
086        public int getPoolSize() {
087            return threadPool.getPoolSize();
088        }
089    
090        public int getMaximumPoolSize() {
091            return threadPool.getMaximumPoolSize();
092        }
093    
094        public void setMaximumPoolSize(int maximumPoolSize) {
095            threadPool.setMaximumPoolSize(maximumPoolSize);
096        }
097    
098        public int getLargestPoolSize() {
099            return threadPool.getLargestPoolSize();
100        }
101    
102        public int getActiveCount() {
103            return threadPool.getActiveCount();
104        }
105    
106        public long getTaskCount() {
107            return threadPool.getTaskCount();
108        }
109    
110        public long getCompletedTaskCount() {
111            return threadPool.getCompletedTaskCount();
112        }
113    
114        public long getTaskQueueSize() {
115            if (threadPool.getQueue() != null) {
116                return threadPool.getQueue().size();
117            } else {
118                return 0;
119            }
120        }
121    
122        public boolean isTaskQueueEmpty() {
123            if (threadPool.getQueue() != null) {
124                return threadPool.getQueue().isEmpty();
125            } else {
126                return true;
127            }
128        }
129    
130        public long getKeepAliveTime() {
131            return threadPool.getKeepAliveTime(TimeUnit.SECONDS);
132        }
133    
134        public void setKeepAliveTime(long keepAliveTimeInSeconds) {
135            threadPool.setKeepAliveTime(keepAliveTimeInSeconds, TimeUnit.SECONDS);
136        }
137    
138        public boolean isShutdown() {
139            return threadPool.isShutdown();
140        }
141    
142        public void purge() {
143            threadPool.purge();
144        }
145    
146        public int getTaskQueueRemainingCapacity() {
147            if (threadPool.getQueue() != null) {
148                return threadPool.getQueue().remainingCapacity();
149            } else {
150                // no queue found, so no capacity
151                return 0;
152            }
153        }
154    
155    }