| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.alibaba.dubbo.rpc.protocol.dubbo.status; |
| |
| import com.alibaba.dubbo.common.Constants; |
| import com.alibaba.dubbo.common.extension.Activate; |
| import com.alibaba.dubbo.common.extension.ExtensionLoader; |
| import com.alibaba.dubbo.common.status.Status; |
| import com.alibaba.dubbo.common.status.StatusChecker; |
| import com.alibaba.dubbo.common.store.DataStore; |
| |
| import java.util.Map; |
| import java.util.concurrent.ExecutorService; |
| import java.util.concurrent.ThreadPoolExecutor; |
| |
| /** |
| * ThreadPoolStatusChecker |
| */ |
| @Activate |
| public class ThreadPoolStatusChecker implements StatusChecker { |
| |
| @Override |
| public Status check() { |
| DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension(); |
| Map<String, Object> executors = dataStore.get(Constants.EXECUTOR_SERVICE_COMPONENT_KEY); |
| |
| StringBuilder msg = new StringBuilder(); |
| Status.Level level = Status.Level.OK; |
| for (Map.Entry<String, Object> entry : executors.entrySet()) { |
| String port = entry.getKey(); |
| ExecutorService executor = (ExecutorService) entry.getValue(); |
| |
| if (executor != null && executor instanceof ThreadPoolExecutor) { |
| ThreadPoolExecutor tp = (ThreadPoolExecutor) executor; |
| boolean ok = tp.getActiveCount() < tp.getMaximumPoolSize() - 1; |
| Status.Level lvl = Status.Level.OK; |
| if (!ok) { |
| level = Status.Level.WARN; |
| lvl = Status.Level.WARN; |
| } |
| |
| if (msg.length() > 0) { |
| msg.append(";"); |
| } |
| msg.append("Pool status:" + lvl |
| + ", max:" + tp.getMaximumPoolSize() |
| + ", core:" + tp.getCorePoolSize() |
| + ", largest:" + tp.getLargestPoolSize() |
| + ", active:" + tp.getActiveCount() |
| + ", task:" + tp.getTaskCount() |
| + ", service port: " + port); |
| } |
| } |
| return msg.length() == 0 ? new Status(Status.Level.UNKNOWN) : new Status(level, msg.toString()); |
| } |
| |
| } |