主类:
@Service
@Slf4j
@AllArgsConstructor
public class PlatNoticeService {
private final ApplicationEventPublisher eventPublisher;
/**
*@param map 需要推送的数据
*/
public void publish(Map<String, Object> map, String type){
eventPublisher.publishEvent(PushMessageEvent
.builder()
.type(type)
.mapJson(JSONUtil.toJsonStr(map))
.build()
);
}
}
event:
@Getter
@Builder
public class PushMessageEvent {
/**
* 数据集合
*/
private String mapJson;
/**
* 相关业务类型(appNotice--app通知栏推送,sms--短信推送,wxTemplate--微信模板推送)
*/
private String type;
}
监听:
@Slf4j
@Component
@AllArgsConstructor
public class PushMessageEventListener {
@Async("asyncServicePool")
@EventListener(PushMessageEvent.class)
public void handleEvent(PushMessageEvent vo) {
// 消息推送
try {
if (log.isWarnEnabled()) {
log.warn("消息发送成功:{}", vo.getMapJson());
}
} catch (Exception e) {
log.error("{}消息推送失败:{}", vo.getType(), e);
}
}
}
PushMessageEvent 自定义的,需要在监听类那里监听这个类的变化 即 @EventListener(PushMessageEvent.class)的作用就是监听