如何編寫異步休息客戶端?我的控制器(不知道它是不是異步)。異步REST客戶端
@RequestMapping(method = RequestMethod.GET, value = "/get/all")
@ResponseBody
public Callable<CustomersListDTO> getAllCustomers() {
return new Callable<CustomersListDTO>() {
@Override
public CustomersListDTO call() throws Exception {
Thread.sleep(2000);
return customerService.getAllCustomers();
}
};
}
我同步其餘客戶方法:
public Response get_all_customers() {
ResponseEntity<CustomersListDTO> response;
try {
response = restTemplate.getForEntity(getMethodURI(ServiceExplanation.GET_ALL_CUSTOMERS),
CustomersListDTO.class);
message = "Customers obtained successfully!";
} catch (HttpServerErrorException ex) {
message = "ERROR: " + ex.getMessage() + " - " + ex.getResponseBodyAsString();
} catch (HttpClientErrorException ex) {
message = "ERROR: " + ex.getMessage() + " - " + ex.getResponseBodyAsString();
} catch (RestClientException ex) {
message = checkIfServerOrInternetDown();
}
return formResponse(message, response);
}
如何使它異步?在SERVER獲取數據並在以後返回找到的數據時,CLIENT如何繼續執行其他任務?
閱讀有關併發性。 http://docs.oracle.com/javase/tutorial/essential/concurrency/ – gouki
請看以下鏈接https://www.adayinthelifeof.nl/2011/06/02/asynchronous-operations-in-rest/ – sasankad