Skip to content

Commit

Permalink
代码优化,自定义线程池优化
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Jun 8, 2023
1 parent 64e608b commit 21b30b1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 175 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.zhengjie.config.thread;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
* 创建自定义的线程池
* @author Zheng Jie
* @description
* @date 2023-06-08
**/
@Configuration
public class CustomExecutorConfig {

/**
* 自定义线程池,用法 @Async
* @return Executor
*/
@Bean
@Primary
public Executor elAsync() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(AsyncTaskProperties.corePoolSize);
executor.setMaxPoolSize(AsyncTaskProperties.maxPoolSize);
executor.setQueueCapacity(AsyncTaskProperties.queueCapacity);
executor.setThreadNamePrefix("el-async-");
executor.setKeepAliveSeconds(AsyncTaskProperties.keepAliveSeconds);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}

/**
* 自定义线程池,用法 @Async("otherAsync")
* @return Executor
*/
@Bean
public Executor otherAsync() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(15);
executor.setQueueCapacity(50);
executor.setKeepAliveSeconds(AsyncTaskProperties.keepAliveSeconds);
executor.setThreadNamePrefix("el-task-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand All @@ -33,12 +32,13 @@
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.*;
import java.util.concurrent.*;

/**
* 参考人人开源,https://gitee.com/renrenio/renren-security
* 参考人人开源,<a href="https://gitee.com/renrenio/renren-security">...</a>
* @author /
* @date 2019-01-07
*/
Expand All @@ -47,7 +47,7 @@ public class ExecutionJob extends QuartzJobBean {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

// 此处仅供参考,可根据任务执行情况自定义线程池参数
private final static ExecutorService executor = ThreadPoolExecutorUtil.getPoll("el-quartz-job");
private final ThreadPoolTaskExecutor executor = SpringContextHolder.getBean("elAsync");

@Override
public void executeInternal(JobExecutionContext context) {
Expand Down

0 comments on commit 21b30b1

Please sign in to comment.