Parallelizing a call in java - java

I have the following code:
List<ObjectA> allObjects = (List<ObjectA>) objArepository.findAll();
for (ObjectA objA : allObjects) {
String location = objA.getUrl();
Client client = utils.createClient();
WebTarget webTarget = client.target(location).path("/testUrl/" + someString);
Invocation.Builder requestBuilder = webTarget.request();
Response response;
try {
response = request.invoke();
}
}
instead of the for loop which sends those calls serially, I would like to send those calls all parallely, the problem is for that I didn't find any examples and I am missing an idea how to do that in java

Use ExecutorService.
The executorService.invokeAll can execute a list of tasks in parallel and wait them to complete.
ExecutorService executor = getExecutorService();
List<Request> requests = getRequests();
List<Callable> tasks = requests.stream()
.map(r -> new Processor(r))
.collect(Collectors.toList());
executor.invokeAll(tasks);
If you need asynchronous calls, use executorService.submit or executorService.execute
Update
According to the comment, I add a few more words about the code above.
getExecutorServices() returns a executorService created in other places, maybe a singleton, since the creation of an executorService is quite expensive.
getRequests() returns a list of requests, Request can be anything you want to process, such as ObjectA in the question.
executorService.invokeAll accepts a list of Callable, so you have to encapsulate your requests in callables. Processor is a callable to process Request.
Actually, I think the code is quite descriptive and an ordinary Java programmer can understand it.

Related

Java best way to send multiple HTTP requests

I have a Java program that sends hundreds of GET requests to a server using Java 11 HttpClient, and it needs to do this 2-3 times every minute. Currently, it does the following (just describing the logic):
for each request that needs to be sent:
//build GET request
CompletableFuture<HttpResponse<String>> future = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
futures.add(future)
// for every 100 request, sleep for 2500 milliseconds (to wait for responses)
for each future in futures:
// parse JSON response
The code works mostly fine, however, it is seriously impacted by the strength of the internet connection, as if it is bad, 80% of the requests don't get their responses, as the waiting time wasn't enough.
The question is if this is the correct way to do this, and if not, what would it be. Also, should I rather wait for all requests to get a response (like with sending a requests sinchronously and using .join()), and if yes, how can I do that.
If all your API calls are independent then you can fire first, then you can join the results later.
Simple example with GET:
List<URI> uris = ... //
HttpClient client = HttpClient.newHttpClient();
List<HttpRequest> requests = uris.stream()
.map(HttpRequest::newBuilder)
.map(reqBuilder -> reqBuilder.build())
.collect(toList());
CompletableFuture.allOf(requests.stream()
.map(request -> client.sendAsync(request, ofString()))
.toArray(CompletableFuture<?>[]::new))
.join();
You might benefit from using a custom executor as well:
private static final ExecutorService executorService = Executors.newFixedThreadPool(5);
private static final HttpClient httpClient = HttpClient.newBuilder()
.executor(executorService)
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.build();
Edited----
If you want results, then allOf does not help as it actually returns a VOID.
static CompletableFuture allOf(CompletableFuture<?>... cfs)
Returns a new CompletableFuture that is completed when all of the
given CompletableFutures complete.
Instead you can still use join, however in a different way:
List<CompleteFuture<HttpResponse<String>> listOfCompletableFutures = ...
listOfCompletableFutures.
.stream()
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList());
Good references:
https://openjdk.java.net/groups/net/httpclient/recipes.html
Java 11 HttpClient - What is Optimum Ratio of HttpClients to Concurrent HttpRequests
https://mkyong.com/java/java-11-httpclient-examples/
Java collecting results of CompletableFuture from multiple calls

Async method followed by a parallelly executed method in Java 8

