阳光沙滩博客系统-异步发送邮件

阳光沙滩博客系统-集成邮件发送 (opens new window)

前面我们集成了邮件发送,但是呢,很慢!

所以我们通过异步的方式来发送,让用户体验变得更好一点。

# 添加AsyncConfiguration配置

@Configuration
@EnableAsync
public class AsyncConfiguration {

    @Bean
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(10);
        executor.setThreadNamePrefix("sob_blog_task_worker-");
        executor.setQueueCapacity(30);
        executor.initialize();
        return executor;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 开启异步

@Service
public class TaskService {
    @Async
    public void sendEmailVerifyCode(String verifyCode, String emailAddress) throws Exception {
        EmailSender.sendRegisterVerifyCode(verifyCode, emailAddress);
    }
}
1
2
3
4
5
6
7

# 邮箱正则校验

 String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(邮箱地址);
1
2
3
上次更新: 2022/03/28, 23:04:38