Compare commits

Invalid templates have been ignored

1 invalid template(s) found pull_request_template.md: frontmatter must start with a separator line

...

1 Commits

Author SHA1 Message Date
Rajitha Kumara b8d5759109 Add batch executor
2 years ago

@ -145,6 +145,11 @@
</profiles> </profiles>
<dependencies> <dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.testng</groupId> <groupId>org.testng</groupId>
<artifactId>testng</artifactId> <artifactId>testng</artifactId>

@ -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() {}
}

@ -59,6 +59,8 @@ public class Constants {
public static final String IOS = "ios"; public static final String IOS = "ios";
public static final String WINDOWS = "windows"; public static final String WINDOWS = "windows";
public static final int MAX_REQUEST_FOR_PUBLISHER = 50;
public final class OperationStatus { public final class OperationStatus {
private OperationStatus () { throw new AssertionError(); } private OperationStatus () { throw new AssertionError(); }

@ -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…
Cancel
Save