我有一个绑定到findAsync
呼叫的简单回调。即使数据库中的数据是静态的,我每隔几个请求就会观察到一次失败。
一些快速调试表明我的回调总是被调用两次。我的应用程序失败的情况是由null
MongoIterator<Document>
传入我的回调引起的。当然,我只希望在数据准备好时(或者如果有异常)有一个回调。
这是预期的行为吗?我可以做些什么来确保我的回调在查询完成时只被调用一次?
这是代码片段:
collection.findAsync(
[
callback: { MongoIterator<Document> v ->
List data = []
try {
while(v.hasNext()) {
data.add(docToJson(v.next()))
}
} finally {
if (v != null) v.close()
}
sendReply([ status: 'ok', data: data ])
},
exception: { Throwable t ->
sendReply([ status: 'error', message: t.message ])
}
] as Callback<MongoIterator<Document>>,
find
)
这是堆栈跟踪:
Unexpected MongoDB Connection closed: Auth(MongoDB(43026-->localhost/127.0.0.1:27017)). Will try to reconnect.
Reconnected to localhost/127.0.0.1:27017
Exception in thread "MongoDB 43026<--localhost/127.0.0.1:27017" java.lang.NullPointerException: Cannot invoke method hasNext() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at mongoAsync$_run_closure2_closure6.doCall(mongoAsync.groovy:126)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at groovy.lang.Closure.call(Closure.java:411)
at org.codehaus.groovy.runtime.ConvertedMap.invokeCustom(ConvertedMap.java:50)
at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:81)
at com.sun.proxy.$Proxy14.callback(Unknown Source)
at com.allanbank.mongodb.client.AbstractReplyCallback.handle(AbstractReplyCallback.java:82)
at com.allanbank.mongodb.client.AbstractValidatingReplyCallback.callback(AbstractValidatingReplyCallback.java:72)
at com.allanbank.mongodb.client.AbstractValidatingReplyCallback.callback(AbstractValidatingReplyCallback.java:33)
at com.allanbank.mongodb.connection.message.ReplyHandler.reply(ReplyHandler.java:77)
at com.allanbank.mongodb.connection.socket.SocketConnection.reply(SocketConnection.java:560)
at com.allanbank.mongodb.connection.socket.SocketConnection$ReceiveRunnable.receiveOne(SocketConnection.java:735)
at com.allanbank.mongodb.connection.socket.SocketConnection$ReceiveRunnable.run(SocketConnection.java:683)
at java.lang.Thread.run(Thread.java:722)