简体   繁体   中英

Using the Restlet ClientResource correctly

I've been having a problem with the ClientResource in Restlet (v 2.0.5) which may be a consequence of not understanding its correct usage.

I'm using the ClientResource, with the Apache HTTP Client connector, and have written the following:

        private final ClientResource httpClient;
        public SendClient(String uri) {
            httpClient = new ClientResource(uri);
        }
        // Omitted code would create messages to send, and then use an executor
        // to send this particular message to its destination.
        public void run() {
           ClientResource sendClient = null;
           try {
              sendClient = wsClient.getChild(uriResource); // re-use original httpclient instance, uriResource is passed in to the method that calls this.
              sendClient.post(form);
           } catch (Throwable e) {
              logger.error("Unable to send message, {}", e.getMessage());
           } finally {
              if (sendClient != null) {
                 sendClient.release(); // As I understand from [Restlet WIKI][1] 
              }
           }
        }

Is this correct? I suspect that it is not, since after several hours (7 or more) this section of code starts throwing the following error, "Internal Server Error", and messages are no longer received by the destination.

Any ideas of what I am doing incorrectly?

NOTE I am aware that ClientResource is not thread-safe, and you'll notice that in my code I am using an executor to run this section of code, however, that executor contains only a single thread, so, until I understand otherwise, I've ruled out that as a problem.

NOTE 2: The ClientResource javadoc states: "Concurrency note: instances of the class are not designed to be shared among several threads. If thread-safety is necessary, consider using the lower-level Client class instead." However, the restlet creator, says that in fact it is Thread-safe, just not explicitly designed for this purpose. Thanks.

ClientResource is thread safe, but it wasn't especially designed to be used by several concurrent threads, even though it is possible. However, it is perfectly valid to reuse the same instance several times.

Back to your problem, we would need a more detailed stack trace of your problem to help out, because the "Internal Server Error" leads to an issue on the server side rather than the client-side.

Hope this helps, Jerome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM