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