我试图从两个不同的文件中读取数据,一个是csv格式,另一个文件是xml数据。使用completeFuture,我试图从两个异步文件中读取数据。我收到类型转换错误。请让我知道我是否在下面的代码中按照正确的方法使用completefuture对象]
例外:
java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.util.function.Supplier (java.util.ArrayList and java.util.function.Supplier are in module java.base of loader 'bootstrap')
从主线程读取数据的代码
CompletableFuture<Object> csvdata = CompletableFuture.supplyAsync((Supplier<Object>) processdatacsv());
CompletableFuture<Object> xmldata1 = CompletableFuture.supplyAsync((Supplier<Object>) processxmldata());
List<String[]> csvfiledata = null;
if (csvdata.isDone())
csvfiledata = (List<String[]>) csvdata.get();
List<String[]> xmlfiledata = null;
if (xmldata1.isDone())
xmlfiledata = (List<String[]>) xmldata1.get();
private List<String[]> processdatacsv() {
CSVReader reader = null;
Resource resource1 = new ClassPathResource("sample.csv");
try {
String csvFile = resource1.getFile().toString();
reader = new CSVReader(new FileReader(csvFile));
return reader.readAll();
} catch (Exception e) {
LOGGER.error("Error while process csv records");
return null;
}
}
private List<String[]> processxmldata() {
Resource resource = new ClassPathResource("sample.xml");
File file;
try {
file = resource.getFile();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("record");
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
List<String[]> dataList = new ArrayList<String[]>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
String[] data = new String[3];
data[0] = eElement.getAttribute("reference").toString();
data[1] = eElement.getElementsByTagName("Number").item(0).getTextContent().toString();
data[2] = eElement.getElementsByTagName("des").item(0).getTextContent().toString();
}
}
return dataList;
} catch (Exception e) {
LOGGER.error("Error while process csv records");
return null;
}
}
就像例外情况一样:您不能只将列表投射到供应商对象。 Supplier是一种对象,具有get方法来请求其提供的对象(在您的情况下为列表)。
在您的代码中,文件不会被异步加载,因为您是直接调用方法processdatacsv
和processxmldata
,因此将直接执行它们。要稍后(在另一个线程中)执行方法,您需要提供者(以便另一个线程可以调用get方法并执行代码)。
在您提供的问题代码中,像这样使用lambda expressions是最容易的:
CompletableFuture<List<String[]>> csvdata = CompletableFuture.supplyAsync(() -> processdatacsv());
CompletableFuture<List<String[]>> xmldata1 = CompletableFuture.supplyAsync(() -> processxmldata());
在此代码段中,() -> processdatacsv()
部分的解释如下:创建一个新的Supplier对象,该对象从其方法processdatacsv
中调用方法get
。
或者您可以这样做(可能更容易理解):
CompletableFuture<List<String[]>> csvdata = CompletableFuture.supplyAsync(new Supplier<List<String[]>>() {
public List<String[]> get() {
processdatacsv();
}
});
CompletableFuture<List<String[]>> csvdata = CompletableFuture.supplyAsync(new Supplier<List<String[]>>() {
public List<String[]> get() {
processxmldata();
}
});
对于CompletableFuture.supplyAsync
,您需要一个Supplier
。这不能通过将列表强制转换为Supplier<Object>
类型来完成。正确的方式是这样的:
CompletableFuture<List<String[]>> xmldata1 = CompletableFuture.supplyAsync(() -> processxmldata());
使用Supplier
的原因是,评估不能立即从构造Supplier
的语句开始。 Future
将在异步线程中开始评估。
稍后,如果您确实需要结果,则必须等待。最简单的方法是:
xmlfiledata = xmldata1.get();
看看get方法的javadoc。