Skip to content

Commit 7d97024

Browse files
Наводников Максим АлександровичНаводников Максим Александрович
authored andcommitted
timeout for parallel processed batches
1 parent 961ebe8 commit 7d97024

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/main/java/com/googlecode/jsonrpc4j/JsonRpcBasicServer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.*;
2121
import java.util.concurrent.ExecutorService;
2222
import java.util.concurrent.Future;
23+
import java.util.concurrent.TimeUnit;
2324
import java.util.regex.Pattern;
2425

2526
import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.ERROR_NOT_HANDLED;
@@ -78,6 +79,7 @@ public class JsonRpcBasicServer {
7879
private boolean shouldLogInvocationErrors = true;
7980
private List<JsonRpcInterceptor> interceptorList = new ArrayList<>();
8081
private ExecutorService batchExecutorService = null;
82+
private long parallelBatchProcessingTimeout;
8183

8284
/**
8385
* Creates the server with the given {@link ObjectMapper} delegating
@@ -389,7 +391,7 @@ private void handleRethrowException(JsonResponse response, JsonResponse singleJs
389391
private JsonResponse getSingleJsonResponse(Map.Entry<Object, Future<JsonResponse>> responseFuture) {
390392
JsonResponse response;
391393
try {
392-
response = responseFuture.getValue().get();
394+
response = responseFuture.getValue().get(parallelBatchProcessingTimeout, TimeUnit.MILLISECONDS);
393395
} catch (Throwable t) {
394396
JsonError jsonError = new JsonError(INTERNAL_ERROR.code, t.getMessage(), t.getClass().getName());
395397
return createResponseError(VERSION, responseFuture.getKey(), jsonError);
@@ -1101,8 +1103,12 @@ public void setShouldLogInvocationErrors(boolean shouldLogInvocationErrors) {
11011103
public void setBatchExecutorService(ExecutorService batchExecutorService) {
11021104
this.batchExecutorService = batchExecutorService;
11031105
}
1104-
1105-
private static class ErrorObjectWithJsonError {
1106+
1107+
public void setParallelBatchProcessingTimeout(long parallelBatchProcessingTimeout) {
1108+
this.parallelBatchProcessingTimeout = parallelBatchProcessingTimeout;
1109+
}
1110+
1111+
private static class ErrorObjectWithJsonError {
11061112
private final ObjectNode node;
11071113
private final JsonError error;
11081114

src/main/java/com/googlecode/jsonrpc4j/spring/AbstractJsonServiceExporter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ abstract class AbstractJsonServiceExporter extends RemoteExporter implements Ini
3535
private String contentType = null;
3636
private List<JsonRpcInterceptor> interceptorList;
3737
private ExecutorService batchExecutorService = null;
38+
private long parallelBatchProcessingTimeout;
3839

3940
/**
4041
* {@inheritDoc}
@@ -73,6 +74,7 @@ public void afterPropertiesSet() throws Exception {
7374
jsonRpcServer.setConvertedParameterTransformer(convertedParameterTransformer);
7475
jsonRpcServer.setShouldLogInvocationErrors(shouldLogInvocationErrors);
7576
jsonRpcServer.setBatchExecutorService(batchExecutorService);
77+
jsonRpcServer.setParallelBatchProcessingTimeout(parallelBatchProcessingTimeout);
7678

7779
if (contentType != null) {
7880
jsonRpcServer.setContentType(contentType);

src/test/java/com/googlecode/jsonrpc4j/server/JsonRpcServerParallelBatchProcessingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void setup() {
2020
50,
2121
1000,
2222
TimeUnit.MILLISECONDS,
23-
new ArrayBlockingQueue<Runnable>(50));
23+
new ArrayBlockingQueue<>(50));
2424
jsonRpcServer.setBatchExecutorService(threadPoolExecutor);
2525
}
2626
}

0 commit comments

Comments
 (0)