HTTPClientTest의 동일한 인스턴스를 사용하는 두 개의 스레드가 있습니다. 두 개의 스레드는 동일한 HTTPClientTest에서 send 메소드를 호출합니다. send 메소드를 호출하는 각 스레드마다 다른 소켓 시간 초과를 설정해야합니다. 만약 send 메소드 내에서 이렇게하면 send 메소드를 실행하는 두 개의 쓰레드 모두 동일한 소켓 타임 아웃을 가지게됩니다.
managerParams.setSoTimeout (60); connectionManager.setParams (managerParams);MultiThreadedHttpConnectionManager를 사용하여 동일한 httpclient에 다른 소켓 시간 제한을 설정하십시오.
HTTPClientTest의 동일한 인스턴스에서 send 메소드를 실행하는 여러 스레드에 대해 다른 소켓 시간 초과를 어떻게 작성해야합니까?
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();
}
}
}