推送抽象类;公共模板方法封装. @author itning @since 2021/3/22 16:36
| 29 | * @since 2021/3/22 16:36 |
| 30 | */ |
| 31 | @Slf4j |
| 32 | public abstract class AbstractPush implements Push, RetryListener { |
| 33 | |
| 34 | private final Retryer<JsonObject> retryer; |
| 35 | protected final RequestConfig requestConfig; |
| 36 | |
| 37 | public AbstractPush() { |
| 38 | |
| 39 | HttpHost proxy = ConfigLoader.helperConfig.getPushConfig().getProxy(); |
| 40 | RequestConfig.Builder builder = RequestConfig.custom() |
| 41 | .setConnectTimeout(5000) |
| 42 | .setConnectionRequestTimeout(5000) |
| 43 | .setSocketTimeout(10000); |
| 44 | if (null != proxy) { |
| 45 | builder.setProxy(proxy); |
| 46 | } |
| 47 | requestConfig = builder.build(); |
| 48 | |
| 49 | retryer = RetryerBuilder.<JsonObject>newBuilder() |
| 50 | // 出现异常进行重试 |
| 51 | .retryIfException() |
| 52 | // 检查结果进行重试 |
| 53 | .retryIfResult((it) -> !checkPushStatus(it)) |
| 54 | // 每次重试等待策略 |
| 55 | .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) |
| 56 | // 重试停止策略 |
| 57 | .withStopStrategy(StopStrategies.stopAfterAttempt(3)) |
| 58 | // 持续时间限制 |
| 59 | .withAttemptTimeLimiter(AttemptTimeLimiters.noTimeLimit()) |
| 60 | // 重试监听器 |
| 61 | .withRetryListener(this) |
| 62 | .build(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public final PushResult doPush(PushMetaInfo metaInfo, String content) { |
| 67 | String url = generatePushUrl(metaInfo); |
| 68 | assert null != url : "推送URL不能为空"; |
| 69 | List<String> pushList = segmentation(metaInfo, content); |
| 70 | PushResult pushResult = PushResult.success(); |
| 71 | for (String pushItemContent : pushList) { |
| 72 | String pushContent = generatePushBody(metaInfo, pushItemContent); |
| 73 | PushResult result = push2Target(url, pushContent); |
| 74 | if (!result.isSuccess()) { |
| 75 | pushResult = PushResult.failed(); |
| 76 | break; |
| 77 | } |
| 78 | if (pushList.size() > 1) { |
| 79 | try { |
| 80 | // 限速 |
| 81 | TimeUnit.MILLISECONDS.sleep(1500); |
| 82 | } catch (InterruptedException ignore) { |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return pushResult; |
| 87 | } |
| 88 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…