Spring事件驱动,在spring boot中使用spring 的事件模型。事件模型包含 事件类,事件监听器,注册事件监听器,事件发布者。以下代码功能:1.自定义监听器执行顺序 2.监听器由线程池异步执行。1和2是互斥关系,稍稍改动即可
package com.example.demo.springevent;
import org.springframework.context.ApplicationEvent;
public class StudentEvent extends ApplicationEvent {
String id;
String name;
Integer price;
public StudentEvent(Object source,String id,String name, Integer price) {
super(source);
this.id=id;
this.name =name;
this.price =price;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Integer getPrice() {
return price;
}
}
package com.example.demo.springevent.e3.e2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class AppliOederConfig {
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(10);
executor.setThreadNamePrefix("Async-");
executor.initialize();
return executor;
}
@Bean
public StudentEventListenerInBookOrder studentEventListenerInBookOrder(Executor executor) {
return new StudentEventListenerInBookOrder(executor);
}
@Bean
public StudentEventListenerInFoodOrder studentEventListenerInFoodOrder(Executor executor) {
return new StudentEventListenerInFoodOrder(executor);
}
@Bean
public StudentEventListenerInWifiOrder studentEventListenerInWifiOrder(Executor executor) {
return new StudentEventListenerInWifiOrder(executor);
}
@Bean
public StudentEventListenerInWaterOrder studentEventListenerInWaterOrder(Executor executor) {
return new StudentEventListenerInWaterOrder(executor);
}
}
package com.example.demo.springevent.e3.e2;
import com.example.demo.springevent.StudentEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import java.util.concurrent.Executor;
@Slf4j
public class StudentEventListenerInBookOrder implements SmartApplicationListener {
private final Executor executor;
public StudentEventListenerInBookOrder(Executor executor) {
this.executor = executor;
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return eventType == StudentEvent.class;
}
@Async
@Override
public void onApplicationEvent(ApplicationEvent event) {
executor.execute(() -> {
StudentEvent studentEvent = (StudentEvent) event;
String id = studentEvent.getId();
String name = studentEvent.getName();
int price = studentEvent.getPrice();
log.info("y1图书信息清零,处理完毕" + id + " " + name + " " + price + " " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
});
}
@Override
public int getOrder() {
return 1;
}
}
package com.example.demo.springevent.e3.e2;
import com.example.demo.springevent.StudentEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
@Slf4j
public class StudentEventListenerInFoodOrder implements SmartApplicationListener {
private final Executor executor;
public StudentEventListenerInFoodOrder(Executor executor) {
this.executor = executor;
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return eventType == StudentEvent.class;
}
@Async
@Override
public void onApplicationEvent(ApplicationEvent event) {
executor.execute(() -> {
StudentEvent studentEvent = (StudentEvent) event;
String id = studentEvent.getId();
String name = studentEvent.getName();
int price = studentEvent.getPrice();
log.info("y2饭卡信息清零,处理完毕" + id + " " + name + " " + price + " " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
});
}
@Override
public int getOrder() {
return 2;
}
}
package com.example.demo.springevent.e3.e2;
import com.example.demo.springevent.StudentEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.concurrent.*;
@Slf4j
public class StudentEventListenerInWaterOrder implements SmartApplicationListener {
private final Executor executor;
public StudentEventListenerInWaterOrder(Executor executor) {
this.executor = executor;
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return eventType == StudentEvent.class;
}
@Async
@Override
public void onApplicationEvent(ApplicationEvent event) {
executor.execute(() -> {
StudentEvent studentEvent = (StudentEvent) event;
String id = studentEvent.getId();
String name = studentEvent.getName();
int price = studentEvent.getPrice();
log.info("y3网络信息清零,处理完毕" + id + " " + name + " " + price + " " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
});
}
@Override
public int getOrder() {
return 3;
}
}
package com.example.demo.springevent.e3.e2;
import com.alibaba.fastjson.JSON;
import com.example.demo.springevent.StudentEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
@Service("publisher2")
@Slf4j
public class StudentEventPublisher2 {
public void publishStudentEvent() {
ApplicationContext context = new AnnotationConfigApplicationContext(AppliOederConfig.class);
ApplicationEventPublisher publisher = context;
StudentEvent studentEvent = new StudentEvent(new Object(), "y", "yyyy", 8888);
log.info("发布事件,{}", JSON.toJSONString(studentEvent));
publisher.publishEvent(studentEvent);
}
}
package com.example.demo.service;
import com.example.demo.springevent.e2.StudentEventPublisher1;
import com.example.demo.springevent.e3.e2.StudentEventPublisher2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class BuildData implements ApplicationRunner {
@Resource(name = "publisher2")
StudentEventPublisher2 studentEventPublisher2;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner方法执行");
studentEventPublisher2.publishStudentEvent();
}
}