2011-05-06 51 views
1

我一直在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創建者說,實際上它是線程安全的,只是沒有明確地爲此設計。 謝謝。

回答

0

ClientResource是線程安全的,但它並不是專門設計用於多個併發線程,儘管它是可能的。但是,多次重複使用同一個實例是完全有效的。

回到您的問題,我們需要更詳細的問題堆棧跟蹤以幫助您,因爲「內部服務器錯誤」導致服務器端而不是客戶端出現問題。

希望這會有幫助, 傑羅姆

+0

感謝您的信息。 – 2011-05-27 23:12:43