After spending the day of learning about the java Concurrency API, I still dont quite get how could I create the following functionality with the help of CompletableFuture and ExecutorService classes:
When I get a request on my REST endpoint I need to:
Start an asynchronous task (includes DB query, filtering, etc.), which will give me a list of String URLs at the end
In the meanwhile, responde back to the REST caller with HTTP OK, that the request was received, I'm working on it
When the asynchronous task is finished, I need to send HTTP requests (with the payload, the REST caller gave me) to the URLs I got from the job. At most the number of URLs would be around a 100, so I need these to happen in parallel.
Ideally I have some syncronized counter which counts how many of the http requests were a success/fail, and I can send this information back to the REST caller (the URL I need to send it back to is provided inside the request payload).
I have the building blocks (methods like: getMatchingObjectsFromDB(callerPayload), getURLs(resultOfgetMachingObjects), sendHttpRequest(Url, methodType), etc...) written for these already, I just cant quite figure out how to tie step 1 and step 3 together. I would use CompletableFuture.supplyAsync() for step 1, then I would need the CompletableFuture.thenComponse method to start step 3, but it's not clear to me how parallelism can be done with this API. It is rather intuitive with ExecutorService executor = Executors.newWorkStealingPool(); though, which creates a thread pool based on how much processing power is available and the tasks can be submitted via the invokeAll() method.
How can I use CompletableFutureand ExecutorService together? Or how can I guarantee parallel execution of a list of tasks with CompletableFuture? Demonstrating code snippet would be much appreciated. Thanks.
You should use join() to wait for all thread finish.
Create Map<String, Boolean> result to store your request result.
In your controller:
public void yourControllerMethod() {
CompletableFuture.runAsync(() -> yourServiceMethod());
}
In your service:
// Execute your logic to get List<String> urls
List<CompletableFuture> futures = urls.stream().map(v ->
CompletableFuture.supplyAsync(url -> requestUrl(url))
.thenAcceptAsync(requestResult -> result.put(url, true or false))
).collect(toList()); // You have list of completeable future here
Then use .join() to wait for all thread (Remember that your service are executed in its own thread already)
CompletableFuture.allOf(futures).join();
Then you can determine which one success/fail by accessing result map
Edit
Please post your proceduce code so that other may understand you also.
I've read your code and here are the needed modification:
When this for loop was not commented out, the receiver webserver got
the same request twice,
I dont understand the purpose of this for loop.
Sorry in my previous answer, I did not clean it up. That's just a temporary idea on my head that I forgot to remove at the end :D
Just remove it from your code
// allOf() only accepts arrays, so the List needed to be converted
/* The code never gets over this part (I know allOf() is a blocking call), even long after when the receiver got the HTTP request
with the correct payload. I'm not sure yet where exactly the code gets stuck */
Your map should be a ConcurrentHashMap because you're modifying it concurrently later.
Map<String, Boolean> result = new ConcurrentHashMap<>();
If your code still does not work as expected, I suggest to remove the parallelStream() part.
CompletableFuture and parallelStream use common forkjoin pool. I think the pool is exhausted.
And you should create your own pool for your CompletableFuture:
Executor pool = Executors.newFixedThreadPool(10);
And execute your request using that pool:
CompletableFuture.supplyAsync(YOURTASK, pool).thenAcceptAsync(Yourtask, pool)
For the sake of completion here is the relevant parts of the code, after clean-up and testing (thanks to Mạnh Quyết Nguyễn):
Rest controller class:
#POST
#Path("publish")
public Response publishEvent(PublishEvent eventPublished) {
/*
Payload verification, etc.
*/
//First send the event to the right subscribers, then send the resulting hashmap<String url, Boolean subscriberGotTheRequest> back to the publisher
CompletableFuture.supplyAsync(() -> EventHandlerService.propagateEvent(eventPublished)).thenAccept(map -> {
if (eventPublished.getDeliveryCompleteUri() != null) {
String callbackUrl = Utility
.getUri(eventPublished.getSource().getAddress(), eventPublished.getSource().getPort(), eventPublished.getDeliveryCompleteUri(), isSecure,
false);
try {
Utility.sendRequest(callbackUrl, "POST", map);
} catch (RuntimeException e) {
log.error("Callback after event publishing failed at: " + callbackUrl);
e.printStackTrace();
}
}
});
//return OK while the event publishing happens in async
return Response.status(Status.OK).build();
}
Service class:
private static List<EventFilter> getMatchingEventFilters(PublishEvent pe) {
//query the database, filter the results based on the method argument
}
private static boolean sendRequest(String url, Event event) {
//send the HTTP request to the given URL, with the given Event payload, return true if the response is positive (status code starts with 2), false otherwise
}
static Map<String, Boolean> propagateEvent(PublishEvent eventPublished) {
// Get the event relevant filters from the DB
List<EventFilter> filters = getMatchingEventFilters(eventPublished);
// Create the URLs from the filters
List<String> urls = new ArrayList<>();
for (EventFilter filter : filters) {
String url;
try {
boolean isSecure = filter.getConsumer().getAuthenticationInfo() != null;
url = Utility.getUri(filter.getConsumer().getAddress(), filter.getPort(), filter.getNotifyUri(), isSecure, false);
} catch (ArrowheadException | NullPointerException e) {
e.printStackTrace();
continue;
}
urls.add(url);
}
Map<String, Boolean> result = new ConcurrentHashMap<>();
Stream<CompletableFuture> stream = urls.stream().map(url -> CompletableFuture.supplyAsync(() -> sendRequest(url, eventPublished.getEvent()))
.thenAcceptAsync(published -> result.put(url, published)));
CompletableFuture.allOf(stream.toArray(CompletableFuture[]::new)).join();
log.info("Event published to " + urls.size() + " subscribers.");
return result;
}
Debugging this was a bit harder than usual, sometimes the code just magically stopped. To fix this, I only put code parts into the async task which was absolutely necessary, and I made sure the code in the task was using thread-safe stuff. Also I was a dumb-dumb at first, and my methods inside the EventHandlerService.class used the synchronized keyword, which resulted in the CompletableFuture inside the Service class method not executing, since it uses a thread pool by default.
A piece of logic marked with synchronized becomes a synchronized block, allowing only one thread to execute at any given time.

