private static final ThreadPoolExecutor threadPoolExecutor =
new ThreadPoolExecutor(1, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(500));
@Override
protected Boolean doPostAuditResult(CardApplyRecord cardApplyRecord, CardApplyCredit cardApplyCredit) {
CardApplyEventStream cardApplyEventStream = cardApplyEventStreamRepository
.findByApplyIdAndEventId(cardApplyRecord.getApplyId(), CardApplyEventStreamEnum.PUSHED_AUDIT_RESULT_TO_BANK.value);
if (null != cardApplyEventStream) {
LOGGER.info("申请记录{}信审结果已推送到银行,直接返回", cardApplyRecord.getApplyId());
return Boolean.TRUE;
}
String cacheKey = String.format(
CommonConstants.CACHEKEY_PREFIX_WZBANKCARDSERVICE_POSTAUDITRESULT, cardApplyRecord.getApplyId());
if(isPostAuditResultTaskCacheExist(cacheKey)){
LOGGER.info("申请记录{}信审结果正在推送中,直接返回", cardApplyRecord.getApplyId());
return Boolean.FALSE;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
runTask(cardApplyRecord, requestDO, requestXmlDO, auditResultFieldDO);
}
};
try {
cachePostAuditResultTask(cacheKey, Boolean.FALSE.toString());
threadPoolExecutor.execute(runnable);
} catch (RejectedExecutionException e) {
LOGGER.info("申请记录{}信审结果被任务队列丢弃,稍后重试", cardApplyRecord.getApplyId(), e);
}
return Boolean.FALSE;
}
private void runTask(
CardApplyRecord cardApplyRecord, ApplyCardRequestDO requestDO,
ApplyCardRequestXmlDO requestXmlDO, AuditResultFieldDO auditResultFieldDO){
String strLockKey = String.format(CacheConstants.LOCKKEY_UPDATE_POSTAUDITRESUT, cardApplyRecord.getApplyId());
Boolean lockSuccess = Boolean.FALSE;
try {
lockSuccess = distributeLock.tryLock(strLockKey, 200);
if (!lockSuccess) {
throw new IgnoreStackException(org.apache.http.HttpStatus.SC_CONFLICT, "获取更新锁异常");
}
CardApplyEventStream cardApplyEventStream = cardApplyEventStreamRepository.findByApplyIdAndEventId(
cardApplyRecord.getApplyId(), CardApplyEventStreamEnum.PUSHED_AUDIT_RESULT_TO_BANK.value);
if (null != cardApplyEventStream) {
LOGGER.info("申请记录{}信审结果已推送到银行,结束任务直接返回", cardApplyRecord.getApplyId());
return;
}
applyCardDomainService.postAuditResult(requestDO, requestXmlDO, auditResultFieldDO);
cardApplyEventStream = cardApplyEventStreamDomainService
.genCardApplyEventStream4Save(cardApplyRecord.getApplyId(), CardApplyEventStreamEnum.PUSHED_AUDIT_RESULT_TO_BANK);
Integer rowCount;
if (null == cardApplyEventStream.getAutoId()) {
rowCount = cardApplyEventStreamRepository.insert(cardApplyEventStream);
} else {
rowCount = cardApplyEventStreamRepository.update(cardApplyEventStream);
}
if (rowCount == 0) {
LOGGER.error("保存设置申请{}事件流失败, 事件内容为{}", cardApplyRecord.getApplyId(),
CardApplyEventStreamEnum.PUSHED_AUDIT_RESULT_TO_BANK.desc);
throw new IgnoreStackException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "保存事件流失败");
}
String cacheKey = String.format(CommonConstants.CACHEKEY_PREFIX_WZBANKCARDSERVICE_POSTAUDITRESULT,
cardApplyRecord.getApplyId());
enniuJedisCommands.del(cacheKey);
} catch (IgnoreStackException e) {
if (HttpStatus.SC_INTERNAL_SERVER_ERROR == e.getHttpStatus() && StringUtils.equals("999999", e.getMessage())) {
CreditReport creditReport = creditReportRepository.findByApplyId(cardApplyRecord.getApplyId());
if (null != creditReport) {
return;
}
}
throw e;
} finally {
if (lockSuccess) {
distributeLock.unlock(strLockKey);
}
}
}