Java 程序在Mongodb异步查询完成之前终止
我正在学习教程:。我只是尝试连接到数据库并阅读我创建的包含2个文档的集合:Java 程序在Mongodb异步查询完成之前终止,java,mongodb,asynchronous,Java,Mongodb,Asynchronous,我正在学习教程:。我只是尝试连接到数据库并阅读我创建的包含2个文档的集合: import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoClient; import com.mongodb.async.client.MongoClients; import com.mongodb.async.client.MongoCollection; import com.mongodb.async.
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.async.client.MongoDatabase;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
public class Main {
MongoClient client = MongoClients.create();
MongoDatabase database = client.getDatabase("mydb");
public Main() {
readUsers();
}
public void readUsers() {
MongoCollection<Document> collection = database.getCollection("user");
// find documents
collection.find().into(new ArrayList<Document>(),
new SingleResultCallback<List<Document>>() {
@Override
public void onResult(final List<Document> result, final Throwable t) {
System.out.println("Found Documents: #" + result.size());
}
});
}
public static void main(String[] args) throws Exception {
new Main();
}
}
import com.mongodb.async.SingleResultCallback;
导入com.mongodb.async.client.MongoClient;
导入com.mongodb.async.client.MongoClients;
导入com.mongodb.async.client.MongoCollection;
导入com.mongodb.async.client.MongoDatabase;
导入java.util.ArrayList;
导入java.util.List;
导入org.bson.Document;
公共班机{
MongoClient=MongoClient.create();
MongoDatabase数据库=client.getDatabase(“mydb”);
公用干管(){
readUsers();
}
public void readUsers(){
MongoCollection collection=database.getCollection(“用户”);
//查找文档
collection.find().into(新的ArrayList(),
新的SingleResultCallback(){
@凌驾
公共无效onResult(最终列表结果,最终可丢弃t){
System.out.println(“找到的文档:#”+result.size());
}
});
}
公共静态void main(字符串[]args)引发异常{
新的Main();
}
}
但我一直得到以下错误:
2015年12月28日下午6:22:51 com.mongodb.diagnostics.logging.JULLogger日志
信息:使用设置{hosts=[localhost:27017]创建的群集,
模式=单一,requiredClusterType=未知,
serverSelectionTimeout='30000毫秒',maxWaitQueueSize=500}2015年12月28日
下午6:22:51 com.mongodb.diagnostics.logging.JULLogger日志信息:否
ReadPreferenceServerSelector{readPreference=primary}选择的服务器
来自群集描述ClusterDescription{type=UNKNOWN,
connectionMode=SINGLE,all=[ServerDescription{address=localhost:27017,
类型=未知,状态=正在连接}]}。在计时之前等待30000毫秒
出去
我不知道为什么会这样?我正在关注文档中显示的内容。您需要确保在
readUsers
返回以下内容之前,Main()
代码没有完成:
...
import java.util.concurrent.Semaphore;
public class Main {
MongoClient client = MongoClients.create();
MongoDatabase database = client.getDatabase("mydb");
Semaphore semaphore = new Semaphore(0);
public Main() throws Exception {
readUsers();
semaphore.acquire();
}
public void readUsers() {
MongoCollection<Document> collection = database.getCollection("users");
// find documents
collection.find().into(new ArrayList<Document>(),
new SingleResultCallback<List<Document>>() {
@Override
public void onResult(final List<Document> result, final Throwable t) {
System.out.println("Found Documents: #" + result.size());
semaphore.release();
}
});
}
public static void main(String[] args) throws Exception {
new Main();
}
}
。。。
导入java.util.concurrent.Semaphore;
公共班机{
MongoClient=MongoClient.create();
MongoDatabase数据库=client.getDatabase(“mydb”);
信号量信号量=新信号量(0);
public Main()引发异常{
readUsers();
semaphore.acquire();
}
public void readUsers(){
MongoCollection collection=database.getCollection(“用户”);
//查找文档
collection.find().into(新的ArrayList(),
新的SingleResultCallback(){
@凌驾
公共无效onResult(最终列表结果,最终可丢弃t){
System.out.println(“找到的文档:#”+result.size());
semaphore.release();
}
});
}
公共静态void main(字符串[]args)引发异常{
新的Main();
}
}
信息:ReadPreferenceServerSelector{readPreference=primary}未选择任何服务器
这闻起来像是副本集处于只读模式(未选择主副本)@SalvadorJuanMartinez感谢您的回复。我在本地计算机上只运行了一个实例,没有副本集?是的,我注意到我在看到[ServerDescription{address=localhost:27017,type=UNKNOWN,state=CONNECTING}]}
时匆忙发表了评论,你试过正常连接吗?只是为了确保它有效。另外,作为旁注,您仍然可以拥有一个只有一个节点的副本集,这有点毫无意义,但这是可能的。@SalvadorJuanMartinez是的,连接可以从shell工作,我尝试了Mongo Management Studio IDE,我可以连接和读/写数据。Mongo安装是否新鲜?我的意思是,你有没有玩过配置和更改过默认设置?(端口、接口、安全性等)。我怀疑问题是由MongoClient=MongoClients.create()引起的代码>这样调用,只是使用默认值。