这是我的代码片段。我必须使用ForkJoin进行并行调用,但我的throws堆栈溢出,甚至没有到达服务调用。
请求:
@NoArgsConstructor
@Getter
@Setter
public class Request{
@JsonProperty("id")
private String id;
public Request id(String id){
this.id=id;
return this;
}
public static Request getRequest(AnotherRequest anotherReq){
return new Request().id(anotherReq.identity);
}
public String getJson() throws Exception {
return new ObjectMapper().writeValueasString(this);
}
}
MyCallable:
@AllargsConstructor
MyCallable implements Callable<Response> {
private Service service;
private Request request;
public Response call () throws Exception{
return service.callWebservice(this.request.getJson());
}
}
主要方法:
@Autowired
private Service service;
List<MyCallable> jobs = new ArrayList<MyCallable>()
anotherRequestSS.forEach(anotherRequest->{
jobs.add(new MyCallable(Request.getRequest(anotherRequest),service);
}
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
pool.invokeAll(jobs);
此代码进入无限循环,这意味着getJson
被调用无限次,导致堆栈溢出。它甚至没有达到invokeAll()
的点。这可能是什么原因造成的?
列表大小anotherRequestSS
为2。
问题是fasterxml将方法"getJson(("解释为必须包含在生成的JSON中的属性。
将你的类重写为
@NoArgsConstructor
@Getter
@Setter
public class Request{
@JsonProperty("id")
private String id;
public Request id(String id){
this.id=id;
return this;
}
public static Request getRequest(AnotherRequest anotherReq){
return new Request().id(anotherReq.identity);
}
public String asJson() throws Exception {
return new ObjectMapper().writeValueAsString(this);
}
}
以及相应的MyCallable
@AllargsConstructor
MyCallable implements Callable<Response> {
private Service service;
private Request request;
public Response call () throws Exception{
return service.callWebservice(this.request.asJson());
}
}