接收平台消息,模板模式实践

接收平台消息,模板模式实践

模板抽象类

@Service
public abstract class BaseMessageService {

    private static final Logger logger = LoggerFactory.getLogger(KwaiOrderMessageService.class);

    /**
     * Description: 数据处理
     * Date: 2021/9/15 11:17
     * @Author: wangwenying10
     * @Param: [parentMessage]
     * @Return: com.gome.retail.boot.model.Response<java.lang.Boolean>
     */
    protected abstract Response<Boolean> dataHandle(KwaiParentMessage parentMessage);

    /**
     * Description: 任务执行 先处理数据再进行业务处理
     * Date: 2021/9/15 11:13
     * @Author: wangwenying10
     * @Param: [parentMessage]
     * @Return: com.gome.retail.boot.model.Response<java.lang.Boolean>
     */
    public Response<Boolean> execute(KwaiParentMessage parentMessage) {
        Response<Boolean> kwaiParentMessageResponse = dataHandle(parentMessage);
        if (!kwaiParentMessageResponse.isOk()) {
            logger.error(kwaiParentMessageResponse.getMsg());
            return Response.fail("参数解析失败");
        }
        return bussiness(parentMessage);
    }

    /**
     * Description: 业务处理
     * Date: 2021/9/15 11:17
     * @Author: wangwenying10
     * @Param: [parentMessage]
     * @Return: com.gome.retail.boot.model.Response<java.lang.Boolean>
     */
    protected abstract Response<Boolean> bussiness(KwaiParentMessage parentMessage);
}

继承模板类的子类

@KwaiEvent({"kwaishop_order_statusChange"})
@Service
public class KwaiOrderMessageService extends BaseMessageService {

    private static final Logger logger = LoggerFactory.getLogger(KwaiOrderMessageService.class);

    @Resource
    private KwaiApiService kwaiApiService;

    @Resource
    private KwaiExtchCfgService kwaiExtchCfgService;

    @Resource
    private KwaiMcoOrderService kwaiMcoOrderService;

    @Override
    protected Response<Boolean> dataHandle(KwaiParentMessage parentMessage) {
        try {
            if (StringUtils.isEmpty(parentMessage.getInfo())) {
                return Response.fail("缺少消息体");
            }
            KwaiMessageOrder kwaiMessageOrder = JSON.parseObject(parentMessage.getInfo(), KwaiMessageOrder.class);
            parentMessage.setKwaiMessage(kwaiMessageOrder);
        } catch (Exception e) {
            logger.error("参数解析失败message:{}", JSON.toJSONString(parentMessage), e);
            return Response.fail("参数解析失败");
        }
        return Response.ok();
    }

    @Override
    protected Response<Boolean> bussiness(KwaiParentMessage parentMessage) {
        KwaiExtchCfg kwaiExtchCfg = kwaiExtchCfgService.obtainCacheExtchCfg(String.valueOf(parentMessage.getUserId()));
        KwaiMessageOrder kwaiMessageOrder = (KwaiMessageOrder) parentMessage.getKwaiMessage();
        logger.info("消息单个订单获取 oid:{}", kwaiMessageOrder.getOid());
        OpenSellerOrderDetailResponse response = kwaiApiService.orderInfoGet(kwaiMessageOrder.getOid(), kwaiExtchCfg);
        logger.info("消息单个订单获取返回 oid:{} response:{}", kwaiMessageOrder.getOid(), JSON.toJSONString(response));
        if (response.isSuccess() && null != response.getData()) {
            Response<Boolean> singleResponse = kwaiMcoOrderService.singleOrder(response.getData(), kwaiExtchCfg);
            logger.info("消息单个订单处理结果oid:{}, res:{}", kwaiMessageOrder.getOid(), JSON.toJSONString(singleResponse));
        }
        return Response.ok();
    }
}

实现类缓存

@Component
public class KwaiAcceptServiceFactory {

    private static final Logger logger = LoggerFactory.getLogger(KwaiAcceptServiceFactory.class);

    /**
     * 消息service注册表
     */
    private Map<String, BaseMessageService> messageServiceMap;

    @PostConstruct
    private void init() {
        messageServiceMap = new HashMap<>();
        initMessageService();
    }

    /**
     * 初始化service注册表
     */
    private void initMessageService() {
        Map<String, BaseMessageService> result = SpringBeanUtil.getApplicationContext().getBeansOfType(BaseMessageService.class);
        if (CollectionUtils.isEmpty(result)) {
            logger.warn("初始化获取失败,没有发现策略实现 bean。");
            throw new RuntimeException("初始化消息处理类失败");
        }
        for (Map.Entry<String, BaseMessageService> entry : result.entrySet()) {
            String beanName = entry.getKey();
            BaseMessageService value = entry.getValue();

            KwaiEvent dataAnnotation = value.getClass().getAnnotation(KwaiEvent.class);
            if (null != dataAnnotation) {
                for (String string : dataAnnotation.value()) {
                    messageServiceMap.put(string, value);
                }
            } else {
                logger.info("初始化,operateType is null, bean:{}", beanName);
            }
        }
    }

    /**
     * Description: 获取消息处理类
     * Date: 2021/9/14 19:21
     *
     * @Author: wangwenying10
     * @Param: [operateType]
     * @Return: cn.com.gome.scot.alamein.mco.kwai.job.business.domain.message.BaseMessageService
     */
    public BaseMessageService obtainMessageService(String operateType) {
        if (operateType == null) {
            logger.error("[McoServiceFactory.getExtchDataSaveService] the input param is null, return [null].");
            return null;
        }
        return messageServiceMap.get(operateType);
    }
}

调用


            BaseMessageService service = kwaiAcceptServiceFactory.obtainMessageService(kwaiMessage.getEvent());
            dispatchExecutorsPool.getDefaultThreadPool().execute(() -> service.execute(kwaiMessage));
posted @ 2021-09-15 21:39  倔强的老铁  阅读(37)  评论(0编辑  收藏  举报