我有一个基于jax-rs的REST服务,我使用Java 11在64位Linux的Tomcat 8.5上运行;该服务连接到同一Linux机器上的RavenDB 4.1.2实例。我利用流查询来返回请求结果。我使用Postman提交相同的请求,并且一切正常:返回结果的速度相当快。
但是-它只能工作10次。当我第11次提交与以前相同的请求时,results = currentSession.advanced().stream(query);
行挂起并且不返回。
起初,我认为我可能与StreamingOutput
或OutputStreamWriter
没有适当关闭有关。也许与Response
有所关系-但是当我在调试模式下逐步浏览Eclipse中已部署的代码时,我注意到执行挂在该流线上。
(我发现恰好有10次是一种特殊的“人为选择”的数字...)
我的代码的相关部分:
@GET
@Path("/abcntr/{ccode}/{st}/{zm}")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
public Response retrieveInfo(@PathParam("ccode") String ccode, @PathParam("st") String st, @PathParam("zm") String zm)
{
(...)
StreamingOutput adminAreaStream = new StreamingOutput()
{
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
@Override
public void write(OutputStream output) throws IOException, WebApplicationException
{
try(IDocumentSession currentSession = ServiceListener.ravenDBStore.openSession())
{
Writer writer = new BufferedWriter(new OutputStreamWriter(output));
(...)
if(indexToBeQueried.startsWith("Level0"))
{
IDocumentQuery<AdministrativeArea> query = currentSession.query(area.class, Query.index(indexToBeQueried))
.whereEquals("i", ccode);
results = currentSession.advanced().stream(query);
}
else
{
IDocumentQuery<AdministrativeArea> query = currentSession.query(area.class, Query.index(indexToBeQueried))
.whereEquals("i", ccode)
.andAlso()
.whereEquals("N1", sName);
results = currentSession.advanced().stream(query); // THIS IS WHERE IT DOESNT COME BACK
}
while(results.hasNext())
{
StreamResult<AdministrativeArea> adma = results.next();
adma.getDocument().properties = retrievePropertiesForArea(adma.getDocument(), currentSession);
writer.write(ow.writeValueAsString(adma.getDocument()));
writer.write(",");
}
(...)
currentSession.advanced().clear();
currentSession.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage() + e.getStackTrace());
}
}
};
if(!requestIsValid)
return Response.status(400).build();
else
return Response.ok(adminAreaStream).build();
}
RavenDB错误日志为空,而Tomcat错误日志也为空。唯一与此类似的错误消息是从“收集调试信息”中显示的内容:
System.ArgumentNullException: Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Raven.Server.Documents.Handlers.Debugging.QueriesDebugHandler.QueriesCacheList() in C:\Builds\RavenDB-Stable-4.1\src\Raven.Server\Documents\Handlers\Debugging\QueriesDebugHandler.cs:line 181
at Raven.Server.ServerWide.LocalEndpointClient.InvokeAsync(RouteInformation route, Dictionary`2 parameters) in C:\Builds\RavenDB-Stable-4.1\src\Raven.Server\ServerWide\LocalEndpointClient.cs:line 61
at Raven.Server.ServerWide.LocalEndpointClient.InvokeAndReadObjectAsync(RouteInformation route, JsonOperationContext context, Dictionary`2 parameters) in C:\Builds\RavenDB-Stable-4.1\src\Raven.Server\ServerWide\LocalEndpointClient.cs:line 91
at Raven.Server.Documents.Handlers.Debugging.ServerWideDebugInfoPackageHandler.WriteForDatabase(ZipArchive archive, JsonOperationContext jsonOperationContext, LocalEndpointClient localEndpointClient, String databaseName, String path) in C:\Builds\RavenDB-Stable-4.1\src\Raven.Server\Documents\Handlers\Debugging\ServerWideDebugInfoPackageHandler.cs:line 311
感谢您提供给我的各种调查提示。
更新: 将编译器和Tomcat JVM移回Java 1.8时也是一样。
答案 0 :(得分:2)
似乎它与Java 11(或1.8)无关,只是它使我的视线关闭了CloseableIterator<StreamResult<AdministrativeArea>> results;
。添加了简单的results.close();
之后,一切似乎都可以正常工作。如果不是此解决方案,我将返回并更新。