package cn.edu.tju.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.ForkJoinPool;

@RestController
public class DeferredController {
@GetMapping("/async")
public DeferredResult<String> getDeferredResult() {
DeferredResult<String> result = new DeferredResult<>(5000L,"fallback");

ForkJoinPool.commonPool().submit(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
result.setResult("i am deferred result...");
});

return result;
}

}
@GetMapping("/async2")
public Callable<String> getCallable() {
return new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "callable returned result";
}
};
}