Время ожидания процедуры, определяемое пользователем
Как я могу реализовать механизм тайм-аута для моей пользовательской процедуры 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: запрошенная операция не может быть выполнена, потому что она должна выполняться в транзакции. Убедитесь, что вы заключили операцию в соответствующий шаблон транзакции, и попробуйте еще раз.