1.修改多线程配置,修复线程不复用的问题

This commit is contained in:
2025-07-08 15:19:59 +08:00
parent 12f4059ba0
commit 7aceb760a2
7 changed files with 44 additions and 15 deletions

View File

@@ -0,0 +1,23 @@
package com.yupi.springbootinit.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10); // 与 yaml 保持一致
executor.setMaxPoolSize(20);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("save-task-");
executor.initialize();
return executor;
}
}

View File

@@ -99,8 +99,8 @@ public class ServerBigBrother {
/**
* 该数据所属的账号id
*/
@TableField(value = "owner_id")
private String ownerId;
@TableField(value = "user_id")
private Long userId;
/**
* 租户 Id

View File

@@ -45,7 +45,7 @@ public class MQReceiver {
// }
@RabbitListener(queues = "HOST_INFO_QUEUE")
@Async
@Async("taskExecutor")
public void receive(List<NewHosts> hosts, Channel channel, Message message) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try {
@@ -62,7 +62,7 @@ public class MQReceiver {
}
@RabbitListener(queues = "BIG_BROTHER_QUEUE")
@Async
@Async("taskExecutor")
public void bigBrotherReceive(ServerBigBrother bigBrotherList, Channel channel, Message message) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try {
@@ -74,6 +74,7 @@ public class MQReceiver {
} catch (Exception e) {
channel.basicNack(deliveryTag, false, false);
log.error("消息消费失败");
log.error(e.getMessage());
throw new BusinessException(ErrorCode.QUEUE_CONSUMPTION_FAILURE);
}
}

View File

@@ -48,7 +48,6 @@ public class HostInfoServiceImpl extends ServiceImpl<NewHostsMapper, NewHosts> i
@Override
@Async("taskExecutor")
public CompletableFuture<Void> saveHostInfo(List<NewHosts> newHosts) {
try {
StopWatch stopWatch = new StopWatch();

View File

@@ -1,5 +1,6 @@
package com.yupi.springbootinit.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
@@ -24,13 +25,13 @@ public class ServerBigBrotherServiceImpl extends ServiceImpl<ServerBigBrotherMap
@Override
@Transactional(rollbackFor = Exception.class)
@Async("taskExecutor")
public void saveData(ServerBigBrother bigBrother) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ServerBigBrother serverBigBrother = baseMapper.selectOne(Wrappers.<ServerBigBrother>lambdaQuery()
.eq(ServerBigBrother::getDisplayId, bigBrother.getDisplayId())
.eq(ServerBigBrother::getTenantId, bigBrother.getTenantId()));
QueryWrapper<ServerBigBrother> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("display_id", bigBrother.getDisplayId())
.eq("tenant_id", bigBrother.getTenantId());
ServerBigBrother serverBigBrother = baseMapper.selectOne(queryWrapper);
if(serverBigBrother == null){
save(bigBrother);
stopWatch.stop();

View File

@@ -5,7 +5,7 @@ spring:
task:
# Spring 执行器配置,对应 TaskExecutionProperties 配置类。对于 Spring 异步任务,会使用该执行器。
execution:
thread-name-prefix: save-task # 线程池的线程名的前缀。默认为 task- ,建议根据自己应用来设置
thread-name-prefix: save-task # 线程池的线程名的前缀。默认为 task- ,建议根据自己应用来设置
pool: # 线程池相关
core-size: 10 # 核心线程数,线程池创建时候初始化的线程数。默认为 8 。
max-size: 20 # 最大线程数,线程池最大的线程数,只有在缓冲队列满了之后,才会申请超过核心线程数的线程。默认为 Integer.MAX_VALUE

View File

@@ -30,10 +30,15 @@
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
<logger name="com.yupi.springbootinit" level="DEBUG" additivity="false">
<logger name="com.yupi.springbootinit" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="com.baomidou.mybatisplus" level="INFO"/>
<logger name="com.baomidou.mybatisplus" level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.springframework" level="INFO"/>
<logger name="org.redisson" level="INFO"/>
</springProfile>
<springProfile name="prod">
@@ -45,8 +50,8 @@
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<logger name="com.baomidou.mybatisplus" level="WARN"/>
<logger name="org.redisson" level="WARN"/>
<logger name="org.springframework" level="INFO"/>
<logger name="com.baomidou.mybatisplus" level="INFO"/>
<logger name="org.redisson" level="INFO"/>
</springProfile>
</configuration>