【问题标题】:RESTlet ClientResource hangs after calling a remote host several timesRESTlet ClientResource 在多次调用远程主机后挂起
【发布时间】:2013-02-18 17:33:04
【问题描述】:

我正在使用 RESTlet ClientResource 从服务器资源调用 Google Places 端点。具体来说,REST 请求调用 RESTlet ServerResource,后者调用 ClientResource 并返回 Google Places 结果。这在 Tomcat 中作为 Web 应用程序运行。

这可以正常工作大约一个小时,然后每个请求都会挂起。停止 tomcat 后,我​​看到所有请求都在日志中得到处理。这是非常不稳定的,因为 Tomcat 需要每隔一小时重新启动一次。

我已经寻找了一段时间的答案,发现问题可能是连接没有被释放。建议是在表示上使用耗尽并在客户端资源上停止。

我使用的是restlets 2.1.1的最新稳定版。

这是我执行上面解释的过程的方式:

 ClientResource storeRoot = new ClientResource(placeEndPoint);
 Client c = (Client)storeRoot.getNext();

 String jsonString = storeRoot.get().getText();
    try {
    c.stop();
} catch (Exception e) {
    e.printStackTrace();
}

storeRoot.release();

在服务器资源返回表示的部分中,我称之为排气:

            JSONArray ja = new JSONArray(placesList);
    JsonRepresentation jr = new JsonRepresentation(ja);
    jr.setCharacterSet(CharacterSet.UTF_8);
    try {
        jr.exhaust();
        jr.release();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{

    }

    return jr;

有谁知道如何阻止客户端资源挂起?

【问题讨论】:

    标签: tomcat7 restlet freeze restlet-2.0


    【解决方案1】:

    问题是 ClientResource 有关闭连接的问题。对我有用的是使用 apache httpclient。

    我使用以下方法在 RESTlet 资源中执行 http get:

     public String httpGet(String url) throws IllegalStateException, IOException {
    
        HttpClient httpclient = new DefaultHttpClient();
    
         // Prepare a request object
         HttpGet httpget = new HttpGet(url);
    
         // Execute the request
         HttpResponse response = httpclient.execute(httpget);
    
         // Examine the response status
         System.out.println(response.getStatusLine());
    
         // Get hold of the response entity
         HttpEntity entity = response.getEntity();
    
         // If the response does not enclose an entity, there is no need
         // to worry about connection release
         if (entity != null) {
             InputStream instream = entity.getContent();
             try {
    
                 BufferedReader reader = new BufferedReader(
                         new InputStreamReader(instream));
                 // do something useful with the response
                 //System.out.println(reader.readLine());
    
                 StringBuilder builder = new StringBuilder();
                 String aux = "";
    
                 while ((aux = reader.readLine()) != null) {
                     builder.append(aux);
                 }
    
                 String text = builder.toString();
    
                 return text;
    
             } catch (IOException ex) {
    
                 // In case of an IOException the connection will be released
                 // back to the connection manager automatically
                 throw ex;
    
             } catch (RuntimeException ex) {
    
                 // In case of an unexpected exception you may want to abort
                 // the HTTP request in order to shut down the underlying
                 // connection and release it back to the connection manager.
                 httpget.abort();
                 throw ex;
    
             } finally {
    
                 // Closing the input stream will trigger connection release
                 instream.close();
                 httpclient.getConnectionManager().shutdown();
             }
    
             // When HttpClient instance is no longer needed,
             // shut down the connection manager to ensure
             // immediate deallocation of all system resources
    
         }
    
         return null;
      }
    

    【讨论】:

      【解决方案2】:

      ClientConnectionHelper 负责关闭 ClientResource 的连接。每当您完成请求时,您应该调用 storeRoot.release() 告诉 ClientConnectionHelper 停止连接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-02
        • 2019-12-31
        • 2014-07-28
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多