Authored by qinchao

鉴定分账改成异步

@@ -31,6 +31,8 @@ import com.yohoufo.order.service.IPaymentService; @@ -31,6 +31,8 @@ import com.yohoufo.order.service.IPaymentService;
31 import com.yohoufo.order.service.cache.CacheCleaner; 31 import com.yohoufo.order.service.cache.CacheCleaner;
32 import com.yohoufo.order.service.cache.CacheKeyBuilder; 32 import com.yohoufo.order.service.cache.CacheKeyBuilder;
33 import com.yohoufo.order.service.proxy.InBoxFacade; 33 import com.yohoufo.order.service.proxy.InBoxFacade;
  34 +import com.yohoufo.order.service.proxy.InBoxThreadFactory;
  35 +import com.yohoufo.order.utils.PubThreadFactory;
34 import com.yohoufo.order.utils.SmsHelper; 36 import com.yohoufo.order.utils.SmsHelper;
35 import org.slf4j.Logger; 37 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory; 38 import org.slf4j.LoggerFactory;
@@ -40,6 +42,10 @@ import org.springframework.stereotype.Service; @@ -40,6 +42,10 @@ import org.springframework.stereotype.Service;
40 import java.math.BigDecimal; 42 import java.math.BigDecimal;
41 import java.util.Arrays; 43 import java.util.Arrays;
42 import java.util.List; 44 import java.util.List;
  45 +import java.util.concurrent.ArrayBlockingQueue;
  46 +import java.util.concurrent.ExecutorService;
  47 +import java.util.concurrent.ThreadPoolExecutor;
  48 +import java.util.concurrent.TimeUnit;
43 49
44 /** 50 /**
45 * Created by chenchao on 2018/9/27. 51 * Created by chenchao on 2018/9/27.
@@ -49,6 +55,8 @@ public class AppraiseService { @@ -49,6 +55,8 @@ public class AppraiseService {
49 55
50 private static final Logger LOGGER = LoggerFactory.getLogger(AppraiseService.class); 56 private static final Logger LOGGER = LoggerFactory.getLogger(AppraiseService.class);
51 57
  58 + private ExecutorService executorService = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new PubThreadFactory("appraise-processor"));
  59 +
52 @Autowired 60 @Autowired
53 private IExpressInfoService expressInfoService; 61 private IExpressInfoService expressInfoService;
54 62
@@ -132,11 +140,18 @@ public class AppraiseService { @@ -132,11 +140,18 @@ public class AppraiseService {
132 //先校验,提前把错误抛出 140 //先校验,提前把错误抛出
133 paymentService.transferMonCheck(tmReq); 141 paymentService.transferMonCheck(tmReq);
134 //再分账 142 //再分账
  143 + /**
135 boolean transferSuccessFlag=transferService.transfer(tmReq); 144 boolean transferSuccessFlag=transferService.transfer(tmReq);
136 if(!transferSuccessFlag){ 145 if(!transferSuccessFlag){
137 apiResponse.setCode(400); 146 apiResponse.setCode(400);
138 apiResponse.setMessage("保证金分账失败"); 147 apiResponse.setMessage("保证金分账失败");
139 } 148 }
  149 + */
  150 + //改成异步的分账,不关心分账执行结果
  151 + executorService.execute(()->{
  152 + LOGGER.info("in appraiseSuccess,begin transfer async");
  153 + transferService.transfer(tmReq);
  154 + });
140 155
141 /** 156 /**
142 * 退保证金给卖家 157 * 退保证金给卖家
@@ -267,11 +282,18 @@ public class AppraiseService { @@ -267,11 +282,18 @@ public class AppraiseService {
267 //先校验,提前把错误抛出 282 //先校验,提前把错误抛出
268 paymentService.transferMonCheck(tmReq); 283 paymentService.transferMonCheck(tmReq);
269 //再分账 284 //再分账
  285 + /*
270 boolean transferSuccessFlag=transferService.transfer(tmReq); 286 boolean transferSuccessFlag=transferService.transfer(tmReq);
271 if(!transferSuccessFlag){ 287 if(!transferSuccessFlag){
272 apiResponse.setCode(400); 288 apiResponse.setCode(400);
273 apiResponse.setMessage("保证金分账失败"); 289 apiResponse.setMessage("保证金分账失败");
274 } 290 }
  291 + */
  292 + //改成异步的分账,不关心分账执行结果
  293 + executorService.execute(()->{
  294 + LOGGER.info("in appraiseFail,begin transfer async");
  295 + transferService.transfer(tmReq);
  296 + });
275 297
276 BigDecimal goodsMoney = buyerOrder.getAmount(); 298 BigDecimal goodsMoney = buyerOrder.getAmount();
277 PaymentRequest req = PaymentRequest.builder().uid(buyerUid) 299 PaymentRequest req = PaymentRequest.builder().uid(buyerUid)
  1 +package com.yohoufo.order.utils;
  2 +
  3 +import java.util.concurrent.ThreadFactory;
  4 +import java.util.concurrent.atomic.AtomicInteger;
  5 +
  6 +/**
  7 + * 自定义java api线程池内工作线程工厂
  8 + * 指定生成线程名称:pool-XXX-thread-[num]
  9 + */
  10 +public class PubThreadFactory implements ThreadFactory {
  11 +
  12 + private static final AtomicInteger poolNumber = new AtomicInteger(1);
  13 + private final AtomicInteger threadNumber = new AtomicInteger(1);
  14 + private final String namePrefix;
  15 + private final ThreadGroup group;
  16 +
  17 + public PubThreadFactory(String nameProcessor) {
  18 + SecurityManager s = System.getSecurityManager();
  19 + group = (s != null) ? s.getThreadGroup() :
  20 + Thread.currentThread().getThreadGroup();
  21 + namePrefix = "pool-"+nameProcessor+"-" +
  22 + poolNumber.getAndIncrement() +
  23 + "-thread-";
  24 + }
  25 +
  26 +
  27 + @Override
  28 + public Thread newThread(Runnable r) {
  29 + Thread t = new Thread(group, r,
  30 + namePrefix + threadNumber.getAndIncrement(),
  31 + 0);
  32 + if (t.isDaemon())
  33 + t.setDaemon(true);
  34 + if (t.getPriority() != Thread.NORM_PRIORITY)
  35 + t.setPriority(Thread.NORM_PRIORITY);
  36 + return t;
  37 + }
  38 +}