繁体   English   中英

正确使用Restlet ClientResource

[英]Using the Restlet ClientResource correctly

我在Restlet(v 2.0.5)中的ClientResource遇到了问题,这可能是由于不了解其正确用法所致。

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

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

关于我做错了什么的任何想法?

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

注2:ClientResource Javadoc指出:“并发注释:该类的实例并非旨在在多个线程之间共享。如果需要线程安全,请考虑使用较低级别的Client类。” 但是,restlet创建者说,实际上它是线程安全的,只是没有明确为此目的而设计。 谢谢。

ClientResource是线程安全的,但是即使有可能,它也不是专门为由多个并发线程使用而设计的。 但是,多次重用同一实例是完全有效的。

回到您的问题,我们将需要对您的问题进行更详细的堆栈跟踪以寻求帮助,因为“内部服务器错误”会导致服务器端而不是客户端端出现问题。

希望这会有所帮助,杰罗姆

暂无
暂无

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

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