/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed 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.
*
* Contributors:
* ohun@live.cn (夜色)
*/
package com.mpush.tools.thread.pool;
import com.mpush.tools.common.JVMUtil;
import com.mpush.tools.config.CC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_ABORT;
import static com.mpush.tools.thread.pool.ThreadPoolConfig.REJECTED_POLICY_CALLER_RUNS;
public final class DumpThreadRejectedHandler implements RejectedExecutionHandler {
private final static Logger LOGGER = LoggerFactory.getLogger(DumpThreadRejectedHandler.class);
private volatile boolean dumping = false;
private static final String DUMP_DIR = CC.mp.monitor.dump_dir;
private final ThreadPoolConfig poolConfig;
private final int rejectedPolicy;
public DumpThreadRejectedHandler(ThreadPoolConfig poolConfig) {
this.poolConfig = poolConfig;
this.rejectedPolicy = poolConfig.getRejectedPolicy();
}
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
LOGGER.warn("one task rejected, poolConfig={}, poolInfo={}", poolConfig, ThreadPoolManager.getPoolInfo(e));
if (!dumping) {
dumping = true;
dumpJVMInfo();
}
if (rejectedPolicy == REJECTED_POLICY_ABORT) {
throw new RejectedExecutionException("one task rejected, pool=" + poolConfig.getName());
} else if (rejectedPolicy == REJECTED_POLICY_CALLER_RUNS) {
if (!e.isShutdown()) {
r.run();
}
}
}
private void dumpJVMInfo() {
LOGGER.info("start dump jvm info");
JVMUtil.dumpJstack(DUMP_DIR + "/" + poolConfig.getName());
LOGGER.info("end dump jvm info");
}
}