2009-11-24 6 views
0

느린 웹 서비스 (각 요청이 약 4 분)로 작업 중이며 2 시간 내에 약 100 건의 요청을 수행해야하므로 여러 스레드를 사용하십시오. 문제는 스텁이 다른 모든 것을 거부하기 때문에 2 개의 스레드 만 가질 수 있다는 것입니다. Here 설명과 가능한 해결책을 찾았습니다.java 및 axis2를 사용하여 웹 서비스에 멀티 스레딩 요청을 보낼 때 "시간 초과"

같은 문제가있었습니다. 그것의 소스가 MultiThreadedHttpConnectionManager 나를 위해 2. 해결 방법,

아래의 예에서 같은 것을 MultiThreadedHttpConnectionManager의 자신의 인스턴스를 생성하고 서비스 스텁에서 사용 이었다 동일 defaultMaxConnectionsPerHost 값 에 것 같다

필자는 작성자가 말했듯이 더 높은 setMaxTotalConnectionssetDefaultMaxConnectionsPerHost 값을 가진 스텁에 HttpClient를 전달했지만 문제는 이제 해당 응용 프로그램 동결은 (글쎄, 실제로 얼지 않는다. 그러나 아무것도하지 않는다).

public ReportsStub createReportsStub(String url, HttpTransportProperties.Authenticator auth){ 
    ReportsStub stub = null; 
    HttpClient httpClient = null; 
    try { 
    stub = new ReportsStub(url); 
    httpClient = createHttpClient(10,5); 
    stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(10000000); 
    stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth); 
    stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, false); 
    stub._getServiceClient().getServiceContext().getConfigurationContext().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient); 
    return stub; 
    } catch (AxisFault e) { 
    e.printStackTrace(); 
    } 
    return stub; 
} 

protected HttpClient createHttpClient(int maxTotal, int maxPerHost) { 
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager(); 
    HttpConnectionManagerParams params = httpConnectionManager.getParams(); 
    if (params == null) { 
     params = new HttpConnectionManagerParams(); 
     httpConnectionManager.setParams(params); 
    } 
    params.setMaxTotalConnections(maxTotal); 
    params.setDefaultMaxConnectionsPerHost(maxPerHost); 
    HttpClient httpClient = new HttpClient(httpConnectionManager); 
    return httpClient; 
} 

그럼 내가 그 스텁 스레드 각각에 요청을 전달하고이를 실행

내 코드를 이잖아. HttpClient를 설정하지 않고 기본값을 사용하면 두 개의 스레드 만 실행되고 설정하면 응용 프로그램이 작동하지 않습니다. 어떤 생각?

+0

스레드 덤프를 보면이 스레드들은 무엇을하고 있습니까? (예를 들어, Eclipse에서 클라이언트를 디버깅하는 경우 디버그 퍼스펙티브로 이동하여 모든 스레드를 일시 중지 한 다음 각 스레드를 클릭하여 최대 성능을 확인하십시오. –

+0

이 경우 시도해 볼 수도 있습니다. 나는 그들이 왜 교수형을하는지 이해하지 못한다. – Sergi

답변

0

응답하기 위해 오랜 시간이 걸릴 수있는 백엔드 서비스를 호출 한 회사 웹 응용 프로그램에서이 사실을 발견했습니다. 웹 응용 프로그램은 단일 호스트에 대한 연결 수가 2 개로 제한되어 고정됩니다.

httpConnectionManager.setParams(params)으로 전화하십시오. 전에 params.setDefaultMaxConnectionsPerHost()으로 전화하십시오. 매개 변수의 적용이 httpConnectionManager.setParams 함수 자체에서 발생하지 않는다는 것을 확인하기 위해 반대 순서로 이러한 함수를 호출 해 보았습니까? 사람이 WSO2 Axis2와의 동적 REST 클라이언트를 작성하고자하는 경우, 다음 코드는 나를 위해 일한

+0

그 대답은 확실하지 않지만 어쨌든 내일 시도해 보겠습니다. 이 순간 기업의 웹 서비스는 매우 전문적입니다. – Sergi

+0

흠 나는 네가 옳을 수도 있다고 생각한다. 결국 당신은 연결 관리자 객체에서'getParams'를 호출하고 그 객체를 수정하려고합니다. 어설 션으로'setParams()'로 설정 한 객체가'getParams()'함수가 반환 한 객체와 같은지 확인할 수 있습니까? –

+0

당신은 @PP가 맞았습니다. 함수가 호출 된 순서입니다.적어도 그것은 현재 작동하고 있기 때문에, 나는 웹 서비스 (그 신뢰성이 끔찍한)와 관련된 문제를 폐기하지 않는다고 생각합니다. – Sergi

1

...

// Set the max connections to 20 and the timeout to 20 seconds 
MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager(); 
HttpConnectionManagerParams params = new HttpConnectionManagerParams(); 
params.setDefaultMaxConnectionsPerHost(20); 
params.setMaxTotalConnections(20); 
params.setSoTimeout(20000); 
params.setConnectionTimeout(20000); 
multiThreadedHttpConnectionManager.setParams(params); 
HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager); 

// Create the service client 
ServiceClient serviceClient = new ServiceClient(); 
Options options = new Options(); 
options.setTo(new EndpointReference(endpoint)); 
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE); 
options.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_POST); 

serviceClient.getServiceContext().getConfigurationContext().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient); 
serviceClient.setOptions(options); 

// Blocking call 
OMElement result = serviceClient.sendReceive(ClientUtils.getRestPayload()); // just a dummy payload <root></root> 

// Cleanup Transport after each call, this is needed to otherwise the HTTP gets blocked 
serviceClient.cleanupTransport(); 

나는 20 최대 연결 20 초에 타임 아웃을했습니다. 또한 내 '끝점'에는 모든 REST 인수가 포함되어 있습니다. serviceClient.sendReceive() 메서드에서 더미 페이로드 "< 루트 > </root "을 사용하고 있습니다.