π‘ λͺ©ν
κΈ°μ‘΄μ μ΄λ©μΌ κ²μ¦ κ³Όμ μμ μΈμ¦λ²νΈ μμ²μ 보λ΄λ©΄ μ μ΄λ―Έμ§μ κ°μ΄ νκ· 4.18 μ΄μ μμ²μκ°μ΄ κ±Έλ Έλ€.
1. μ€μ μλΉμ€λ₯Ό μ΄μνλ μ μ₯μμλ μ¬μ©μκ° νμκ°μ μ μ°¨κΉμ§ λ€μ΄μλ€λ©΄ λΉ λ₯΄κ² νμκ°μ μ μ°¨λ₯Ό κ°λλ‘ νλ κ²μ΄ μ€μνλ€. λ§μ½ μ΄λ©μΌ μΈμ¦ λ²νΌμ λλ λλ° 4.2 μ΄λ μλ²μ μλ΅μ κΈ°λ€λ¦¬κ³ μλ€λ©΄ μ¬μ©μκ° νμκ°μ μ μλν κ²μ ννν΄λ²λ¦΄ μ§λ λͺ¨λ₯Έλ€.
2. μ΄λ©μΌ λ°μ‘μ μλμ μΌλ‘ μκ°μ΄ μ€λ 걸리λ μμ μ΄κΈ° λλ¬Έμ, μ΄λ₯Ό λΉλκΈ°λ‘ μ²λ¦¬νλ©΄ μ΄λ©μΌ λ°μ‘ λκΈ° μ€μλ λ€λ₯Έ μμ²μ μ²λ¦¬ν μ μμ΄μ μλ² λ¦¬μμ€λ₯Ό ν¨μ¨μ μΌλ‘ μ¬μ©ν μ μμ΄ μμ² λκΈ°μκ°μ΄ λ§μ΄ μ€ κ²μΌλ‘ μμλλ€.
3. μ΄λ©μΌ λ°μ‘ μμ μ€ λ€νΈμν¬ μ§μ°μ΄λ μ΄λ©μΌ μλ²μ μλ΅ μ§μ°μ΄ λ°μνλλΌλ, μλ²μ νμκ°μ μλ΅μκ°μλ μν₯μ΄ λ―ΈμΉμ§ μλλ‘ ν΄μΌνλ€.
μ΄λ¬ν μ΄μ λ€λ‘ λλ μ΄λ©μΌ μ μ‘ λ‘μ§μ λΉλκΈ°λ‘ μ²λ¦¬νκΈ°λ‘ νλ€.
AsyncConfig
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
@Bean(name = "emailTaskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10); // κΈ°λ³Έ μ€λ λ μ
executor.setMaxPoolSize(20); // μ΅λ μ€λ λ μ
executor.setQueueCapacity(100); // λκΈ° νμ ν¬κΈ°
executor.setThreadNamePrefix("EmailThread-");
executor.initialize();
// μ€λ λ νμ μνλ₯Ό λ‘κ·Έλ‘ μΆλ ₯
logThreadPoolStatus(executor);
return executor;
}
private void logThreadPoolStatus(ThreadPoolTaskExecutor executor) {
System.out.println("Active Threads: " + executor.getActiveCount());
System.out.println("Total Tasks: " + executor.getThreadPoolExecutor().getTaskCount());
System.out.println("Completed Tasks: " + executor.getThreadPoolExecutor().getCompletedTaskCount());
}
}
μ°λ λ μ λ± μ°λ λ κ΄λ ¨ μ΅μ κ°λ€μ λͺ μν΄μ£Όκ³ , ThreadPoolTaskExecutor λ₯Ό μ΄μ©ν΄μ κΈ°μ‘΄μ μ¬μ©λ μ°λ λλ₯Ό μ¬μ¬μ©νλλ‘ μ€μ νλ€.
EmailConfig
@Async("emailTaskExecutor")
public CompletableFuture<Boolean> sendCertificationEmail(String email, String certificationNumber) {
λΉλκΈ° μ²λ¦¬νκ³ μ νλ λ©μλ μμ @Async("emailTaskExecutor") μ κ±Έμ΄μ€¬λ€.
SsakTiumApplication
@SpringBootApplication
@EnableJpaAuditing
@EnableAsync
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO)
public class SSakTiumApplication {
public static void main(String[] args) {
SpringApplication.run(SSakTiumApplication.class, args);
}
}
main application μ κ°μ μμ @EnableAsync μ΄λ Έν μ΄μ μ λ¬μ쀬λ€.
π‘ λΉλκΈ° μ²λ¦¬λ₯Ό ν΅ν΄ νκ· 4.18μ΄μμ νκ· μ½ 1.04μ΄λ‘ 75.2% μ±λ₯ κ°μ μ΄ μ΄λ£¨μ΄μ‘λ€!