Authored by wujiexiang

重试

@@ -23,6 +23,7 @@ import com.yohoufo.order.service.stats.impl.SellerOrderStatsConfiguration; @@ -23,6 +23,7 @@ import com.yohoufo.order.service.stats.impl.SellerOrderStatsConfiguration;
23 import com.yohoufo.order.service.stats.impl.SellerOrderStatsEntry; 23 import com.yohoufo.order.service.stats.impl.SellerOrderStatsEntry;
24 import com.yohoufo.order.service.support.BuyerOrderMetaMapperSupport; 24 import com.yohoufo.order.service.support.BuyerOrderMetaMapperSupport;
25 import com.yohoufo.order.utils.LoggerUtils; 25 import com.yohoufo.order.utils.LoggerUtils;
  26 +import com.yohoufo.order.utils.RetryerUtils;
26 import org.slf4j.Logger; 27 import org.slf4j.Logger;
27 import org.springframework.beans.factory.annotation.Autowired; 28 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component; 29 import org.springframework.stereotype.Component;
@@ -281,20 +282,30 @@ public class BuyerOrderChangeBusinessPostProcessor { @@ -281,20 +282,30 @@ public class BuyerOrderChangeBusinessPostProcessor {
281 private class SellerOrderQuantityStatsBusinessProcessor extends BusinessProcessorAdapter { 282 private class SellerOrderQuantityStatsBusinessProcessor extends BusinessProcessorAdapter {
282 @Override 283 @Override
283 public void appraiseSuccess(BusinessProcessorContext context) { 284 public void appraiseSuccess(BusinessProcessorContext context) {
284 - doProcess(context.buyerOrder); 285 + try {
  286 + RetryerUtils.getRetryer().call(() -> doProcess(context.buyerOrder));
  287 + } catch (Exception e) {
  288 + logger.error("appraiseSuccess happened exception:{}", e);
  289 + //便于异常告警
  290 + throw new RuntimeException(e);
  291 + }
  292 +
285 } 293 }
286 294
287 @Override 295 @Override
288 public void acceptFault(BusinessProcessorContext context) { 296 public void acceptFault(BusinessProcessorContext context) {
289 - doProcess(context.buyerOrder); 297 + try {
  298 + RetryerUtils.getRetryer().call(() -> doProcess(context.buyerOrder));
  299 + } catch (Exception e) {
  300 + logger.error("acceptFault happened exception:{}", e);
  301 + //便于异常告警
  302 + throw new RuntimeException(e);
  303 + }
290 } 304 }
291 305
292 - private void doProcess(BuyerOrder buyerOrder) {  
293 - 306 + private boolean doProcess(BuyerOrder buyerOrder) {
294 BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), buyerOrder.getOrderCode()); 307 BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), buyerOrder.getOrderCode());
295 -  
296 SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(buyerOrderGoods.getSkup()); 308 SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(buyerOrderGoods.getSkup());
297 -  
298 SellerOrderStatsEntry statsEntry = SellerOrderStatsEntry.builder() 309 SellerOrderStatsEntry statsEntry = SellerOrderStatsEntry.builder()
299 .buyerUid(buyerOrder.getUid()) 310 .buyerUid(buyerOrder.getUid())
300 .buyerOrderCode(buyerOrder.getOrderCode()) 311 .buyerOrderCode(buyerOrder.getOrderCode())
@@ -303,8 +314,8 @@ public class BuyerOrderChangeBusinessPostProcessor { @@ -303,8 +314,8 @@ public class BuyerOrderChangeBusinessPostProcessor {
303 .build(); 314 .build();
304 315
305 SellerOrderStatsConfiguration statsConfig = statsConfigurationManager.getStatsConfig(statsEntry); 316 SellerOrderStatsConfiguration statsConfig = statsConfigurationManager.getStatsConfig(statsEntry);
306 -  
307 statsConfigurationManager.getStatsProcessor(statsConfig.getStatsProcessorName()).accept(statsEntry, statsConfig); 317 statsConfigurationManager.getStatsProcessor(statsConfig.getStatsProcessorName()).accept(statsEntry, statsConfig);
  318 + return true;
308 } 319 }
309 } 320 }
310 321
  1 +package com.yohoufo.order.utils;
  2 +
  3 +import com.github.rholder.retry.*;
  4 +import com.google.common.base.Predicates;
  5 +import org.slf4j.Logger;
  6 +import org.slf4j.LoggerFactory;
  7 +
  8 +import java.util.concurrent.TimeUnit;
  9 +
  10 +/**
  11 + * Created by jiexiang.wu on 2019/7/24.
  12 + */
  13 +public class RetryerUtils {
  14 + private static final Logger logger = LoggerFactory.getLogger(RetryerUtils.class);
  15 + //重试
  16 + private static Retryer<Boolean> RETRYER = RetryerBuilder.<Boolean>newBuilder()
  17 + .retryIfException()
  18 + .retryIfResult(Predicates.equalTo(false))
  19 + //重调策略
  20 + .withWaitStrategy(WaitStrategies.fixedWait(200, TimeUnit.MILLISECONDS))
  21 + //尝试次数
  22 + .withStopStrategy(StopStrategies.stopAfterAttempt(3))
  23 + .withRetryListener(new RetryListener() {
  24 + @Override
  25 + public <V> void onRetry(Attempt<V> attempt) {
  26 + if (attempt.hasException()) {
  27 + logger.error("retry happened exception at attempt:{}", attempt.getAttemptNumber());
  28 + }
  29 + }
  30 + }).build();
  31 +
  32 + public static Retryer getRetryer() {
  33 + return RETRYER;
  34 + }
  35 +}