ApplicationEventPublisher的使用

主类:

@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)的作用就是监听

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值