简体   繁体   English

正确使用Restlet ClientResource

[英]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. 我在Restlet(v 2.0.5)中的ClientResource遇到了问题,这可能是由于不了解其正确用法所致。

I'm using the ClientResource, with the Apache HTTP Client connector, and have written the following: 我正在使用ClientResource和Apache HTTP Client连接器,并编写了以下内容:

        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. 我怀疑不是这样,因为几个小时后(7个或更多个),此部分代码开始引发以下错误“内部服务器错误”,并且目的地不再接收消息。

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. 注意:我知道ClientResource不是线程安全的,并且您会注意到,在我的代码中,我使用执行程序来运行此部分代码,但是,该执行程序仅包含一个线程,因此,直到我理解其他情况为止,我已经排除了这个问题。

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." 注2:ClientResource Javadoc指出:“并发注释:该类的实例并非旨在在多个线程之间共享。如果需要线程安全,请考虑使用较低级别的Client类。” However, the restlet creator, says that in fact it is Thread-safe, just not explicitly designed for this purpose. 但是,restlet创建者说,实际上它是线程安全的,只是没有明确为此目的而设计。 Thanks. 谢谢。

ClientResource is thread safe, but it wasn't especially designed to be used by several concurrent threads, even though it is possible. ClientResource是线程安全的,但是即使有可能,它也不是专门为由多个并发线程使用而设计的。 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 希望这会有所帮助,杰罗姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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