How can I implement a timeout mechanism for my neo4j user-defined procedure?
My code is simply this.
@Procedure(value = "nameOfMyProcedure", mode = Mode.WRITE)
@Description("finds the minimal sub-graph from given nodes")
public Stream<Output> nameOfMyProcedure(@Name("p1") Long p1, @Name("p2") Long p2, ...) {
// there are some codes here. I want to make a timeout mechanism for codes inside here
}
I tried using java.util.concurrent.Callable
and java.util.concurrent.ExecutorService
AdvancedQuery that = this;
Callable<Output> receiveResponse = () -> {
// put all the codes inside this callable
return output;
}
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Output> future = executor.submit(receiveResponse);
Output result = null;
try {
result = future.get(10000, TimeUnit.MILLISECONDS);
log.info("Completed successfully");
} catch (InterruptedException | ExecutionException e) {
log.info("unexpected error: ", e.getMessage());
} catch (TimeoutException e) {
log.info("Timed out. Cancelling the runnable...");
future.cancel(true);
}
executor.shutdown();
if (result == null) {
return Stream.of(new Output());
}
return Stream.of(result);
I get this ExecutionException
org.neo4j.kernel.impl.core.ThreadToStatementContextBridge$BridgeNotInTransactionException: The requested operation cannot be performed, because it has to be performed in a transaction. Ensure you are wrapping your operation in the appropriate transaction boilerplate and try again.