首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restlet,无法处理ConnectionException

Restlet,无法处理ConnectionException
EN

Stack Overflow用户
提问于 2015-05-12 23:44:11
回答 1查看 763关注 0票数 1

我有以下代码:

代码语言:javascript
复制
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中没有被捕获,它会在外部弹出,为什么会发生这种情况?

EN

回答 1

Stack Overflow用户

发布于 2015-05-13 01:23:08

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

我做了一些测试,抛出了一个带有原因java.net.ConnectException的Restlet ResourceException。但是我可以使用try / catch (异常)来捕获它。如下所示:

代码语言:javascript
复制
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版本...

希望对你有帮助,蒂埃里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30195661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档