From 96691ea013a1c20560b715b786513f356ab4ea74 Mon Sep 17 00:00:00 2001 From: "fengyu.wang" Date: Fri, 18 Sep 2020 12:05:38 +0800 Subject: [PATCH] thread pool update --- .../otc/common/config/ApplicationConfig.java | 21 ++++++++++ .../otc/service/impl/OrderServiceImpl.java | 40 +++++++++---------- 2 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/common/config/ApplicationConfig.java diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/common/config/ApplicationConfig.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/common/config/ApplicationConfig.java new file mode 100644 index 0000000..9133af0 --- /dev/null +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/common/config/ApplicationConfig.java @@ -0,0 +1,21 @@ +package com.blockchain.server.otc.common.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.stereotype.Component; + +import java.util.concurrent.ThreadPoolExecutor; + +@Component +public class ApplicationConfig { + @Bean + public ThreadPoolTaskExecutor executor() { + ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); + threadPoolTaskExecutor.setCorePoolSize(5); + threadPoolTaskExecutor.setMaxPoolSize(10); + threadPoolTaskExecutor.setQueueCapacity(50); + threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); + threadPoolTaskExecutor.setThreadNamePrefix("executor-"); + return threadPoolTaskExecutor; + } +} diff --git a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/OrderServiceImpl.java b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/OrderServiceImpl.java index 764e299..32d49e4 100644 --- a/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/OrderServiceImpl.java +++ b/blockchain-server/blockchain-server-otc/src/main/java/com/blockchain/server/otc/service/impl/OrderServiceImpl.java @@ -31,15 +31,12 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.PriorityBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; @Service public class OrderServiceImpl implements OrderService, ITxTransaction { @@ -80,9 +77,10 @@ public class OrderServiceImpl implements OrderService, ITxTransaction { private RedisTemplate redisTemplate; @Autowired private ApplicationContext applicationContext; + @Autowired + private ThreadPoolTaskExecutor executor; private static final BigDecimal DECIMAL_DISH = new BigDecimal("0.1"); //下单金额计算的偏差值 - private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(5, 10, 0, TimeUnit.MINUTES, new PriorityBlockingQueue<>()); @Override public List selectByAdIdAndStatus(String adId, String... status) { @@ -302,25 +300,25 @@ public class OrderServiceImpl implements OrderService, ITxTransaction { YyyfUserDto yyyfUserDto = YyyfUserDtoUtils.getYyyfUserDtoByUserId(userId, redisTemplate); - ResultDTO resultDTO = yyyyfMoneyFeign.resetWallet(yyyfUserDto.getAssessUserId(), payType,BigDecimal.ZERO.subtract(order.getTurnover())); + ResultDTO resultDTO = yyyyfMoneyFeign.resetWallet(yyyfUserDto.getAssessUserId(), payType, BigDecimal.ZERO.subtract(order.getTurnover())); - if(resultDTO.getCode()==200 && resultDTO.getData()) { - //更新订单状态 - receiptOrPayUpdateOrder(order, UserHandleConstants.PAY); - //记录用户操作 - insertUserHandleLog(userId, order.getOrderNumber(), UserHandleConstants.PAY); - //确认付款发送提示消息 - sendNewOrderMsg(JgMsgEnums.CONFIRM_BUYER_SELL.getName(), JgMsgEnums.CONFIRM_BUYER_BUY.getName(), - order.getSellUserId(), order.getBuyUserId(), order.getId()); - //发送手机消息通知 - pushToSingle(order.getSellUserId(), order.getId(), PushEnums.OTC_ORDER_PAY.getPushType()); - }else{ + if (resultDTO.getCode() == 200 && resultDTO.getData()) { + //更新订单状态 + receiptOrPayUpdateOrder(order, UserHandleConstants.PAY); + //记录用户操作 + insertUserHandleLog(userId, order.getOrderNumber(), UserHandleConstants.PAY); + //确认付款发送提示消息 + sendNewOrderMsg(JgMsgEnums.CONFIRM_BUYER_SELL.getName(), JgMsgEnums.CONFIRM_BUYER_BUY.getName(), + order.getSellUserId(), order.getBuyUserId(), order.getId()); + //发送手机消息通知 + pushToSingle(order.getSellUserId(), order.getId(), PushEnums.OTC_ORDER_PAY.getPushType()); + } else { throw new OtcException(OtcEnums.FREEBALANCE_NOT_ENOUGH); } if (BooleanUtils.isTrue(ad.getBuiltIn())) { - EXECUTOR_SERVICE.execute(() -> applicationContext.getBean(OrderService.class).receipt(order.getSellUserId(), orderId, "123456")); + executor.execute(() -> applicationContext.getBean(OrderService.class).receipt(order.getSellUserId(), orderId, "123456")); } } @@ -343,8 +341,8 @@ public class OrderServiceImpl implements OrderService, ITxTransaction { checkOrderCanHandle(order, userId, UserHandleConstants.RECEIPT); YyyfUserDto yyyfUserDto = YyyfUserDtoUtils.getYyyfUserDtoByUserId(userId, redisTemplate); - ResultDTO resultDTO = yyyyfMoneyFeign.resetWallet(yyyfUserDto.getAssessUserId(),order.getOrderPayType(),order.getTurnover()); - if(resultDTO.getCode()==200 && resultDTO.getData()) { + ResultDTO resultDTO = yyyyfMoneyFeign.resetWallet(yyyfUserDto.getAssessUserId(), order.getOrderPayType(), order.getTurnover()); + if (resultDTO.getCode() == 200 && resultDTO.getData()) { //更新订单状态 @@ -362,7 +360,7 @@ public class OrderServiceImpl implements OrderService, ITxTransaction { order.getSellUserId(), order.getBuyUserId(), order.getId()); //发送手机消息通知 pushToSingle(order.getBuyUserId(), order.getId(), PushEnums.OTC_ORDER_RECEIPT.getPushType()); - }else{ + } else { throw new OtcException(OtcEnums.ERROR_IN_WALLET_CALL); } }