add async for none blocking on event listener
structure of packages not standard
This commit is contained in:
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
Reference in New Issue
Block a user