如何为我的neo4j 用户定义过程实现超时机制?
我的代码就是这样。
@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
}
我尝试使用java.util.concurrent.Callable
和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);
我明白了ExecutionException
org.neo4j.kernel.impl.core.ThreadToStatementContextBridge$BridgeNotInTransactionException: 请求的操作无法执行,因为它必须在事务中执行。确保您将操作包装在适当的事务样板中,然后重试。