我正在尝试在 Controller 内执行一些并行方法,但我在返回类型方面遇到了一些问题。
我有 4 个方法,每个方法都返回一个列表。我需要以并行方式执行这些方法,然后获取每个返回的列表,并将所有这些列表放入映射中,并从 Controller 返回该映射。 代码如下:
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;
我很确定我没有做正确的事情,因为它抛出java.lang.ClassCastException:com.test.controller.HomeController$1与java.util.List不兼容
我猜问题是名为 listOfWebsites 的 map ,它应该具有可调用对象的返回类型,一些列表:(
最佳答案
首先,更改可调用对象的类型以避免强制转换:
Callable<List<?>> callable1 = new Callable<List>(){...}
然后,你必须等待并行计算的结果。您不需要创建 CompletableFuture
,因为您可以直接从 ExecutorService
获取 Future
:
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;
就是这样。
关于java - Spring Boot 在 Controller 内执行并行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361625/