通过http发送一个post请求,但是可能存在数据量比较大或者时延比较高的场景,
这时候就需要发送方手工来获取响应,并且有个超时时间,比如5分钟,在快到5分钟的时候获取一次,
获取不到则标记超时或者失败;
这个有什么思路或者API吗
当前使用的http调用工具类是这样的:
public static String sendPostRequest(String url, String param) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> formEntity = new HttpEntity<String>(param, headers);
return restTemplate.postForObject(url, formEntity, String.class);
}
个人想法,如果是要等待结果响应后才返回给客户端的情况下,这样处理思路应该能满足你的需求,直接上代码吧
ExecutorService executor = Executors.newFixedThreadPool(1); try { Callable<List<xxx>> call = new Callable<List<xxx>>() { @Override public List<Voladetail> call() throws Exception { return xxxService.xxxfunction(xxx); } }; Future<List<xxx>> future = executor.submit(call); //300秒超时 list = future.get(1000 * 300, TimeUnit.MILLISECONDS); } catch (TimeoutException e){ log.error("超时异常", e); } catch (Exception e) { log.error("异常", e); } finally { executor.shutdown(); }
网上稍微搜下就有了,http请求可以设置超时时间,超时的话会报错