From a5bbaf08dc004c81e7961e1f4784e1621b25d8f8 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Thu, 8 May 2025 09:58:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/impl/UserInfoServiceImpl.java | 2 +- src/main/resources/application.yml | 4 ++- .../promotion/timeout/AsyncConfig.java | 24 +++++++++++++++ .../promotion/timeout/MyController.java | 30 +++++++++++++++++++ .../promotion/timeout/MyService.java | 21 +++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/greenorange/promotion/timeout/AsyncConfig.java create mode 100644 src/test/java/com/greenorange/promotion/timeout/MyController.java create mode 100644 src/test/java/com/greenorange/promotion/timeout/MyService.java diff --git a/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java b/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java index 339ecb5..ac4c2e5 100644 --- a/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java +++ b/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java @@ -239,7 +239,7 @@ public class UserInfoServiceImpl extends ServiceImpl LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(UserInfo::getPhoneNumber, phoneNumber); UserInfo userInfo = this.getOne(lambdaQueryWrapper); - ThrowUtils.throwIf(userInfo != null, ErrorCode.OPERATION_ERROR, "手机号未注册"); + ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "手机号未注册"); String verificationCode = SendSmsUtil.getVerificationCode(phoneNumber); ThrowUtils.throwIf(verificationCode == null, ErrorCode.OPERATION_ERROR, "验证码获取失败"); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 795ce0a..376b968 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,7 +8,9 @@ spring: maximum-pool-size: 20 max-lifetime: 120000 - +# mvc: +# async: +# request-timeout: 400 data: diff --git a/src/test/java/com/greenorange/promotion/timeout/AsyncConfig.java b/src/test/java/com/greenorange/promotion/timeout/AsyncConfig.java new file mode 100644 index 0000000..919f663 --- /dev/null +++ b/src/test/java/com/greenorange/promotion/timeout/AsyncConfig.java @@ -0,0 +1,24 @@ +package com.greenorange.promotion.timeout; + +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(5); + executor.setMaxPoolSize(10); + executor.setQueueCapacity(25); + executor.setThreadNamePrefix("async-"); + executor.initialize(); + return executor; + } +} diff --git a/src/test/java/com/greenorange/promotion/timeout/MyController.java b/src/test/java/com/greenorange/promotion/timeout/MyController.java new file mode 100644 index 0000000..235dd4c --- /dev/null +++ b/src/test/java/com/greenorange/promotion/timeout/MyController.java @@ -0,0 +1,30 @@ +package com.greenorange.promotion.timeout; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +@RestController +public class MyController { + + @Autowired + private MyService myService; + + @GetMapping("/executeAsync") + public String executeAsync() { + CompletableFuture future = myService.longRunningTask(); + + try { + // 设置超时时间为5秒,如果5秒后任务仍未完成则抛出 TimeoutException + return future.get(2000, TimeUnit.MILLISECONDS); // 设置5秒超时 + } catch (TimeoutException e) { + return "Request timed out"; + } catch (Exception e) { + return "An error occurred: " + e.getMessage(); + } + } +} diff --git a/src/test/java/com/greenorange/promotion/timeout/MyService.java b/src/test/java/com/greenorange/promotion/timeout/MyService.java new file mode 100644 index 0000000..dd65557 --- /dev/null +++ b/src/test/java/com/greenorange/promotion/timeout/MyService.java @@ -0,0 +1,21 @@ +package com.greenorange.promotion.timeout; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.util.concurrent.CompletableFuture; + +@Service +public class MyService { + + @Async + public CompletableFuture longRunningTask() { + try { + // 模拟长时间的操作,比如数据库查询、调用外部接口等 + Thread.sleep(1000); // 1秒 + } catch (InterruptedException e) { + e.printStackTrace(); + } + return CompletableFuture.completedFuture("Task Completed"); + } +}