Spring Boot executing parallel methods inside a controller
I'm trying to execute some parallel methods inside my controller but i'm having a few problems with the return type.
I have 4 methods and each of those methods return a list.I need to execute those methods in a parallel way and then get each returned list and place all of those lists inside a map and return that map from the controller.
Here's the code:
Callable<List> callable1 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutovit> lista;
lista = scrapperAutovit.searchAutovit(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa,
orasParam);
return lista;
}
};
Callable<List> callable2 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultOlx> listaOlx;
String marcaOlx = marcaId.toLowerCase();
String modelOlx = modelId.toLowerCase();
String orasOlx = orasParam.toLowerCase();
listaOlx = scrapperOlx.searchOlx(marcaOlx, modelOlx, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasOlx);
return listaOlx;
}
};
Callable<List> callable3 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultPubli24> listaPubli24;
String orasPubli24 = orasParam.toLowerCase();
listaPubli24 = scrapperPubli24.searchPubli24(marcaId, modelId, orasPubli24, anFabrDeLa, anFabrPanaLa, pretDeLa, pretPanaLa);
return listaPubli24;
}
};
Callable<List> callable4 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutoUncle> listaAutoUncle;
listaAutoUncle = scrapperAutoUncle.searchAutoUncle(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasParam);
return listaAutoUncle;
}
};
//add to a list
List<Callable<List>> taskList = new ArrayList<Callable<List>>();
taskList.add(callable1);
taskList.add(callable2);
taskList.add(callable3);
taskList.add(callable4);
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.invokeAll(taskList);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", (List<?>) taskList.get(0));
listOfWebsites.put("listaOlx", (List<?>) taskList.get(1));
listOfWebsites.put("listaPubli24", (List<?>) taskList.get(2));
listOfWebsites.put("listaAutoUncle", (List<?>) taskList.get(3));
return listOfWebsites;
I'm pretty sure that i'm not doing something right because it throws java.lang.ClassCastException: com.test.controller.HomeController$1 incompatible with java.util.List
I guess the problem is the map called listOfWebsites which should have the returned type of the callables, some lists:(
java multithreading concurrency
add a comment |
I'm trying to execute some parallel methods inside my controller but i'm having a few problems with the return type.
I have 4 methods and each of those methods return a list.I need to execute those methods in a parallel way and then get each returned list and place all of those lists inside a map and return that map from the controller.
Here's the code:
Callable<List> callable1 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutovit> lista;
lista = scrapperAutovit.searchAutovit(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa,
orasParam);
return lista;
}
};
Callable<List> callable2 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultOlx> listaOlx;
String marcaOlx = marcaId.toLowerCase();
String modelOlx = modelId.toLowerCase();
String orasOlx = orasParam.toLowerCase();
listaOlx = scrapperOlx.searchOlx(marcaOlx, modelOlx, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasOlx);
return listaOlx;
}
};
Callable<List> callable3 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultPubli24> listaPubli24;
String orasPubli24 = orasParam.toLowerCase();
listaPubli24 = scrapperPubli24.searchPubli24(marcaId, modelId, orasPubli24, anFabrDeLa, anFabrPanaLa, pretDeLa, pretPanaLa);
return listaPubli24;
}
};
Callable<List> callable4 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutoUncle> listaAutoUncle;
listaAutoUncle = scrapperAutoUncle.searchAutoUncle(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasParam);
return listaAutoUncle;
}
};
//add to a list
List<Callable<List>> taskList = new ArrayList<Callable<List>>();
taskList.add(callable1);
taskList.add(callable2);
taskList.add(callable3);
taskList.add(callable4);
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.invokeAll(taskList);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", (List<?>) taskList.get(0));
listOfWebsites.put("listaOlx", (List<?>) taskList.get(1));
listOfWebsites.put("listaPubli24", (List<?>) taskList.get(2));
listOfWebsites.put("listaAutoUncle", (List<?>) taskList.get(3));
return listOfWebsites;
I'm pretty sure that i'm not doing something right because it throws java.lang.ClassCastException: com.test.controller.HomeController$1 incompatible with java.util.List
I guess the problem is the map called listOfWebsites which should have the returned type of the callables, some lists:(
java multithreading concurrency
What do you mean? This code is not inside the constructor
– Truica Sorin
Nov 18 '18 at 13:59
Sorry, misread the question. I'm in a noisy room. I'll remove my comment.
– duffymo
Nov 18 '18 at 14:10
HintHomeController$1
is yourCallable
... More generally though, you should plan to do the whole process asynchronously and return aFuture
from your controller method.
– Boris the Spider
Nov 18 '18 at 14:10
Also - all your lists are rawtypes...
– Boris the Spider
Nov 18 '18 at 14:21
add a comment |
I'm trying to execute some parallel methods inside my controller but i'm having a few problems with the return type.
I have 4 methods and each of those methods return a list.I need to execute those methods in a parallel way and then get each returned list and place all of those lists inside a map and return that map from the controller.
Here's the code:
Callable<List> callable1 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutovit> lista;
lista = scrapperAutovit.searchAutovit(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa,
orasParam);
return lista;
}
};
Callable<List> callable2 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultOlx> listaOlx;
String marcaOlx = marcaId.toLowerCase();
String modelOlx = modelId.toLowerCase();
String orasOlx = orasParam.toLowerCase();
listaOlx = scrapperOlx.searchOlx(marcaOlx, modelOlx, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasOlx);
return listaOlx;
}
};
Callable<List> callable3 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultPubli24> listaPubli24;
String orasPubli24 = orasParam.toLowerCase();
listaPubli24 = scrapperPubli24.searchPubli24(marcaId, modelId, orasPubli24, anFabrDeLa, anFabrPanaLa, pretDeLa, pretPanaLa);
return listaPubli24;
}
};
Callable<List> callable4 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutoUncle> listaAutoUncle;
listaAutoUncle = scrapperAutoUncle.searchAutoUncle(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasParam);
return listaAutoUncle;
}
};
//add to a list
List<Callable<List>> taskList = new ArrayList<Callable<List>>();
taskList.add(callable1);
taskList.add(callable2);
taskList.add(callable3);
taskList.add(callable4);
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.invokeAll(taskList);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", (List<?>) taskList.get(0));
listOfWebsites.put("listaOlx", (List<?>) taskList.get(1));
listOfWebsites.put("listaPubli24", (List<?>) taskList.get(2));
listOfWebsites.put("listaAutoUncle", (List<?>) taskList.get(3));
return listOfWebsites;
I'm pretty sure that i'm not doing something right because it throws java.lang.ClassCastException: com.test.controller.HomeController$1 incompatible with java.util.List
I guess the problem is the map called listOfWebsites which should have the returned type of the callables, some lists:(
java multithreading concurrency
I'm trying to execute some parallel methods inside my controller but i'm having a few problems with the return type.
I have 4 methods and each of those methods return a list.I need to execute those methods in a parallel way and then get each returned list and place all of those lists inside a map and return that map from the controller.
Here's the code:
Callable<List> callable1 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutovit> lista;
lista = scrapperAutovit.searchAutovit(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa,
orasParam);
return lista;
}
};
Callable<List> callable2 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultOlx> listaOlx;
String marcaOlx = marcaId.toLowerCase();
String modelOlx = modelId.toLowerCase();
String orasOlx = orasParam.toLowerCase();
listaOlx = scrapperOlx.searchOlx(marcaOlx, modelOlx, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasOlx);
return listaOlx;
}
};
Callable<List> callable3 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultPubli24> listaPubli24;
String orasPubli24 = orasParam.toLowerCase();
listaPubli24 = scrapperPubli24.searchPubli24(marcaId, modelId, orasPubli24, anFabrDeLa, anFabrPanaLa, pretDeLa, pretPanaLa);
return listaPubli24;
}
};
Callable<List> callable4 = new Callable<List>()
{
@Override
public List call() throws Exception
{
List<SearchResultAutoUncle> listaAutoUncle;
listaAutoUncle = scrapperAutoUncle.searchAutoUncle(marcaId, modelId, pretDeLa, pretPanaLa, anFabrDeLa, anFabrPanaLa, orasParam);
return listaAutoUncle;
}
};
//add to a list
List<Callable<List>> taskList = new ArrayList<Callable<List>>();
taskList.add(callable1);
taskList.add(callable2);
taskList.add(callable3);
taskList.add(callable4);
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.invokeAll(taskList);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", (List<?>) taskList.get(0));
listOfWebsites.put("listaOlx", (List<?>) taskList.get(1));
listOfWebsites.put("listaPubli24", (List<?>) taskList.get(2));
listOfWebsites.put("listaAutoUncle", (List<?>) taskList.get(3));
return listOfWebsites;
I'm pretty sure that i'm not doing something right because it throws java.lang.ClassCastException: com.test.controller.HomeController$1 incompatible with java.util.List
I guess the problem is the map called listOfWebsites which should have the returned type of the callables, some lists:(
java multithreading concurrency
java multithreading concurrency
edited Nov 18 '18 at 13:54
duffymo
269k32315503
269k32315503
asked Nov 18 '18 at 13:51
Truica SorinTruica Sorin
717
717
What do you mean? This code is not inside the constructor
– Truica Sorin
Nov 18 '18 at 13:59
Sorry, misread the question. I'm in a noisy room. I'll remove my comment.
– duffymo
Nov 18 '18 at 14:10
HintHomeController$1
is yourCallable
... More generally though, you should plan to do the whole process asynchronously and return aFuture
from your controller method.
– Boris the Spider
Nov 18 '18 at 14:10
Also - all your lists are rawtypes...
– Boris the Spider
Nov 18 '18 at 14:21
add a comment |
What do you mean? This code is not inside the constructor
– Truica Sorin
Nov 18 '18 at 13:59
Sorry, misread the question. I'm in a noisy room. I'll remove my comment.
– duffymo
Nov 18 '18 at 14:10
HintHomeController$1
is yourCallable
... More generally though, you should plan to do the whole process asynchronously and return aFuture
from your controller method.
– Boris the Spider
Nov 18 '18 at 14:10
Also - all your lists are rawtypes...
– Boris the Spider
Nov 18 '18 at 14:21
What do you mean? This code is not inside the constructor
– Truica Sorin
Nov 18 '18 at 13:59
What do you mean? This code is not inside the constructor
– Truica Sorin
Nov 18 '18 at 13:59
Sorry, misread the question. I'm in a noisy room. I'll remove my comment.
– duffymo
Nov 18 '18 at 14:10
Sorry, misread the question. I'm in a noisy room. I'll remove my comment.
– duffymo
Nov 18 '18 at 14:10
Hint
HomeController$1
is your Callable
... More generally though, you should plan to do the whole process asynchronously and return a Future
from your controller method.– Boris the Spider
Nov 18 '18 at 14:10
Hint
HomeController$1
is your Callable
... More generally though, you should plan to do the whole process asynchronously and return a Future
from your controller method.– Boris the Spider
Nov 18 '18 at 14:10
Also - all your lists are rawtypes...
– Boris the Spider
Nov 18 '18 at 14:21
Also - all your lists are rawtypes...
– Boris the Spider
Nov 18 '18 at 14:21
add a comment |
2 Answers
2
active
oldest
votes
First, change type of callables to avoid casting:
Callable<List<?>> callable1 = new Callable<List>(){...}
Then, you have to wait for results of parallel computations. You need not to create CompletableFuture
s because you can obtain Future
s directly from ExecutorService
:
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<List<?>> future1 = executor.submit(callable1);
Future<List<?>> future2 = executor.submit(callable2);
Future<List<?>> future3 = executor.submit(callable3);
Future<List<?>> future4 = executor.submit(callable4);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", future1.get());
listOfWebsites.put("listaOlx", future2.get());
listOfWebsites.put("listaPubli24", future3.get());
listOfWebsites.put("listaAutoUncle", future4.get());
return listOfWebsites;
That's it.
add a comment |
I would suggest to use completableFuture Object with supplyasync method.
Giving you approach like below:
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> scrapperAutovit.searchAutovit);
CompletableFuture<String> completableFuture1
= CompletableFuture.supplyAsync(() -> scrapperOlx.searchOlxwith);
CompletableFuture<String> completableFuture2
= CompletableFuture.supplyAsync(() -> scrapperPubli24.searchPubli24);
would be used to check whether you got some response from above call or not.
Like that you can open parallel thread for scrapperOlx.searchOlxwith
and scrapperPubli24.searchPubli24
completableFuture object and wait for execution to finish there task and after that consolidate all data and you can process further.
For more details you can refer below link
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
https://dzone.com/articles/java-8-definitive-guide
https://www.callicoder.com/java-8-completablefuture-tutorial/
This is the good example to understand completablefuture
Multiple thenApply in a completableFuture
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361625%2fspring-boot-executing-parallel-methods-inside-a-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, change type of callables to avoid casting:
Callable<List<?>> callable1 = new Callable<List>(){...}
Then, you have to wait for results of parallel computations. You need not to create CompletableFuture
s because you can obtain Future
s directly from ExecutorService
:
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<List<?>> future1 = executor.submit(callable1);
Future<List<?>> future2 = executor.submit(callable2);
Future<List<?>> future3 = executor.submit(callable3);
Future<List<?>> future4 = executor.submit(callable4);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", future1.get());
listOfWebsites.put("listaOlx", future2.get());
listOfWebsites.put("listaPubli24", future3.get());
listOfWebsites.put("listaAutoUncle", future4.get());
return listOfWebsites;
That's it.
add a comment |
First, change type of callables to avoid casting:
Callable<List<?>> callable1 = new Callable<List>(){...}
Then, you have to wait for results of parallel computations. You need not to create CompletableFuture
s because you can obtain Future
s directly from ExecutorService
:
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<List<?>> future1 = executor.submit(callable1);
Future<List<?>> future2 = executor.submit(callable2);
Future<List<?>> future3 = executor.submit(callable3);
Future<List<?>> future4 = executor.submit(callable4);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", future1.get());
listOfWebsites.put("listaOlx", future2.get());
listOfWebsites.put("listaPubli24", future3.get());
listOfWebsites.put("listaAutoUncle", future4.get());
return listOfWebsites;
That's it.
add a comment |
First, change type of callables to avoid casting:
Callable<List<?>> callable1 = new Callable<List>(){...}
Then, you have to wait for results of parallel computations. You need not to create CompletableFuture
s because you can obtain Future
s directly from ExecutorService
:
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<List<?>> future1 = executor.submit(callable1);
Future<List<?>> future2 = executor.submit(callable2);
Future<List<?>> future3 = executor.submit(callable3);
Future<List<?>> future4 = executor.submit(callable4);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", future1.get());
listOfWebsites.put("listaOlx", future2.get());
listOfWebsites.put("listaPubli24", future3.get());
listOfWebsites.put("listaAutoUncle", future4.get());
return listOfWebsites;
That's it.
First, change type of callables to avoid casting:
Callable<List<?>> callable1 = new Callable<List>(){...}
Then, you have to wait for results of parallel computations. You need not to create CompletableFuture
s because you can obtain Future
s directly from ExecutorService
:
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<List<?>> future1 = executor.submit(callable1);
Future<List<?>> future2 = executor.submit(callable2);
Future<List<?>> future3 = executor.submit(callable3);
Future<List<?>> future4 = executor.submit(callable4);
Map<String,List<?>> listOfWebsites = new HashMap<>();
listOfWebsites.put("listaAutovit", future1.get());
listOfWebsites.put("listaOlx", future2.get());
listOfWebsites.put("listaPubli24", future3.get());
listOfWebsites.put("listaAutoUncle", future4.get());
return listOfWebsites;
That's it.
edited Nov 18 '18 at 16:23
answered Nov 18 '18 at 14:58
Alexei KaigorodovAlexei Kaigorodov
9,88611029
9,88611029
add a comment |
add a comment |
I would suggest to use completableFuture Object with supplyasync method.
Giving you approach like below:
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> scrapperAutovit.searchAutovit);
CompletableFuture<String> completableFuture1
= CompletableFuture.supplyAsync(() -> scrapperOlx.searchOlxwith);
CompletableFuture<String> completableFuture2
= CompletableFuture.supplyAsync(() -> scrapperPubli24.searchPubli24);
would be used to check whether you got some response from above call or not.
Like that you can open parallel thread for scrapperOlx.searchOlxwith
and scrapperPubli24.searchPubli24
completableFuture object and wait for execution to finish there task and after that consolidate all data and you can process further.
For more details you can refer below link
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
https://dzone.com/articles/java-8-definitive-guide
https://www.callicoder.com/java-8-completablefuture-tutorial/
This is the good example to understand completablefuture
Multiple thenApply in a completableFuture
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
add a comment |
I would suggest to use completableFuture Object with supplyasync method.
Giving you approach like below:
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> scrapperAutovit.searchAutovit);
CompletableFuture<String> completableFuture1
= CompletableFuture.supplyAsync(() -> scrapperOlx.searchOlxwith);
CompletableFuture<String> completableFuture2
= CompletableFuture.supplyAsync(() -> scrapperPubli24.searchPubli24);
would be used to check whether you got some response from above call or not.
Like that you can open parallel thread for scrapperOlx.searchOlxwith
and scrapperPubli24.searchPubli24
completableFuture object and wait for execution to finish there task and after that consolidate all data and you can process further.
For more details you can refer below link
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
https://dzone.com/articles/java-8-definitive-guide
https://www.callicoder.com/java-8-completablefuture-tutorial/
This is the good example to understand completablefuture
Multiple thenApply in a completableFuture
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
add a comment |
I would suggest to use completableFuture Object with supplyasync method.
Giving you approach like below:
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> scrapperAutovit.searchAutovit);
CompletableFuture<String> completableFuture1
= CompletableFuture.supplyAsync(() -> scrapperOlx.searchOlxwith);
CompletableFuture<String> completableFuture2
= CompletableFuture.supplyAsync(() -> scrapperPubli24.searchPubli24);
would be used to check whether you got some response from above call or not.
Like that you can open parallel thread for scrapperOlx.searchOlxwith
and scrapperPubli24.searchPubli24
completableFuture object and wait for execution to finish there task and after that consolidate all data and you can process further.
For more details you can refer below link
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
https://dzone.com/articles/java-8-definitive-guide
https://www.callicoder.com/java-8-completablefuture-tutorial/
This is the good example to understand completablefuture
Multiple thenApply in a completableFuture
I would suggest to use completableFuture Object with supplyasync method.
Giving you approach like below:
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> scrapperAutovit.searchAutovit);
CompletableFuture<String> completableFuture1
= CompletableFuture.supplyAsync(() -> scrapperOlx.searchOlxwith);
CompletableFuture<String> completableFuture2
= CompletableFuture.supplyAsync(() -> scrapperPubli24.searchPubli24);
would be used to check whether you got some response from above call or not.
Like that you can open parallel thread for scrapperOlx.searchOlxwith
and scrapperPubli24.searchPubli24
completableFuture object and wait for execution to finish there task and after that consolidate all data and you can process further.
For more details you can refer below link
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html
https://dzone.com/articles/java-8-definitive-guide
https://www.callicoder.com/java-8-completablefuture-tutorial/
This is the good example to understand completablefuture
Multiple thenApply in a completableFuture
edited Nov 18 '18 at 14:20
answered Nov 18 '18 at 14:00
GauravRai1512GauravRai1512
58811
58811
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
add a comment |
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
I'm not sure i understand:( where should i use this CompletableFuture?
– Truica Sorin
Nov 18 '18 at 14:03
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
all future object which you have created in callable can use by completablefuture like i have mentioned one example for scrapperAutovit.searchAutovit like you can parallel y run with scrapperOlx.searchOlx this as so on i have also shared few link you just go through this link you will understand its a java8 future object.
– GauravRai1512
Nov 18 '18 at 14:06
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
Note this will use the common pool rather than a custom executor.
– Boris the Spider
Nov 18 '18 at 14:22
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
You can provide the custom executor as well through constructor.public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) { return asyncSupplyStage(screenExecutor(executor), supplier); }
– GauravRai1512
Nov 18 '18 at 14:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361625%2fspring-boot-executing-parallel-methods-inside-a-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean? This code is not inside the constructor
– Truica Sorin
Nov 18 '18 at 13:59
Sorry, misread the question. I'm in a noisy room. I'll remove my comment.
– duffymo
Nov 18 '18 at 14:10
Hint
HomeController$1
is yourCallable
... More generally though, you should plan to do the whole process asynchronously and return aFuture
from your controller method.– Boris the Spider
Nov 18 '18 at 14:10
Also - all your lists are rawtypes...
– Boris the Spider
Nov 18 '18 at 14:21