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@ManagedResource(description = "Managed ThreadPool") 028public class ManagedThreadPool implements ManagedThreadPoolMBean { 029 030 private final CamelContext camelContext; 031 private final ThreadPoolExecutor threadPool; 032 private final String id; 033 private final String sourceId; 034 private final String routeId; 035 private final String threadPoolProfileId; 036 037 public ManagedThreadPool(CamelContext camelContext, ThreadPoolExecutor threadPool, String id, 038 String sourceId, String routeId, String threadPoolProfileId) { 039 this.camelContext = camelContext; 040 this.threadPool = threadPool; 041 this.sourceId = sourceId; 042 this.id = id; 043 this.routeId = routeId; 044 this.threadPoolProfileId = threadPoolProfileId; 045 } 046 047 public void init(ManagementStrategy strategy) { 048 // do nothing 049 } 050 051 public CamelContext getContext() { 052 return camelContext; 053 } 054 055 public ThreadPoolExecutor getThreadPool() { 056 return threadPool; 057 } 058 059 @Override 060 public String getCamelId() { 061 return camelContext.getName(); 062 } 063 064 @Override 065 public String getCamelManagementName() { 066 return camelContext.getManagementName(); 067 } 068 069 @Override 070 public String getId() { 071 return id; 072 } 073 074 @Override 075 public String getSourceId() { 076 return sourceId; 077 } 078 079 @Override 080 public String getRouteId() { 081 return routeId; 082 } 083 084 @Override 085 public String getThreadPoolProfileId() { 086 return threadPoolProfileId; 087 } 088 089 @Override 090 public int getCorePoolSize() { 091 return threadPool.getCorePoolSize(); 092 } 093 094 @Override 095 public void setCorePoolSize(int corePoolSize) { 096 threadPool.setCorePoolSize(corePoolSize); 097 } 098 099 @Override 100 public int getPoolSize() { 101 return threadPool.getPoolSize(); 102 } 103 104 @Override 105 public int getMaximumPoolSize() { 106 return threadPool.getMaximumPoolSize(); 107 } 108 109 @Override 110 public void setMaximumPoolSize(int maximumPoolSize) { 111 threadPool.setMaximumPoolSize(maximumPoolSize); 112 } 113 114 @Override 115 public int getLargestPoolSize() { 116 return threadPool.getLargestPoolSize(); 117 } 118 119 @Override 120 public int getActiveCount() { 121 return threadPool.getActiveCount(); 122 } 123 124 @Override 125 public long getTaskCount() { 126 return threadPool.getTaskCount(); 127 } 128 129 @Override 130 public long getCompletedTaskCount() { 131 return threadPool.getCompletedTaskCount(); 132 } 133 134 @Override 135 public long getTaskQueueSize() { 136 if (threadPool.getQueue() != null) { 137 return threadPool.getQueue().size(); 138 } else { 139 return 0; 140 } 141 } 142 143 @Override 144 public boolean isTaskQueueEmpty() { 145 if (threadPool.getQueue() != null) { 146 return threadPool.getQueue().isEmpty(); 147 } else { 148 return true; 149 } 150 } 151 152 @Override 153 public long getKeepAliveTime() { 154 return threadPool.getKeepAliveTime(TimeUnit.SECONDS); 155 } 156 157 @Override 158 public void setKeepAliveTime(long keepAliveTimeInSeconds) { 159 threadPool.setKeepAliveTime(keepAliveTimeInSeconds, TimeUnit.SECONDS); 160 } 161 162 @Override 163 public boolean isAllowCoreThreadTimeout() { 164 return threadPool.allowsCoreThreadTimeOut(); 165 } 166 167 @Override 168 public void setAllowCoreThreadTimeout(boolean allowCoreThreadTimeout) { 169 threadPool.allowCoreThreadTimeOut(allowCoreThreadTimeout); 170 } 171 172 @Override 173 public boolean isShutdown() { 174 return threadPool.isShutdown(); 175 } 176 177 @Override 178 public void purge() { 179 threadPool.purge(); 180 } 181 182 @Override 183 public int getTaskQueueRemainingCapacity() { 184 if (threadPool.getQueue() != null) { 185 return threadPool.getQueue().remainingCapacity(); 186 } else { 187 // no queue found, so no capacity 188 return 0; 189 } 190 } 191 192}