0

我正在使用 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;

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

4

2 回答 2

0

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

于 2014-01-21T03:20:13.253 回答
0

问题是 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;
  }
于 2013-02-20T18:08:16.250 回答