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 * 该数据所属的账号id
*/ */
@TableField(value = "owner_id") @TableField(value = "user_id")
private String ownerId; private Long userId;
/** /**
* 租户 Id * 租户 Id

View File

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

View File

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

View File

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

View File

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