我有一个带有Spring Framework的Google AppEngine应用程序,它必须使用List发出一个post请求到另一个暴露REST API的Spring Boot应用程序 . 我正在使用RestTemplate来调用api .
在GAE应用程序内我做:
restTemplate.postForObject(NEO4J_URL_VERTEX, vertices, List.class);
Spring Boot应用程序连接到本地Neo4j数据库 . 在 NEO4J_URL_VERTEX
我有以下方法将顶点保存到db:
@Transactional
@RequestMapping(value = "vertex/all", method = RequestMethod.POST, consumes = "application/json")
public void addVertex(@RequestBody List<Vertex> vertices) {
for (Vertex v : vertices) {
Vertex old = vertexService.findByEmail(v.getEmail());
if (old == null) {
vertexService.create(v);
}
}
}
该场景适用于具有较小Vertex对象的小型列表 . 但大多数情况下我会收到以下错误:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8081/api/v1/vertex/all":Socket operation timed out: The API call remote_socket.Receive() took too long to respond and was cancelled.; nested exception is java.net.SocketException: Socket operation timed out: The API call remote_socket.Receive() took too long to respond and was cancelled.
[INFO] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:580)
[INFO] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
[INFO] at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:330)
Important to note here is that the vertices are saved to the Neo4j database. 没有问题 . GAE应用程序使用对象列表发出post请求,但在一段时间后(aprox 1秒)关闭套接字,因为显然太长了 . 但是使用Neo4j的Spring应用程序继续编写顶点,它们最终都保存到数据库中 .
我已经尝试在GAE应用程序中迭代List并为每个Vertex进行POST,但是如果Vertex对象很大(即有很多队友),我会得到同样的错误 . Vertex对象定义为:
public class Vertex {
private Long id;
private String name;
@Index(unique=true)
private String email;
@Index(unique=true)
private Long universeUserId;
@Relationship(type="WORKS_WITH", direction = Relationship.OUTGOING)
private List<Edge> teammates;
//getters,setters
}
读了一下后,我尝试通过以下方式配置套接字超时:
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(50000);
factory.setConnectTimeout(50000);
return factory;
}
但这没有帮助 . 此外,发布顶点的GAE应用程序的方法是从Appengine调用的
任务队列
它有10分钟的时间限制,所以这不是问题 . 目前,两个应用程序都在localhost上运行 . 任何帮助深表感谢!