RxJava - Emit items like in a queue

I have an observable that:
emits data after few seconds.
can be triggered several times.
the operation can't be executed in parallel. So we need a buffer.
I understand that this isn't clear so let me explain with example:
Observable<IPing> pingObservable = Observable.defer(() ->
new PingCommand(account, folders)
.post()
.asObservable()
);
this is the main feature. It shouldn't be called again while a previous one is executing, but it should remember that user requests it again. So I created close buffer as PublishSubject
closeBuffer = PublishSubject.create();
now I'm wondering how to merge it.
I have tried this:
Observable.defer(() -> new PingCommand(account, folders)
.post()
.asObservable()
.buffer(() -> closeBuffer)
.flatMap(Observable::from)
.first()
);
but it is not working as I want.
Edit:
I will try to explain that better:
I'm sending POST to the server - We can wait for a response several MINUTES (because it is Exchange ActiveSync PUSH). I cannot ping again while one request is sending. So I have to wait until one request is done. I don't need to buffer those observables - just information if an user is requesting ping - and send request after a first one is done. I'm just learning reactive so I don't know how to really use complicated functions like backpressure.
This is how I want this problem to be solved (pseudo code)
??????<Result> request
= ????.???()
.doOnNext( result -> { … })
.doOnSubscribe(() -> { … })
.doOnCompleted(() -> { … })
.…
//__________________________________________________________
Observable<Result> doAsyncWork(Data data) { … } // this is API function
//__________________________________________________________
// api usage example
Subscription s1 = doAsyncWork(someData).subscribe() // start observing async work; executed doOnSubscribe
Subscription s2 = doAsyncWork(someData).subscribe() // wait for async work result …
//__________________________________________________________
// after some time pass, maybe from other thread
Subscription s1 = doAsyncWork(someData).subscribe() // wait for async work result …
//__________________________________________________________
// async work completes, all subscribers obtain the same result; executed doOnCompleted
//__________________________________________________________
// again
Subscription s1 = doAsyncWork(someData).subscribe() // start observing async work; executed doOnSubscribe
// async work completes, subscriber obtains result; executed doOnCompleted
Obviously, I can use if instead but I want to know how to do it in a proper way.

