mirror of https://gitee.com/y_project/RuoYi.git
线程异常打印方法移入工具类
parent
00a661e325
commit
e26b65459e
|
@ -1,17 +1,13 @@
|
||||||
package com.ruoyi.common.config.thread;
|
package com.ruoyi.common.config.thread;
|
||||||
|
|
||||||
import java.util.concurrent.CancellationException;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
import com.ruoyi.common.utils.Threads;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 线程池配置
|
* 线程池配置
|
||||||
|
@ -21,8 +17,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class ThreadPoolConfig
|
public class ThreadPoolConfig
|
||||||
{
|
{
|
||||||
private static final Logger log = LoggerFactory.getLogger(ThreadPoolConfig.class);
|
|
||||||
|
|
||||||
// 核心线程池大小
|
// 核心线程池大小
|
||||||
private int corePoolSize = 50;
|
private int corePoolSize = 50;
|
||||||
|
|
||||||
|
@ -61,39 +55,8 @@ public class ThreadPoolConfig
|
||||||
protected void afterExecute(Runnable r, Throwable t)
|
protected void afterExecute(Runnable r, Throwable t)
|
||||||
{
|
{
|
||||||
super.afterExecute(r, t);
|
super.afterExecute(r, t);
|
||||||
printException(r, t);
|
Threads.printException(r, t);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printException(Runnable r, Throwable t)
|
|
||||||
{
|
|
||||||
if (t == null && r instanceof Future<?>)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Future<?> future = (Future<?>) r;
|
|
||||||
if (future.isDone())
|
|
||||||
{
|
|
||||||
future.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (CancellationException ce)
|
|
||||||
{
|
|
||||||
t = ce;
|
|
||||||
}
|
|
||||||
catch (ExecutionException ee)
|
|
||||||
{
|
|
||||||
t = ee.getCause();
|
|
||||||
}
|
|
||||||
catch (InterruptedException ie)
|
|
||||||
{
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (t != null)
|
|
||||||
{
|
|
||||||
log.error(t.getMessage(), t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package com.ruoyi.common.utils;
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
|
import java.util.concurrent.CancellationException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -12,7 +15,7 @@ import org.slf4j.LoggerFactory;
|
||||||
*/
|
*/
|
||||||
public class Threads
|
public class Threads
|
||||||
{
|
{
|
||||||
private static final Logger logger = LoggerFactory.getLogger("sys-user");
|
private static final Logger logger = LoggerFactory.getLogger(Threads.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sleep等待,单位为毫秒
|
* sleep等待,单位为毫秒
|
||||||
|
@ -59,4 +62,38 @@ public class Threads
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印线程异常信息
|
||||||
|
*/
|
||||||
|
public static void printException(Runnable r, Throwable t)
|
||||||
|
{
|
||||||
|
if (t == null && r instanceof Future<?>)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Future<?> future = (Future<?>) r;
|
||||||
|
if (future.isDone())
|
||||||
|
{
|
||||||
|
future.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (CancellationException ce)
|
||||||
|
{
|
||||||
|
t = ce;
|
||||||
|
}
|
||||||
|
catch (ExecutionException ee)
|
||||||
|
{
|
||||||
|
t = ee.getCause();
|
||||||
|
}
|
||||||
|
catch (InterruptedException ie)
|
||||||
|
{
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (t != null)
|
||||||
|
{
|
||||||
|
logger.error(t.getMessage(), t);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue