mirror of https://github.com/elunez/eladmin
parent
64a3ff7b26
commit
255a3254ce
|
@ -1,4 +1,4 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
|
|
@ -43,8 +43,8 @@ public class AsyncTaskExecutePool implements AsyncConfigurer {
|
|||
executor.setQueueCapacity(AsyncTaskProperties.queueCapacity);
|
||||
//活跃时间
|
||||
executor.setKeepAliveSeconds(AsyncTaskProperties.keepAliveSeconds);
|
||||
//线程名字前缀
|
||||
executor.setThreadNamePrefix("el-async-");
|
||||
//线程工厂
|
||||
executor.setThreadFactory(new TheadFactoryName("el-async"));
|
||||
// setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
|
||||
// CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
*/
|
||||
package me.zhengjie.config.thread;
|
||||
|
||||
import me.zhengjie.utils.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
@ -33,24 +33,25 @@ public class TheadFactoryName implements ThreadFactory {
|
|||
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
||||
private final String namePrefix;
|
||||
|
||||
private final static String DEF_NAME = "el-pool-";
|
||||
|
||||
public TheadFactoryName() {
|
||||
this("el-pool");
|
||||
this(DEF_NAME);
|
||||
}
|
||||
|
||||
private TheadFactoryName(String name){
|
||||
public TheadFactoryName(String name){
|
||||
SecurityManager s = System.getSecurityManager();
|
||||
group = (s != null) ? s.getThreadGroup() :
|
||||
Thread.currentThread().getThreadGroup();
|
||||
//此时namePrefix就是 name + 第几个用这个工厂创建线程池的
|
||||
this.namePrefix = name +
|
||||
POOL_NUMBER.getAndIncrement();
|
||||
this.namePrefix = (StringUtils.isNotBlank(name) ? name : DEF_NAME) + "-" + POOL_NUMBER.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
//此时线程的名字 就是 namePrefix + -thread- + 这个线程池中第几个执行的线程
|
||||
//此时线程的名字 就是 namePrefix + -exec- + 这个线程池中第几个执行的线程
|
||||
Thread t = new Thread(group, r,
|
||||
namePrefix + "-thread-"+threadNumber.getAndIncrement(),
|
||||
namePrefix + "-exec-"+threadNumber.getAndIncrement(),
|
||||
0);
|
||||
if (t.isDaemon()) {
|
||||
t.setDaemon(false);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package me.zhengjie.config.thread;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -26,14 +27,20 @@ import java.util.concurrent.TimeUnit;
|
|||
*/
|
||||
public class ThreadPoolExecutorUtil {
|
||||
|
||||
public static ThreadPoolExecutor getPoll(){
|
||||
public static ExecutorService getPoll(){
|
||||
return getPoll(null);
|
||||
}
|
||||
|
||||
public static ExecutorService getPoll(String threadName){
|
||||
return new ThreadPoolExecutor(
|
||||
AsyncTaskProperties.corePoolSize,
|
||||
AsyncTaskProperties.maxPoolSize,
|
||||
AsyncTaskProperties.keepAliveSeconds,
|
||||
TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<>(AsyncTaskProperties.queueCapacity),
|
||||
new TheadFactoryName()
|
||||
new TheadFactoryName(threadName),
|
||||
// 队列与线程池中线程都满了时使用调用者所在的线程来执行
|
||||
new ThreadPoolExecutor.CallerRunsPolicy()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import cn.hutool.extra.template.Template;
|
|||
import cn.hutool.extra.template.TemplateConfig;
|
||||
import cn.hutool.extra.template.TemplateEngine;
|
||||
import cn.hutool.extra.template.TemplateUtil;
|
||||
import me.zhengjie.config.thread.ThreadPoolExecutorUtil;
|
||||
import me.zhengjie.domain.vo.EmailVo;
|
||||
import me.zhengjie.modules.quartz.domain.QuartzJob;
|
||||
import me.zhengjie.modules.quartz.domain.QuartzLog;
|
||||
|
@ -32,7 +33,6 @@ import me.zhengjie.utils.ThrowableUtil;
|
|||
import org.quartz.JobExecutionContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
@ -42,15 +42,15 @@ import java.util.concurrent.*;
|
|||
* @author /
|
||||
* @date 2019-01-07
|
||||
*/
|
||||
@Async
|
||||
public class ExecutionJob extends QuartzJobBean {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
// 此处仅供参考,可根据任务执行情况自定义线程池参数
|
||||
private final static ExecutorService executor = ThreadPoolExecutorUtil.getPoll("el-quartz-job");
|
||||
|
||||
@Override
|
||||
public void executeInternal(JobExecutionContext context) {
|
||||
// 创建单个线程
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
// 获取任务
|
||||
QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY);
|
||||
// 获取spring bean
|
||||
|
@ -112,7 +112,6 @@ public class ExecutionJob extends QuartzJobBean {
|
|||
}
|
||||
} finally {
|
||||
quartzLogRepository.save(log);
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue