Skip to content

Commit

Permalink
Merge pull request #150 from Lixuhuilll/cancel-emailservice-circular-…
Browse files Browse the repository at this point in the history
…dependency

去除 EmailService 中的循环依赖,避免与 AOT 的兼容性问题
  • Loading branch information
dragove authored Dec 24, 2023
2 parents 72aa329 + e7e25c5 commit 0981269
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
35 changes: 35 additions & 0 deletions src/main/java/plus/maa/backend/config/ThreadPoolConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package plus.maa.backend.config;

import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import static org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME;

@Configuration(proxyBeanMethods = false)
public class ThreadPoolConfig {

@Lazy
@Primary
@Bean(name = {APPLICATION_TASK_EXECUTOR_BEAN_NAME,
AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME})
public ThreadPoolTaskExecutor defaultTaskExecutor(TaskExecutorBuilder builder) {
return builder.build();
}

@Bean
public ThreadPoolTaskExecutor emailTaskExecutor() {
// 在默认线程池配置的基础上修改了核心线程数和线程名称
var taskExecutor = new ThreadPoolTaskExecutor();
// I/O 密集型配置
taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
taskExecutor.setThreadNamePrefix("email-task-");
// 动态的核心线程数量
taskExecutor.setAllowCoreThreadTimeOut(true);
return taskExecutor;
}
}
45 changes: 22 additions & 23 deletions src/main/java/plus/maa/backend/service/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import plus.maa.backend.common.bo.EmailBusinessObject;
Expand Down Expand Up @@ -44,10 +44,8 @@ public class EmailService {

private final MailAccount MAIL_ACCOUNT = new MailAccount();

// 注入自身代理类的延迟加载代理类
@Lazy
@Resource
private EmailService emailService;
@Resource(name = "emailTaskExecutor")
private final AsyncTaskExecutor emailTaskExecutor;

/**
* 初始化邮件账户信息
Expand Down Expand Up @@ -82,25 +80,26 @@ public void sendVCode(String email) {
// 设置失败,说明 key 已存在
throw new MaaResultException(403, String.format("发送验证码的请求至少需要间隔 %d 秒", timeout));
}
// 调用注入的代理类执行异步任务
emailService.asyncSendVCode(email);
// 执行异步任务
asyncSendVCode(email);
}

@Async
protected void asyncSendVCode(String email) {
// 6位随机数验证码
String vcode = RandomStringUtils.random(6, true, true).toUpperCase();
if (flagNoSend) {
log.debug("vcode is " + vcode);
log.warn("Email not sent, no-send enabled");
} else {
EmailBusinessObject.builder()
.setMailAccount(MAIL_ACCOUNT)
.setEmail(email)
.sendVerificationCodeMessage(vcode);
}
// 存redis
redisCache.setCache("vCodeEmail:" + email, vcode, expire);
private void asyncSendVCode(String email) {
emailTaskExecutor.execute(() -> {
// 6位随机数验证码
String vcode = RandomStringUtils.random(6, true, true).toUpperCase();
if (flagNoSend) {
log.debug("vcode is " + vcode);
log.warn("Email not sent, no-send enabled");
} else {
EmailBusinessObject.builder()
.setMailAccount(MAIL_ACCOUNT)
.setEmail(email)
.sendVerificationCodeMessage(vcode);
}
// 存redis
redisCache.setCache("vCodeEmail:" + email, vcode, expire);
});
}

/**
Expand All @@ -115,7 +114,7 @@ public void verifyVCode(String email, String vcode) {
}
}

@Async
@Async("emailTaskExecutor")
public void sendCommentNotification(String email, CommentNotification commentNotification) {
int limit = 25;

Expand Down

0 comments on commit 0981269

Please sign in to comment.