用户模块

This commit is contained in:
chen-xin-zhi 2025-05-08 09:58:10 +08:00
parent 2a1aa9abef
commit a5bbaf08dc
5 changed files with 79 additions and 2 deletions

View File

@ -239,7 +239,7 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
LambdaQueryWrapper<UserInfo> 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, "验证码获取失败");

View File

@ -8,7 +8,9 @@ spring:
maximum-pool-size: 20
max-lifetime: 120000
# mvc:
# async:
# request-timeout: 400
data:

View File

@ -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;
}
}

View File

@ -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<String> 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();
}
}
}

View File

@ -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<String> longRunningTask() {
try {
// 模拟长时间的操作比如数据库查询调用外部接口等
Thread.sleep(1000); // 1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
return CompletableFuture.completedFuture("Task Completed");
}
}