Authored by qinchao

鉴定分账改成异步

... ... @@ -31,6 +31,8 @@ import com.yohoufo.order.service.IPaymentService;
import com.yohoufo.order.service.cache.CacheCleaner;
import com.yohoufo.order.service.cache.CacheKeyBuilder;
import com.yohoufo.order.service.proxy.InBoxFacade;
import com.yohoufo.order.service.proxy.InBoxThreadFactory;
import com.yohoufo.order.utils.PubThreadFactory;
import com.yohoufo.order.utils.SmsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -40,6 +42,10 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by chenchao on 2018/9/27.
... ... @@ -49,6 +55,8 @@ public class AppraiseService {
private static final Logger LOGGER = LoggerFactory.getLogger(AppraiseService.class);
private ExecutorService executorService = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new PubThreadFactory("appraise-processor"));
@Autowired
private IExpressInfoService expressInfoService;
... ... @@ -132,11 +140,18 @@ public class AppraiseService {
//先校验,提前把错误抛出
paymentService.transferMonCheck(tmReq);
//再分账
/**
boolean transferSuccessFlag=transferService.transfer(tmReq);
if(!transferSuccessFlag){
apiResponse.setCode(400);
apiResponse.setMessage("保证金分账失败");
}
*/
//改成异步的分账,不关心分账执行结果
executorService.execute(()->{
LOGGER.info("in appraiseSuccess,begin transfer async");
transferService.transfer(tmReq);
});
/**
* 退保证金给卖家
... ... @@ -263,15 +278,22 @@ public class AppraiseService {
TransferCase transferCase = TransferCase.APPRAISE_FAIL;
TransferMoneyRequest tmReq = TransferMoneyRequest.builder().buyerOrderCode(orderCode)
.type(transferCase.getCode()).transferCase(transferCase).build();
//先校验,提前把错误抛出
paymentService.transferMonCheck(tmReq);
//再分账
/*
boolean transferSuccessFlag=transferService.transfer(tmReq);
if(!transferSuccessFlag){
apiResponse.setCode(400);
apiResponse.setMessage("保证金分账失败");
}
*/
//改成异步的分账,不关心分账执行结果
executorService.execute(()->{
LOGGER.info("in appraiseFail,begin transfer async");
transferService.transfer(tmReq);
});
BigDecimal goodsMoney = buyerOrder.getAmount();
PaymentRequest req = PaymentRequest.builder().uid(buyerUid)
... ...
package com.yohoufo.order.utils;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 自定义java api线程池内工作线程工厂
* 指定生成线程名称:pool-XXX-thread-[num]
*/
public class PubThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
private final ThreadGroup group;
public PubThreadFactory(String nameProcessor) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-"+nameProcessor+"-" +
poolNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(true);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
... ...