neo4j 连接超时
在过去的几周中,我一直在花一些业余时间来创建一个应用程序,该应用程序从Open Roads数据生成运行路线-当然,已转换并导入了Neo4j!
我创建了一个用户定义的过程,该过程结合了几个最短路径查询,但是如果它们花费的时间太长,我想退出所有这些最短路径搜索。 我的代码没有超时如下所示:
StandardExpander orderedExpander = new OrderedByTypeExpander().add( RelationshipType.withName( "CONNECTS" ), Direction.BOTH );PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath( expander, 250 );...
在很多地方,我们都可以检查经过的时间,但是对我来说, 扩展器中的expand方法似乎很明显。 我编写了自己的Expander类,如下所示:
public class TimeConstrainedExpander implements PathExpander
{private final StandardExpander expander;private final long startTime;private final Clock clock;private int pathsExpanded = 0;private long timeLimitInMillis;public TimeConstrainedExpander( StandardExpander expander, Clock clock, long timeLimitInMillis ){this.expander = expander;this.clock = clock;this.startTime = clock.instant().toEpochMilli();this.timeLimitInMillis = timeLimitInMillis;}@Overridepublic Iterable<Relationship> expand( Path path, BranchState state ){long timeSoFar = clock.instant().toEpochMilli() - startTime;if ( timeSoFar > timeLimitInMillis ){return Collections.emptyList();}return expander.expand( path, state );}@Overridepublic PathExpander reverse(){return expander.reverse();}
}
现在需要更新较早版本的代码片段以使用我们的新类,这不太麻烦:
StandardExpander orderedExpander = new OrderedByTypeExpander().add( RelationshipType.withName( "CONNECTS" ), Direction.BOTH );TimeConstrainedExpander expander = new TimeConstrainedExpander(orderedExpander, Clock.systemUTC(), 200);PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath( expander, 250 );
...
我不确定这是否是实现我想要的最佳方法,但是在其他几种方法失败之后,至少这种方法确实有效!
翻译自: https://www.javacodegeeks.com/2017/11/neo4j-traversal-query-timeout.html
neo4j 连接超时