创建 MongoClient 实例时如何捕获异常
How to catch exception when creating MongoClient instance
我正在使用 Mongo DB java 驱动程序连接到 mongo 实例。下面是我用来创建 MongoClient 实例的代码。
try {
new MongoClient("localhost", 1111);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果主机名或端口号不正确,我将得到以下异常。我在徘徊如何捕捉异常。 Mongo数据库连接发生在客户端代码无法捕获的内部线程中。我想知道 MongoClient 是否连接正确。我怎样才能得到这些信息?
INFO: Exception in monitor thread while connecting to server localhost:0
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Can't assign requested address (connect failed)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more
EDIT1
我的代码没有捕捉到上面显示的异常。它可能被 Mongo 代码捕获。所以不知道实例是否创建正确
new MongoClient("localhost", 1111);
} catch (MongoSocketOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您只需在 catch 块中命名适当的异常。这适用于任何例外情况。您可以为每个唯一异常添加尽可能多的 catch 块
服务器连接是在守护线程上创建的。长话短说,您将无法在创建 Mongo 客户端时检查与连接相关的错误。
当您创建第一个涉及读取或写入的真实数据库时,您将不得不延迟连接检查。
仅供演示之用,让您了解一下。
MongoClient mongoClient = new MongoClient("127.0.34.1", 89);
DB db = mongoClient.getDB("test");
try {
db.addUser("user", new char[] {'p', 'a', 's', 's'});
} catch(Exception e) { MongoTimeoutException exception}
Mongo来自守护线程的 SocketOpenException
INFO: Exception in monitor thread while connecting to server 127.0.34.1:89
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
Mongo来自主线程的超时异常
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.34.1:89, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket},
caused by {java.net.ConnectException: Connection refused: connect}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:375)
所以用 MongoTimeoutException
将代码包装在 try catch 块中,它可以正常检查与连接相关的错误。
非常简洁大方:
重定向 System.err
到文件:
ByteArrayOutputStream file=new ByteArrayOutputStream();
System.setErr(new PrintStream(file));
使用您的 MongoClient
和您的 MongoCredentials
:
连接到服务器
MongoCredential credenciales=MongoCredential.createCredential("root", "admin", "root".toCharArray());
MongoClient client = new MongoClient(new ServerAddress("localhost"), Arrays.asList(credenciales));
读取错误输出,在ByteArrayOutputStream
对象中:
String texto=new String(file.toByteArray());
检查Autentication failed
字符串是否存在:
if (texto.contains("'Authentication failed.'"))
// do something;
else
...
对于现在偶然发现此问题的任何人。我遇到了同样的问题并尝试使用 Macario 的答案,但结果并不顺利。然后意识到监视连接的线程没有将它发送到 System.err,而是将它发送到 System.out,所以不要使用 System.err,而是像这样使用 System.out:
PrintStream out = System.out; // Save the original out stream to reset later
// Set out to a buffer
ByteArrayOutputStream file=new ByteArrayOutputStream();
System.setOut(new PrintStream(file));
mongoClient = new MongoClient(new MongoClientURI("<connection_string>")));
// Found this to be important as we need to wait for the thread to dump to out
// 1000 millis was too fast for me but 2000 did the trick
Thread.sleep(2000);
// Convert buffer to a string
String texto=new String(file.toByteArray());
System.setOut(out); // Reset out
// Check if saved out contins the error (I was looking for MongoSocketException)
if(texto.contains("MongoSocketException")){
// Do Stuff
}
我正在使用 Mongo DB java 驱动程序连接到 mongo 实例。下面是我用来创建 MongoClient 实例的代码。
try {
new MongoClient("localhost", 1111);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果主机名或端口号不正确,我将得到以下异常。我在徘徊如何捕捉异常。 Mongo数据库连接发生在客户端代码无法捕获的内部线程中。我想知道 MongoClient 是否连接正确。我怎样才能得到这些信息?
INFO: Exception in monitor thread while connecting to server localhost:0
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Can't assign requested address (connect failed)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more
EDIT1
我的代码没有捕捉到上面显示的异常。它可能被 Mongo 代码捕获。所以不知道实例是否创建正确
new MongoClient("localhost", 1111);
} catch (MongoSocketOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您只需在 catch 块中命名适当的异常。这适用于任何例外情况。您可以为每个唯一异常添加尽可能多的 catch 块
服务器连接是在守护线程上创建的。长话短说,您将无法在创建 Mongo 客户端时检查与连接相关的错误。
当您创建第一个涉及读取或写入的真实数据库时,您将不得不延迟连接检查。
仅供演示之用,让您了解一下。
MongoClient mongoClient = new MongoClient("127.0.34.1", 89);
DB db = mongoClient.getDB("test");
try {
db.addUser("user", new char[] {'p', 'a', 's', 's'});
} catch(Exception e) { MongoTimeoutException exception}
Mongo来自守护线程的 SocketOpenException
INFO: Exception in monitor thread while connecting to server 127.0.34.1:89
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
Mongo来自主线程的超时异常
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.34.1:89, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket},
caused by {java.net.ConnectException: Connection refused: connect}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:375)
所以用 MongoTimeoutException
将代码包装在 try catch 块中,它可以正常检查与连接相关的错误。
非常简洁大方:
重定向
System.err
到文件:ByteArrayOutputStream file=new ByteArrayOutputStream(); System.setErr(new PrintStream(file));
使用您的
连接到服务器MongoClient
和您的MongoCredentials
:MongoCredential credenciales=MongoCredential.createCredential("root", "admin", "root".toCharArray()); MongoClient client = new MongoClient(new ServerAddress("localhost"), Arrays.asList(credenciales));
读取错误输出,在
ByteArrayOutputStream
对象中:String texto=new String(file.toByteArray());
检查
Autentication failed
字符串是否存在:if (texto.contains("'Authentication failed.'")) // do something; else ...
对于现在偶然发现此问题的任何人。我遇到了同样的问题并尝试使用 Macario 的答案,但结果并不顺利。然后意识到监视连接的线程没有将它发送到 System.err,而是将它发送到 System.out,所以不要使用 System.err,而是像这样使用 System.out:
PrintStream out = System.out; // Save the original out stream to reset later
// Set out to a buffer
ByteArrayOutputStream file=new ByteArrayOutputStream();
System.setOut(new PrintStream(file));
mongoClient = new MongoClient(new MongoClientURI("<connection_string>")));
// Found this to be important as we need to wait for the thread to dump to out
// 1000 millis was too fast for me but 2000 did the trick
Thread.sleep(2000);
// Convert buffer to a string
String texto=new String(file.toByteArray());
System.setOut(out); // Reset out
// Check if saved out contins the error (I was looking for MongoSocketException)
if(texto.contains("MongoSocketException")){
// Do Stuff
}