流程
extends ApplicationEvent建立操作事件—>业务service注册事件—>Listener监听消费
参考资料
https://www.jianshu.com/p/af2aa0fce0ed
https://www.jianshu.com/p/4359dd4b36a6
方案
说明:两种方案不同在于@Async性能有一定提升
listener可以多个业务共享建立不同监听方法
@Async需要对每个业务做不同监听
1,Listener
//建立操作事件
package com.onecardmall.core.event;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
@Getter
public class DemoApplicationEventextends ApplicationEvent {
public DemoApplicationEvent(Object source,String uname) {
super(source);
this.uname=uname;
}
private Stringuname;
}
//业务service注册事件
package com.onecardmall.core.service.impl;
import com.onecardmall.core.event.DemoApplicationEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class DemoApplicationEventService {
@Autowired
ApplicationContextapplicationContext;
/**
* 用户注册方法
* @param uname
*/
public void register(String uname)
{
//../省略其他逻辑
//发布UserRegisterEvent事件
applicationContext.publishEvent(new DemoApplicationEvent(this,uname));
}
}
//调用业务
package com.onecardmall.core.controller;
import com.onecardmall.core.annotation.GlobalException;
import com.onecardmall.core.service.impl.DemoApplicationEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demoevent")
@Slf4j
public class DemoApplicationEventCtronller {
//用户业务逻辑实现
@Autowired
private DemoApplicationEventServiceuserService;
/**
* 注册控制方法
*
* @param uname 用户对象
* @return
*/
@RequestMapping(value ="/register")
public Stringregister
(
String uname
) {
//调用注册业务逻辑
userService.register(uname);
return "注册成功.";
}
}
//Listener监听消费
package com.onecardmall.core.listener;
import com.onecardmall.core.event.DemoApplicationEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AnnotationRegisterListener {
/**
* 注册监听实现方法
* @param userRegisterEvent 用户注册事件
*/
@EventListener
public void register(DemoApplicationEvent userRegisterEvent)
{
//输出注册用户信息
System.out.println("@EventListener注册信息,用户名:"+userRegisterEvent.getUname());
}
}
2,@Async
//配置线程池
package com.onecardmall.core.constant;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class ListenerAsyncConfigurationimplements AsyncConfigurer {
/**
* 获取异步线程池执行对象
* @return
*/
@Override
public ExecutorgetAsyncExecutor() {
//使用Spring内置线程池任务对象
ThreadPoolTaskExecutor taskExecutor =new ThreadPoolTaskExecutor();
//设置线程池参数
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandlergetAsyncUncaughtExceptionHandler() {
return null;
}
}
//配置注解监听 注意 implements ApplicationListener 这个和listener版本不同
package com.onecardmall.core.listener;
import com.onecardmall.core.event.DemoApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncRegisterListener implements ApplicationListener {
@Async
public void onApplicationEvent(DemoApplicationEvent applicationEvent) {
System.out.println("@onApplicationEvent");
//转换事件类型
//获取注册用户对象信息
//输出注册用户信息
System.out.println("onApplicationEvent @EventListener注册信息,用户名:"+applicationEvent.getUname());
}
}
其它同listener