一、定义事件event
public class AddVehicleEvent extends ApplicationEvent {
private Map<String, Object> params;
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
public AddVehicleEvent(Object source, Map<String, Object> params) {
super(source);
this.params = params;
}
}
二、监听事件
@Component
public class AddVehicleEventListener {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private VehicleService vehicleService;
@Async //异步处理
@EventListener
public void AddVehicleEvent(AddVehicleEvent soutEvent) {
if (soutEvent == null) {
return;
}
try {
Thread.sleep(20000); //先睡20秒
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("监听到添加车辆事件");
Map<String, Object> map = soutEvent.getParams();
Object object = map.get("data");
Vehicle vehicle = (Vehicle) object;
vehicleService.insert(vehicle);
logger.info("添加车辆完成");
}
}
三、发布事件
@Component
public class MyEventPublisher {
@Autowired
ApplicationContext applicationContext;
public void test(Map<String, Object> params) {
applicationContext.publishEvent(new AddVehicleEvent(this,params));
}
}
四、service
@Component
public class AddVehicleService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private MyEventPublisher myEventPublisher;
public void test() {
logger.info("准备添加一辆车");
Vehicle vehicle = new Vehicle();
vehicle.setVehicleId("01234567890");
Map<String, Object> map = new HashMap<>();
map.put("data",vehicle);
myEventPublisher.test(map);
logger.info("方法执行完毕");
}
}
五、启动类增加异步注解 @EnableAsync
六、UT测试
今天学习完之后,自己写了一个测试方法,发现异步时候总是不处理,不插入数据库,后来才想起来,是用UT在测,执行完项目就停了,你要等20秒,还处理个锤子啊,真是蠢哭了
七、在controller测试
1、执行方法,立即返回执行完毕
2、20秒后我们的异步方法开始执行
单线程 -------------执行时间:38875
@Async
@EventListener
public void AddVehicleEvent(AddVehicleEvent soutEvent) throws InterruptedException {
if (soutEvent == null) {
return;
}
long start = System.currentTimeMillis();
ExecutorService service = Executors.newSingleThreadExecutor();
for (int i=0; i<500; i++) {
service.execute(() -> {
logger.info("监听到添加车辆事件");
Map<String, Object> map = soutEvent.getParams();
Object object = map.get("data");
Vehicle vehicle = (Vehicle) object;
vehicleService.insert(vehicle);
logger.info("添加车辆完成");
});
}
service.shutdown();
service.awaitTermination(1,TimeUnit.DAYS);
long end = System.currentTimeMillis();
logger.info("执行时间:" + (end - start));
}
多线程-----------------执行时间:4416
@Async
@EventListener
public void AddVehicleEvent(AddVehicleEvent soutEvent) throws InterruptedException {
if (soutEvent == null) {
return;
}
long start = System.currentTimeMillis();
ExecutorService service = Executors.newFixedThreadPool(300);
for (int i=0; i<500; i++) {
service.execute(() -> {
logger.info("监听到添加车辆事件");
Map<String, Object> map = soutEvent.getParams();
Object object = map.get("data");
Vehicle vehicle = (Vehicle) object;
vehicleService.insert(vehicle);
logger.info("添加车辆完成");
});
}
service.shutdown();
service.awaitTermination(1,TimeUnit.DAYS);
long end = System.currentTimeMillis();
logger.info("执行时间:" + (end - start));
}