1.创建事件
@Getter
@Setter
public class CreateEvent extends ApplicationEvent {
private String id;
private int way;
public CreateEvent(String source,int way) {
super(source);
this.id = source;
this.way = way;
}
}
2.事件发布
@Component
public class CreateEventPublisher implements ApplicationEventPublisherAware {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
public ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public Boolean pushEvent(String id,int way) throws BaseException{
CreateEvent createEvent = new CreateEvent(id,way);
publisher.publishEvent(createEvent);
return true;
}
}
3.创建监听器
@Component
@Slf4j
public class CreateEventListener {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
//@Async
@EventListener
public void onApplicationEvent(CreateEvent createEvent){
//业务逻辑
}
}