forked from community/device-mgt-core
parent
c8c18c4f3d
commit
b8d5759109
@ -0,0 +1,52 @@
|
||||
package org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BatchResponse implements Serializable {
|
||||
private List<Response> responses = new ArrayList<>();
|
||||
private boolean success;
|
||||
private List<Response> failedResponses = new ArrayList<>();
|
||||
private List<Response> succeededResponses = new ArrayList<>();
|
||||
|
||||
private List<Request> failedRequests = new ArrayList<>();
|
||||
|
||||
public void setResponses(List<Response> responses) { this.responses = responses; }
|
||||
|
||||
public List<Response> getResponses() { return this.responses; }
|
||||
|
||||
public void setFailedResponses(List<Response> failedResponses) { this.failedResponses = failedResponses; }
|
||||
|
||||
public List<Response> getFailedResponses() { return this.failedResponses; }
|
||||
|
||||
public void setSucceededResponses(List<Response> succeededResponses) { this.succeededResponses = succeededResponses; }
|
||||
|
||||
public List<Response> getSucceededResponses() { return this.succeededResponses; }
|
||||
|
||||
public void setFailedRequests(List<Request> failedRequests) { this.failedRequests = failedRequests; }
|
||||
|
||||
public List<Request> getFailedRequests() { return this.failedRequests; }
|
||||
|
||||
public void setSuccess(boolean success) { this.success = success; }
|
||||
|
||||
public boolean getSuccess() { return this.success; }
|
||||
|
||||
public void addResponse(Response response) {
|
||||
this.responses.add(response);
|
||||
if(response.isSuccessful()) {
|
||||
this.succeededResponses.add(response);
|
||||
} else {
|
||||
this.failedResponses.add(response);
|
||||
}
|
||||
}
|
||||
|
||||
public void addRequest(Request request) {
|
||||
this.failedRequests.add(request);
|
||||
}
|
||||
|
||||
public BatchResponse() {}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package org.wso2.carbon.device.mgt.jaxrs.util;
|
||||
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.BatchResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class RESTEndpointHandler {
|
||||
private static final Log logger = LogFactory.getLog(RESTEndpointHandler.class);
|
||||
|
||||
// executes batch of requests to hosts indicate by requests
|
||||
public BatchResponse batchExecute(List<Request> batch, int maxRequestsPerHost) {
|
||||
BatchResponse batchResponse = new BatchResponse();
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
client.dispatcher().setMaxRequestsPerHost(maxRequestsPerHost);
|
||||
batch.forEach(request -> {
|
||||
client.newCall(request).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
batchResponse.addRequest(call.request());
|
||||
String msg = "Failed to execute request to "+call.request().url();
|
||||
logger.error(msg, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) {
|
||||
batchResponse.addResponse(response);
|
||||
}
|
||||
});
|
||||
});
|
||||
if(logger.isDebugEnabled()) {
|
||||
logger.info(
|
||||
"Batch execution is completed."+
|
||||
"Executed : "+batch.size()+"\n"+
|
||||
"Failed : "+batchResponse.getFailedRequests().size()+"\n"+
|
||||
"Succeeded :"+batchResponse.getResponses().size()
|
||||
);
|
||||
}
|
||||
return batchResponse;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue