add async for none blocking on event listener

structure of packages not standard
This commit is contained in:
esmailian
2025-04-27 15:25:40 +03:30
parent e7f5113ec0
commit 97f1d67130
3 changed files with 27 additions and 0 deletions

View File

@ -3,9 +3,11 @@ package com.example.mmad.testapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableDiscoveryClient
@EnableAsync
public class TestAAppApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,23 @@
package com.example.mmad.testapp.config;
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(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("taskExecutor-");
executor.initialize();
return executor;
}
}

View File

@ -2,10 +2,12 @@ package com.example.mmad.testapp.query;
import com.example.mmad.testapp.event.UserCreatedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class UserQueryHandler {
@Async("taskExecutor")
@EventListener
public void handelUserCreated(UserCreatedEvent event) {
System.out.println("Received user created event" + event.getUserId());