Commit e3906db1 authored by joubran.mansour's avatar joubran.mansour

Implement WebClient and Aggregator

parent 355a464e
target/
.idea/
*.iml
logs/
out/
\ No newline at end of file
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
......@@ -11,17 +12,30 @@ public class Aggregator {
/*send task to list of workers*/
public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks) {
/*
Add your code here
*/
throw new UnsupportedOperationException();
return sendTasksToWorkers(workersAddresses, tasks, null);
}
/*send task to list of workers*/
public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks, String headers) {
/*
Add your code here
*/
throw new UnsupportedOperationException();
if (workersAddresses.size() != tasks.size()) {
throw new IllegalArgumentException(
"Number of workers (" + workersAddresses.size()
+ ") must equal number of tasks (" + tasks.size() + ")");
}
List<CompletableFuture<String>> futures = new ArrayList<>();
for (int i = 0; i < workersAddresses.size(); i++) {
byte[] payload = tasks.get(i).getBytes();
CompletableFuture<String> future = (headers == null)
? webClient.sendTask(workersAddresses.get(i), payload)
: webClient.sendTask(workersAddresses.get(i), payload, headers);
futures.add(future);
}
List<String> results = new ArrayList<>();
for (CompletableFuture<String> future : futures) {
results.add(future.join());
}
return results;
}
}
\ No newline at end of file
import java.net.URI;
import java.net.http.HttpClient;
import java.util.ArrayList;
import java.util.List;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
public class WebClient {
......@@ -15,17 +16,29 @@ public class WebClient {
/* send task (post http request) asynchronously */
public CompletableFuture<String> sendTask(String url, byte[] requestPayload) {
/*
Add your code here
*/
throw new UnsupportedOperationException();
return sendTask(url, requestPayload, null);
}
/* send task (post http request) asynchronously with custom headers*/
public CompletableFuture<String> sendTask(String url, byte[] requestPayload, String headers) {
/*
Add your code here
*/
throw new UnsupportedOperationException();
HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(URI.create(url))
.POST(HttpRequest.BodyPublishers.ofByteArray(requestPayload));
if (headers != null && !headers.isBlank()) {
int idx = headers.indexOf(':');
if (idx <= 0) {
throw new IllegalArgumentException(
"Invalid header format, expected 'Name: value' but got: " + headers);
}
String name = headers.substring(0, idx).trim();
String value = headers.substring(idx + 1).trim();
builder.header(name, value);
}
HttpRequest request = builder.build();
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment