com.codingapi.springboot.framework.handler.SpringEventHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of springboot-starter Show documentation
Show all versions of springboot-starter Show documentation
springboot-starter project for Spring Boot
package com.codingapi.springboot.framework.handler;
import com.codingapi.springboot.framework.event.DomainEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* handler订阅的Spring触发器,在异步的情况下可配置多线程。
*/
@Slf4j
public class SpringEventHandler implements ApplicationListener {
/**
* 异步多线程的KEY
* 可通过 System.setProperty(THREAD_KEY,"20") 调整线程数
*/
public final static String THREAD_KEY = "Handler.ThreadPools";
private final ExecutorService executorService = Executors.newFixedThreadPool(Integer.parseInt(System.getProperty(THREAD_KEY, "10")));
public SpringEventHandler(List handlers) {
ApplicationHandlerUtils.getInstance().addHandlers(handlers);
}
@Override
public void onApplicationEvent(DomainEvent domainEvent) {
if (domainEvent.isSync()) {
ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent());
} else {
executorService.execute(() -> {
ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent());
});
}
}
}