質問

I am using the RESTlet ClientResource to call a google places endpoint from a server resource. Specifically, a REST request calls the RESTlet ServerResource which calls the ClientResource and returns Google Places results. This runs in Tomcat as a web application.

This works fine for about an hour and then each request hangs. After I stop tomcat, I see all the requests get processed in the logs. This is extremely unstable because Tomcat needs to be restarted every hour.

I have sought an answer for some time and seen that the problem is likely connections not being released. The advice has been to use the exhaust on the representation and stop on the client resource.

I am using the latest stable version of restlets 2.1.1.

Here is how I carry out the process I explained above:

 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();

In the portion where the server resource returns a representation I call exhaust like so:

            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;

Does anyone know how to stop the client resource from hanging?

役に立ちましたか?

解決

The problem is that ClientResource has a problem with closing connections. What worked for me is to use apache httpclient.

I used the following method to do an http get inside of a RESTlet resource:

 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;
  }

他のヒント

ClientConnectionHelper is responsible for closing the connection for ClientResource. Whenever you finished the request you should call storeRoot.release() to tell ClientConnectionHelper to stop the connection.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top