1 不同实现类字典
@AllArgsConstructor
@Getter
public enum SendMessageEnum {HUAWEI("AndHWPush"),JIGUANG("AndJPush"),MEIZU("AndMZPush"),OPPO("AndOppoPush"),XIAOMI("AndXMPush"),XINGE("AndXinGePush"),;private String type;/*** 匹配*/public static SendMessageEnum explain(String type) {for (SendMessageEnum sendMessageEnum : SendMessageEnum.values()) {if (sendMessageEnum.getType().equals(type)) {return sendMessageEnum;}}return null;}
}
1 // sendMessage 实现类对象service 调用推送方法
/*** 捕获的异常属于对接推送渠道的异常信息*/@Overridepublic void channelPush(NotificationBO notificationBO, ChannelDO channelDO, PushClientDO pushClientDO, List <String> aliasList) {String pushConf = pushClientDO.getPushConf();JSONObject jsonObject = JSONObject.parseObject(pushConf);PushConfigInfo pushConfigInfo = JSON.toJavaObject(jsonObject, PushConfigInfo.class);if (pushConfigInfo == null) {return ;}pushConfigInfo.setNoticePush(true);//获取不同的实现类对象SendMessageEnum sendMessageEnum = SendMessageEnum.explain(pushClientDO.getPushClient());try {SendMessageService sendMessage = sendMessageFactory.getSendMessage(sendMessageEnum);// sendMessage 实现类对象service 调用推送方法ThreadPoolUtil.getInstance().synChannelPush(sendMessage, channelDO, pushConfigInfo, notificationBO, aliasList);channelDO.setLastMsg(notificationBO.getPushMessage());} catch (Exception e) {log.error("[FAIL]Push Exception information : {}", e.getMessage());}}
2 通过工厂获取service 对象
@Component
public class SendMessageFactory {@Autowiredprivate List <SendMessageService> sendMessageServices;public SendMessageService getSendMessage(SendMessageEnum sendMessageEnum){for (SendMessageService sendMessageService : sendMessageServices) {// 通过support判断是哪一个实现类if (sendMessageService.support(sendMessageEnum)) {return sendMessageService;}}throw new UnsupportedOperationException("unsupported request: " + sendMessageEnum);}
}@Slf4j
@Component(value = "hWSendNotifyMessage")
public class HwSendNotifyMessage implements SendMessageService {// 通过support判断是哪一个实现类@Overridepublic boolean support(SendMessageEnum sendMessageEnum) {return sendMessageEnum == SendMessageEnum.HUAWEI;}
}// 发送简写了自己补全下 @Overridepublic boolean send(SendMessageEnum sendMessageEnum) {return sendMessageEnum == SendMessageEnum.HUAWEI;}
public void synChannelPush(SendMessageService sendMessage,ChannelDO channelDO,PushConfigInfo pushConfigInfo,NotificationBO notificationBO, List <String> aliasList){CompletableFuture <String> future = CompletableFuture.supplyAsync(new Supplier <String>() {@Overridepublic String get() {try{sendMessage.send(channelDO,pushConfigInfo,notificationBO,aliasList);return "task finally channelDO : " + channelDO;} catch (Exception e){log.error("[FAIL]Push Exception information : {}",e.getMessage());e.printStackTrace();}return "[fail] push Exception channelDO : " + channelDO.toString();}}, pool);//lambada的实现方式future.thenAccept(e -> log.info("推送结果" + e));}