验证中...
使用线程池与CountDownLatch多线程提升系统性能
原始数据 复制代码
https://blog.csdn.net/exceptional_derek/article/details/52234640
private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
@Override
public void execute(ShardingContext shardingContext) {
List<MerchantOrder> orders = merchantOrderDao.selectWaitRepaymentMerchantOrder();
if (orders != null && orders.size() > 0) {
int i = 1;
CountDownLatch latch = new CountDownLatch(orders.size());
try {
for (MerchantOrder order : orders) {
try {
String seqNo = StringUtils.leftPad(String.valueOf(i), 6, '0');
EXECUTOR.execute(new PaybackNotifyTask(latch, seqNo, order));
i++;
Thread.sleep(5000);
} catch (InterruptedException e) {
continue;
}
}
latch.await();
} catch (InterruptedException e) {
LOGGER.error("payback notify task error: " + e.getMessage());
}
}
}
class PaybackNotifyTask implements Runnable {
private CountDownLatch latch;
private MerchantOrder order;
private String seqNo;
public PaybackNotifyTask(CountDownLatch latch, String seqNo, MerchantOrder order) {
this.latch = latch;
this.seqNo = seqNo;
this.order = order;
}
@Override
public void run() {
if (order == null || StringUtils.isBlank(order.getOrderNO()) || order.getProduct() == null
|| order.getProduct().getMerchant() == null || StringUtils.isBlank(order.getProduct().getMerchant().getAccountId())) {
return;
}
try {
String channel = "000002";
String orderNo = order.getOrderNO();
String batchTxDate = orderNo.substring(3, 11);
String batchNo = StringUtils.substring(orderNo, StringUtils.length(orderNo) - 6);
JSONObject invokeResult = batchQuery(channel, batchTxDate, batchNo, seqNo);
if (invokeResult != null && invokeResult.containsKey("retCode") && StringUtils.equals(invokeResult.getString("retCode"), BankResponseEnum.SUCCESS.code())){
String sucCounts = invokeResult.getString("sucCounts");
if(StringUtils.isNotBlank(sucCounts) && Integer.parseInt(sucCounts) > 0){
JSONObject json = new JSONObject();
json.put("retCode", invokeResult.getString("retCode"));
json.put("txDate", batchTxDate);
json.put("txTime", orderNo.substring(11, 17));
json.put("seqNo", orderNo.substring(17, 23));
json.put("channel", channel);
json.put("batchNo", batchNo);
json.put("failCounts", invokeResult.getString("failCounts"));
json.put("sucCounts", invokeResult.getString("sucCounts"));
json.put("acqRes", order.getProduct().getId() + COLON + "0" + COLON + order.getProduct().getMerchant().getAccountId() + COLON + "0");
String url = properties.getNotifyUrl() + properties.getPaybackServiceNotify();
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("bgData", json.toString()));
String result = post(url, nvps, UTF8);
LOGGER.info("payback notify result: " + result);
}
}
} finally {
latch.countDown();
}
}
}

评论列表( 0 )

你可以在登录后,发表评论

8_float_left_people 8_float_left_close