正确使用Restlet ClientResource

时间:2011-05-06 16:54:38

标签: java apache httpclient restlet

我一直遇到Restlet中的ClientResource问题(v 2.0.5),这可能是因为不了解它的正确用法。

我正在使用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创建者说,事实上它是线程安全的,只是没有为此目的明确设计。 感谢。

1 个答案:

答案 0 :(得分:0)

ClientResource是线程安全的,但它并没有特别设计为由多个并发线程使用,即使它是可能的。但是,多次重复使用同一个实例是完全有效的。

回到你的问题,我们需要更详细的问题堆栈跟踪来帮助解决问题,因为“内部服务器错误”会导致服务器端而不是客户端出现问题。

希望这有帮助, 杰罗姆