数据对象:
@Data
@Builder
public class SelfEvent {
/**
* 消息类型
*/
private SelfEventTypeEnum selfEventTypeEnum;
/**
* 消息体
*/
private Object body;
}
事件发布工具类:
@Component
public class SelfEventService {
private static ApplicationContext context;
public SelfEventService(ApplicationContext applicationContext) {
this.context = applicationContext;
}
public static void publishSelfEvent(SelfEvent selfEvent) {
context.publishEvent(selfEvent);
}
}
事件处理类:
@Component
public class SelfEventListener {
private final Logger log = LoggerFactory.getLogger(SelfEventListener.class);
@EventListener
@Async
public void handleOrderEvent(SelfEvent event) {
if (event == null) {
return;
}
switch (event.getSelfEventTypeEnum()) {
case ***:
handle....;
break;
default:
log.info("不支持的事件类型");
}
}
}