private static final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
private static volatile ScheduledFuture<?> scheduledFuture;
private static final long INITIAL_DELAY = 0L;
private static final long PERIOD = 1L;
@Override
public ResponseDto start(RequestDto requestDto) {
String path = gradingUrlHuawei + "/mock/start";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(path, requestDto, String.class);
String body = responseEntity.getBody();
this.schedulerStart(() -> gradingResultService.gradingImage());
return new ResponseDto();
}
private void schedulerStart(Runnable command) {
if (scheduledFuture == null) {
synchronized (this) {
if (scheduledFuture == null) {
System.out.println("判级开始");
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(command, INITIAL_DELAY, PERIOD, TimeUnit.SECONDS);
}
}
}
}
private void schedulerStop() {
if (scheduledFuture != null) {
synchronized (this) {
if (scheduledFuture != null) {
System.out.println("判级结束");
scheduledFuture.cancel(false);
scheduledFuture = null;
}
}
}
}
@Override
public ResponseDto stop(RequestDto requestDto) {
String path = gradingUrlHuawei + "/mock/stop";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(path, requestDto, String.class);
String body = responseEntity.getBody();
this.schedulerStop();
return null;
}