正确使用Restlet ClientResource

发布于 2024-11-05 09:04:24 字数 1373 浏览 5 评论 0原文

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

我正在使用 ClientResource 和 Apache HTTP 客户端连接器,并编写了以下内容:

        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 创建者表示,实际上它是线程安全的,只是没有明确为此目的而设计。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-11-12 09:04:24

ClientResource 是线程安全的,但它并不是专门设计用于多个并发线程使用的,尽管这是可能的。然而,多次重复使用同一个实例是完全有效的。

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

希望这有帮助,
杰罗姆

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文