2011-11-28 70 views
0

我有兩個使用HTTPClientTest實例的線程。兩個線程在同一個HTTPClientTest上調用send方法。我應該如何爲每個調用send方法的線程設置不同的套接字超時。如果我在send方法中做了這樣的事情,那麼執行send方法的兩個線程都會有相同的套接字超時。
managerParams.setSoTimeout(60); connectionManager.setParams(managerParams);使用MultiThreadedHttpConnectionManager爲同一個httpclient設置不同的套接字超時時間

我該如何爲HTTPClientTest的同一個實例上的多個線程執行發送方法創建不同的套接字超時。

public class HTTPClientTest implements Runnable{ 
private HttpClient httpClient; 
private MultiThreadedHttpConnectionManager connectionManager; 
private HttpConnectionManagerParams managerParams; 
private HttpClientTest() 
{ 
    connectionManager = new MultiThreadedHttpConnectionManager(); 
    httpClient = new HttpClient(connectionManager); 
} 
public static synchronized HTTPClientTest getInstance() 
{ 
    if(instance == null) 
     instance = new HTTPClientTest(); 
    return instance; 
} 

public void send(String message, String url) 
{ 
    PostMethod post = new PostMethod(url); 
    String reply = ""; 
    String length = message.length() + ""; 
    post.setRequestHeader("Content-Length", length); 
    try 
    { 
     System.out.println("HTTP request: " + message); 
     StringRequestEntity postBody = new StringRequestEntity(message, "text/xml", "UTF-8"); 
     post.setRequestEntity(postBody); 
     int status = httpClient.executeMethod(post); 
     System.out.println("HTTP status: " + status); 
     reply = post.getResponseBodyAsString(); 
     System.out.println("HTTP Post response code: " + reply); 

    } 
    catch(HttpException e) 
    { 
     e.printStackTrace(); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     post.releaseConnection(); 
    } 

} 
} 

回答

1

這很容易:

get.getParams().setParameter("http.socket.timeout",20000); 
httpclient.execute(get); 

的mothods不被線程共享,不是嗎? 根據需要進行修改。

相關問題