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.ThreadPoolExecutor; 020import java.util.concurrent.TimeUnit; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.api.management.ManagedResource; 024import org.apache.camel.api.management.mbean.ManagedThreadPoolMBean; 025import org.apache.camel.spi.ManagementStrategy; 026 027/** 028 * @version 029 */ 030@ManagedResource(description = "Managed ThreadPool") 031public 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 getCamelId() { 063 return camelContext.getName(); 064 } 065 066 public String getCamelManagementName() { 067 return camelContext.getManagementName(); 068 } 069 070 public String getId() { 071 return id; 072 } 073 074 public String getSourceId() { 075 return sourceId; 076 } 077 078 public String getRouteId() { 079 return routeId; 080 } 081 082 public String getThreadPoolProfileId() { 083 return threadPoolProfileId; 084 } 085 086 public int getCorePoolSize() { 087 return threadPool.getCorePoolSize(); 088 } 089 090 public void setCorePoolSize(int corePoolSize) { 091 threadPool.setCorePoolSize(corePoolSize); 092 } 093 094 public int getPoolSize() { 095 return threadPool.getPoolSize(); 096 } 097 098 public int getMaximumPoolSize() { 099 return threadPool.getMaximumPoolSize(); 100 } 101 102 public void setMaximumPoolSize(int maximumPoolSize) { 103 threadPool.setMaximumPoolSize(maximumPoolSize); 104 } 105 106 public int getLargestPoolSize() { 107 return threadPool.getLargestPoolSize(); 108 } 109 110 public int getActiveCount() { 111 return threadPool.getActiveCount(); 112 } 113 114 public long getTaskCount() { 115 return threadPool.getTaskCount(); 116 } 117 118 public long getCompletedTaskCount() { 119 return threadPool.getCompletedTaskCount(); 120 } 121 122 public long getTaskQueueSize() { 123 if (threadPool.getQueue() != null) { 124 return threadPool.getQueue().size(); 125 } else { 126 return 0; 127 } 128 } 129 130 public boolean isTaskQueueEmpty() { 131 if (threadPool.getQueue() != null) { 132 return threadPool.getQueue().isEmpty(); 133 } else { 134 return true; 135 } 136 } 137 138 public long getKeepAliveTime() { 139 return threadPool.getKeepAliveTime(TimeUnit.SECONDS); 140 } 141 142 public void setKeepAliveTime(long keepAliveTimeInSeconds) { 143 threadPool.setKeepAliveTime(keepAliveTimeInSeconds, TimeUnit.SECONDS); 144 } 145 146 public boolean isAllowCoreThreadTimeout() { 147 return threadPool.allowsCoreThreadTimeOut(); 148 } 149 150 public void setAllowCoreThreadTimeout(boolean allowCoreThreadTimeout) { 151 threadPool.allowCoreThreadTimeOut(allowCoreThreadTimeout); 152 } 153 154 public boolean isShutdown() { 155 return threadPool.isShutdown(); 156 } 157 158 public void purge() { 159 threadPool.purge(); 160 } 161 162 public int getTaskQueueRemainingCapacity() { 163 if (threadPool.getQueue() != null) { 164 return threadPool.getQueue().remainingCapacity(); 165 } else { 166 // no queue found, so no capacity 167 return 0; 168 } 169 } 170 171}