gpt4 book ai didi

java - ReSTLet,无法处理 ConnectionException

转载 作者:行者123 更新时间:2023-11-30 08:12:22 27 4
gpt4 key购买 nike

我有以下代码:

private Job[] synchro(URI u)  {
try{
Client client = new Client(new Context(),Protocol.HTTP);
Reference samplesUri = new Reference(u+"/synchrojobs/");

ClientResource cr = new ClientResource(client.getContext(),Method.POST,samplesUri);
cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "xxx", "uxxx");
Job[] back = persistence.all().toArray(new Job[persistence.all().size()]);
for(Job trace : back){
logger.debug(String.format("Sending synchro for %s status:%s time:%s",trace.getId().toString(),trace.getStatus(),trace.getLastStatusChangeDate().toString()));
}
Representation res = cr.post(new JacksonRepresentation<Job[]>(back));
Job[] result = this.mapper.readValue(res.getStream(),Job[].class);
cr.release();
res.release();

logger.info(String.format("Found %s jobs from %s",result.length,u));

return result;
}catch(Exception e){
logger.error("Error synchronizing jobs:"+e.getMessage());
return new Job[0];
}
}

如果对方未连接,会抛出异常,但我的 block 中没有捕获到,而是从外部弹出,为什么会出现这种情况?

最佳答案

事实上,此类错误是由客户端连接器和包装器在 ReSTLet 异常中直接抛出的。

我做了一些测试,并因 java.net.ConnectException 引发了 ReSTLet ResourceException。但我可以使用 try/catch(Exception) 捕获它。见下文:

mai 12, 2015 7:18:54 PM org.restlet.engine.connector.HttpClientHelper start
INFOS: Starting the internal HTTP client
mai 12, 2015 7:18:54 PM org.restlet.resource.ClientResource retry
INFOS: A recoverable error was detected (1000), attempting again in 2000 ms.
mai 12, 2015 7:18:56 PM org.restlet.resource.ClientResource retry
INFOS: A recoverable error was detected (1000), attempting again in 2000 ms.
Connection Error (1000) - The connector failed to connect to the server

at org.restlet.resource.ClientResource.doError(ClientResource.java:590)
at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1153)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1048)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
at org.restlet.resource.ClientResource.handle(ClientResource.java:928)
at org.restlet.resource.ClientResource.get(ClientResource.java:636)
at test.TestClient.main(TestClient.java:9)
Caused by: java.net.ConnectException: Connexion refusée
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:851)
at org.restlet.engine.connector.HttpUrlConnectionCall.sendRequest(HttpUrlConnectionCall.java:356)

也许你可以尝试使用 try/catch(Throwable) 来查看实际抛出了哪个异常?

此外,您能否向我们提供 ReSTLet 的版本以及您使用的客户端连接器?对于我的测试,我使用 ReSTLet 2.3.1 版本...

希望对你有帮助蒂埃里

关于java - ReSTLet,无法处理 ConnectionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195661/

27 4 0
文章推荐: javascript - 优雅加载网页的技巧?
文章推荐: Java Arraylist 想要合并具有相同属性值的对象