@Slf4j
public class PortraitImport implements Callable<Response> {
private TemporaryWorkerService temporaryWorkerService;
private Semaphore semaphore;
private MultipartFile file;
private Integer type;
public PortraitImport(Semaphore semaphore, MultipartFile file, Integer type, TemporaryWorkerService temporaryWorkerService) {
this.semaphore = semaphore;
this.file = file;
this.type = type;
this.temporaryWorkerService = temporaryWorkerService;
}
@Override
public Response call() throws Exception {
Response response = null;
try {
semaphore.acquire();
response = temporaryWorkerService.empPortraitImport(file, type);
} catch (InterruptedException e) {
log.error("人像录入失败:{}", file.getOriginalFilename());
return Response.ofFail(ExceptionCode.VALID_EXCEPTION_CODE, "人像录入失败");
} finally {
semaphore.release();
}
return response;
}
}
private static ExecutorService executorService = null;
@Value("${ai.baidu.maxThread:2}")
public void setMaxThread(Integer maxThread) {
semaphore = new Semaphore(maxThread);
executorService = Executors.newFixedThreadPool(maxThread);
}
Future<Response> submit = executorService.submit(new PortraitImport(semaphore, file, type, temporaryWorkerService));