我一直在Restlet(v 2.0.5)中的ClientResource出现问题,这可能是不了解其正确用法的后果。正确使用Restlet 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创建者说,实际上它是线程安全的,只是没有明确地为此设计。 谢谢。
感谢您的信息。 – 2011-05-27 23:12:43