List> returnFuture = new ArrayList<>();
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("openAccount-pool-%d").build();
ExecutorService pool = new ThreadPoolExecutor(2, 4, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue(), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
for(int i=0;i<10;i++){
returnFuture.add(pool.submit(new OpenAccountCallable(pcl,record, withholdService,oaInput)));
}
pool.shutdown();
for (Future future : returnFuture) {
OpenAccount record = new OpenAccount();
try {
record = future.get();
} catch (Exception e) {
e.printStackTrace();
}
System.out.print(record.toString());
}
public static class OpenAccountCallable implements Callable{
private PayChannel payChannel;
private OpenAccount openAccount;
private IWithholdService withholdService;
private Object object;
private OpenAccountCallable(PayChannel payChannel,OpenAccount openAccount ,IWithholdService withholdService,Object object){
super();
this.payChannel = payChannel;
this.openAccount = openAccount;
this.withholdService = withholdService;
this.object = object;
}
@Override
public OpenAccount call() throws Exception {
OpenAccount oat = new OpenAccount();
if(this.openAccount!=null){
oat = this.openAccount;
}
if(this.object instanceof OpenAccountInput){
}else if(this.object instanceof OpenAccountVerifyInput){
OpenAccountVerifyInput input = (OpenAccountVerifyInput) this.object;
logger.info("渠道{}短信成功之后开户操作==request:{}",payChannel.getChannelName(), input.toString());
OpenAccountVerifyOutput outPut;
//尽量捕获异常,否则获取返回的内容会有问题returnFuture
try {
outPut = withholdService.openAccountVerify(input);
logger.info("渠道{}短信成功之后开户操作==response:{}",payChannel.getChannelName(), outPut.toString());
if(WithholdConstant.WITHHOLD_RESPCODE_SUCCESS.equals(outPut.getRespCode())){
oat.setOpenStatus(Constants.COMMON_STATUS_SUCCESS);
}else if(WithholdConstant.WITHHOLD_RESPCODE_PROCESSING.equals(outPut.getRespCode())){
oat.setOpenStatus(Constants.COMMON_STATUS_PROCESS);
}else{
oat.setOpenStatus(Constants.COMMON_STATUS_FAIL);
}
} catch (Exception e) {
oat.setOpenStatus(Constants.COMMON_STATUS_FAIL);
logger.info("短信成功之后开户操作异常:{}",e.getMessage());
}
oat.setSerialNo(input.getSerialNo());
}
oat.setChannelId(this.payChannel.getId());
return oat;
}
}