public ExecutorService getThreadPool(){ return new ThreadPoolExecutor(75, 125, 180000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(450), new ThreadPoolExecutor.CallerRunsPolicy()); }
@Service public class InsuranceVerificationServiceImpl implements InsuranceVerificationService { private static final Logger logger = LoggerFactory.getLogger(InsuranceVerificationServiceImpl.class); @Override public TaskResponseModel
核保常规校验服务类:UnderwritingCheckServiceImpl,假设耗时50ms
@Service public class UnderwritingCheckServiceImpl implements UnderwritingCheckService { private static final Logger logger = LoggerFactory.getLogger(UnderwritingCheckServiceImpl.class); @Override public TaskResponseModel
核保外部调用服务类:ExternalCallServiceImpl,假设耗时200ms
@Service public class ExternalCallServiceImpl implements ExternalCallService { private static final Logger logger = LoggerFactory.getLogger(ExternalCallServiceImpl.class); @Override public TaskResponseModel
试算服务类:TrialCalculationServiceImpl,假设耗时50ms
@Service public class TrialCalculationServiceImpl implements TrialCalculationService { private static final Logger logger = LoggerFactory.getLogger(TrialCalculationServiceImpl.class); @Override public TaskResponseModel
@Data @ToString @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode @Accessors(chain = true) public class TaskResponseModel implements Serializable { private String key; //唯一调用标志 private String resultCode; //结果码 private String resultMessage; //结果信息 private T data; //业务处理结果
public static TaskResponseModel
注:
key为这次调用的唯一标识,由调用者传进来
resultCode结果码,200为成功,400表示有异常
resultMessage信息,表示不成功或者异常信息
data业务处理结果,如果成功的话
这些服务类都是单例模式
要使用用CompletionService的话,需要创建实现了Callable接口的线程
投保Callable:
@Data @AllArgsConstructor public class InsuranceVerificationCommand implements Callable> { private String key; private PolicyModel policyModel; private final InsuranceVerificationService insuranceVerificationService; @Override public TaskResponseModel call() throws Exception { return insuranceVerificationService.insuranceCheck(key, policyModel); } }
核保常规校验Callable:
@Data @AllArgsConstructor public class UnderwritingCheckCommand implements Callable> { private String key; private PolicyModel policyModel; private final UnderwritingCheckService underwritingCheckService; @Override public TaskResponseModel call() throws Exception { return underwritingCheckService.underwritingCheck(key, policyModel); } }
核保外部调用Callable:
@Data @AllArgsConstructor public class ExternalCallCommand implements Callable> { private String key; private Insured insured; private final ExternalCallService externalCallService; @Override public TaskResponseModel call() throws Exception { return externalCallService.externalCall(key, insured); } }
试算调用Callable:
@Data @AllArgsConstructor public class TrialCalculationCommand implements Callable> { private String key; private Risk risk; private final TrialCalculationService trialCalculationService; @Override public TaskResponseModel call() throws Exception { return trialCalculationService.trialCalc(key, risk); } }
注:
每一次调用,需要创建这4种Callable
返回统一接口TaskResopnseModel
异步执行的类:TaskExecutor
@Component public class TaskExecutor { private static final Logger logger = LoggerFactory.getLogger(TaskExecutor.class); //线程池 private final ExecutorService executorService;
public TaskExecutor(ExecutorService executorService) { this.executorService = executorService; }
//异步执行,获取所有结果后返回 public List> execute(List>> commands) { //创建异步执行对象 CompletionService> completionService = new ExecutorCompletionService<>(executorService); for (Callable> command : commands) { completionService.submit(command); } //获取所有异步执行线程的结果 int taskCount = commands.size(); List> params = new ArrayList<>(taskCount); try { for (int i = 0; i < taskCount; i++) { Future> future = completionService.take(); params.add(future.get()); } } catch (InterruptedException | ExecutionException e) { //异常处理 params.clear(); params.add(TaskResponseModel.failure().setKey("error").setResultMessage("异步执行线程错误")); } //返回,如果执行中发生error, 则返回相应的key值:error return params; } }
@RestController public class CompletionServiceController { //投保key private static final String INSURANCE_KEY = "insurance_"; //核保key private static final String UNDERWRITING_KEY = "underwriting_"; //外部调用key private static final String EXTERNALCALL_KEY = "externalcall_"; //试算key private static final String TRIA_KEY = "trial_";
private static final Logger logger = LoggerFactory.getLogger(CompletionServiceController.class);
private final ExternalCallService externalCallService; private final InsuranceVerificationService insuranceVerificationService; private final TrialCalculationService trialCalculationService; private final UnderwritingCheckService underwritingCheckService; private final TaskExecutor taskExecutor;
/**
* 要执行的算法,返回结果v
*/
public interface Computable<A, V> {
public V comput(final A arg);
}
/**
* 用于缓存数据
*/
public class Memoizer<A, V> implements Computable<A,