refactor: 优化线程池配置及日志级别调整

This commit is contained in:
Jie Zheng 2025-01-13 15:56:31 +08:00
parent eb17ffdfc7
commit 1136bb6172
13 changed files with 69 additions and 31 deletions

View File

@ -1,9 +1,26 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* 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.
*/
package me.zhengjie.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -13,8 +30,9 @@ import java.util.concurrent.atomic.AtomicInteger;
* @description
* @date 2023-06-08
**/
@EnableAsync
@Configuration
public class AsyncExecutor {
public class AsyncExecutor implements AsyncConfigurer {
public static int corePoolSize;
@ -48,9 +66,8 @@ public class AsyncExecutor {
* 自定义线程池用法 @Async
* @return Executor
*/
@Bean
@Primary
public Executor elAsync() {
@Override
public Executor getAsyncExecutor() {
// 自定义工厂
ThreadFactory factory = r -> new Thread(r, "el-async-" + new AtomicInteger(1).getAndIncrement());
// 自定义线程池
@ -60,16 +77,19 @@ public class AsyncExecutor {
}
/**
* 自定义线程池用法 @Async("otherAsync")
* @return Executor
* 自定义线程池用法注入到类中使用
* private ThreadPoolTaskExecutor taskExecutor;
* @return ThreadPoolTaskExecutor
*/
@Bean
public Executor otherAsync() {
// 自定义工厂
ThreadFactory factory = r -> new Thread(r, "tpl-other-" + new AtomicInteger(1).getAndIncrement());
// 自定义线程池
return new ThreadPoolExecutor(2, 4, keepAliveSeconds,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(20), factory,
new ThreadPoolExecutor.CallerRunsPolicy());
@Bean("taskAsync")
public ThreadPoolTaskExecutor taskAsync() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(20);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("el-task-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}

View File

@ -77,6 +77,8 @@ public class RedisConfiguration {
ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.system.domain");
// 模块内的 Dto
ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.security.service.dto");
// 分页返回数据
ParserConfig.getGlobalInstance().addAccept("me.zhengjie.utils.PageResult");
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());

View File

@ -45,7 +45,7 @@ public class ColUtil {
*/
public static PropertiesConfiguration getConfig() {
try {
return new PropertiesConfiguration("generator.properties");
return new PropertiesConfiguration("gen.properties");
} catch (ConfigurationException e) {
log.error(e.getMessage(), e);
}

View File

@ -21,11 +21,9 @@ import me.zhengjie.domain.SysLog;
import me.zhengjie.domain.vo.SysLogQueryCriteria;
import me.zhengjie.utils.PageResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.scheduling.annotation.Async;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @author Zheng Jie
@ -65,7 +63,6 @@ public interface SysLogService extends IService<SysLog>{
* @param joinPoint /
* @param sysLog 日志实体
*/
@Async
void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, SysLog sysLog);
/**

View File

@ -28,6 +28,7 @@ import me.zhengjie.domain.vo.SysLogQueryCriteria;
import me.zhengjie.utils.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
@ -63,6 +64,7 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
return PageUtil.toPage(sysLogMapper.queryAllByUser(criteria, page));
}
@Async
@Override
@Transactional(rollbackFor = Exception.class)
public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, SysLog sysLog) {

View File

@ -23,7 +23,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;
@ -32,7 +31,6 @@ import org.springframework.web.bind.annotation.RestController;
* @date 2018/11/15 9:20:19
*/
@Slf4j
@EnableAsync
@RestController
@Api(hidden = true)
@SpringBootApplication

View File

@ -30,7 +30,6 @@ import me.zhengjie.modules.quartz.domain.vo.QuartzJobQueryCriteria;
import me.zhengjie.modules.quartz.utils.QuartzManage;
import me.zhengjie.utils.*;
import org.quartz.CronExpression;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
@ -124,7 +123,6 @@ public class QuartzJobServiceImpl extends ServiceImpl<QuartzJobMapper, QuartzJob
}
}
@Async
@Override
@Transactional(rollbackFor = Exception.class)
public void executionSubJob(String[] tasks) throws InterruptedException {

View File

@ -32,7 +32,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.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.*;
@ -43,13 +42,12 @@ 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 ThreadPoolTaskExecutor executor = SpringBeanHolder.getBean("elAsync");
private final ThreadPoolTaskExecutor executor = SpringBeanHolder.getBean("taskAsync");
@Override
public void executeInternal(JobExecutionContext context) {
@ -81,7 +79,7 @@ public class ExecutionJob extends QuartzJobBean {
}
// 任务状态
log.setIsSuccess(true);
logger.info("任务执行成功,任务名称:" + quartzJob.getJobName() + ", 执行时间:" + times + "毫秒");
logger.info("任务执行成功,任务名称:{}, 执行时间:{}毫秒", quartzJob.getJobName(), times);
// 判断是否存在子任务
if(StringUtils.isNotBlank(quartzJob.getSubTask())){
String[] tasks = quartzJob.getSubTask().split("[,]");
@ -92,7 +90,7 @@ public class ExecutionJob extends QuartzJobBean {
if(StringUtils.isNotBlank(uuid)) {
redisUtils.set(uuid, false);
}
logger.error("任务执行失败,任务名称:" + quartzJob.getJobName());
logger.error("任务执行失败,任务名称:{}", quartzJob.getJobName());
long times = System.currentTimeMillis() - startTime;
log.setTime(times);
// 任务状态 0成功 1失败

View File

@ -5,4 +5,4 @@
| __| | | (_| | (_| | | | | | | | | | |
\___|_| \__,_|\__,_|_| |_| |_|_|_| |_|
:: Spring Boot :: (v2.6.4)
Spring Boot Version: (${spring-boot.version})EL-ADMIN version: (1.1)

View File

@ -1,8 +1,15 @@
server:
port: 8000
compression:
http2:
# 启用 HTTP/2 支持,提升传输效率
enabled: true
mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
compression:
# 启用 GZIP 压缩,减少传输数据量
enabled: true
# 需要压缩的 MIME 类型
mime-types: text/html, text/xml, text/plain, application/json
# 最小压缩响应大小(字节)
min-response-size: 1024
mybatis-plus:
configuration:

View File

@ -16,4 +16,8 @@
<root level="info">
<appender-ref ref="console" />
</root>
<!--部分日志调整为ERROR-->
<logger name="io.undertow.websockets.jsr" level="ERROR"/>
<logger name="io.netty.resolver.dns.DnsServerAddressStreamProviders" level="ERROR"/>
</configuration>

View File

@ -43,6 +43,18 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--Spring boot 测试-->