concurrent http request to independent web services

i'm trying to find a simple way to send http request concurrently to diferent web services. each request is completely independent of each other.
currently, my implementation look like this ( just a simplification, don't pay attention to design )
let's say a i have a List queries;
public class Service {
private List<HttpClient> httpClients; // one for each web service
public List<QueryResult> doQueries(List<Query> queries) {
ExecutorService service = Executors.... ;
List<Callable<QueryResult>> .... ;
for ( Query q : queries ) {
Future<> .....
}
service.invokeAll(...) ;
***// what should i do from here ?
// how should i wait all those tasks to finish ?***
}
}
my question is specifically that.
how do i wait ?
You seem to create a list of Callable and each callable will return result of type QueryResult as clear from List<Callable<QueryResult>>. You will get Future after submitting them to ExecutorService. So use code in this way:
List<Future<QueryResult >> futures = executorService.invokeAll(callables);
for(Future<QueryResult> future : futures){
System.out.println("future.get = " + future.get());
}
executorService.shutdown();
If you want to set some maximum time to wait for result you can use awaitTermination method as well. IMO ExecutorCompletionService is more suited for your requirements and you can read about it in my article at dzone.
You have 3 choices:
execute each request on a separate thread. Since each thread consumes a lot of memory, you can get OutOfMemoreError if >100 requests run in parallel.
limit the number of threads as akhil_mittal suggested. The number of concurrent requests will be also limited.
Use an async io library, e.g. nio2. They allow thousands of simultaneous requests with moderate memory consumption.

Parallel DB/WS Calls

I am building a set of web services with the intent to aggregate similar data sets across multiple backends (through db calls and service calls). Some of the queries could take more than a couple of seconds to run, and if I stack these requests sequentially, there is a chance the total run time would be outside of the desired response time.
I am hoping to make the calls in parallel, collect all results and then aggregate. What is the best approach to tackling this?
The services will be deployed to Websphere 6.1 (so java 5, j2ee 1.4).
Any information would be greatly appreciated.
Look into the java.util.concurrent API. In particular, you can create a thread Executor, pass in Callables, and get back Futures, that will be run asynchronously.
Your code will look something like this:
ExecutorService exec = Executors.newCachedThreadPool();
Future<ReplyA> raFuture = exec.submit(new Callable<ReplyA>() {
public ReplyA call() {
// call remote service here.
return new ReplyA(...);
}});
Future<ReplyB> rbFuture = exec.submit(new Callable<ReplyB>() {
public ReplyB call() {
// call remote service here.
return new ReplyB(...);
}});
ReplyA replyA = raFuture.get();
ReplyB replyB = rbFuture.get();
exec.shutdown();
You can also use the timeout versions of get() so you can do something reasonable if the responses are taking too long. If you decide to take this path, you would probably be better served with the ExecutorService's invokeAll method, so the timeout will apply to all of the Callables as a group:
Callable<Reply> taskA = new Callable<ReplyA>() { ... };
Callable<Reply> taskB = new Callable<ReplyB>() { ... };
List<Callable<Reply>> tasks = Arrays.asList(taskA, taskB);
List<Future<Reply>> futures = exec.invokeAll(tasks, 20, TimeUnit.SECONDS);
for(Future<Reply> future: futures) {
if(replyFuture.isCancelled()) {
// deal with it
} else {
Reply reply = future.get();
// do something with the reply.
}
}
Start daemon threads, let them do the job, join() them all, aggregate.
Another possible way is to place the queries into a Queue, and let MDBs handle one request at a time. Aggregation is a bit more confusing though -- have to get results from another queue, and accumulate them between replies, and handle the errors... uh, common, just do the threads! :)
RequestAThread rath = new RequestAThread(dataForRequestA);
RequestBThread rbth = new RequestBThread(dataForRequestB);
...
rath.start();
rbth.start();
...
rath.join();
ReplyA ra = rath.getReply();
rbth.join();
ReplyB rb = rbth.getReply();
Result r = aggregate(ra,rb);
Error handling by taste.

Categories

Resources