У меня есть огромные xmls, в результате у меня много xpaths для извлечения записей из xml. Поэтому я пытаюсь создать несколько потоков, чтобы каждый xpath мог оцениваться в другом потоке. Но я получаю ошибки ниже, это код фрагмент, который может дать хорошее представление, я использовал здесь очень маленький xml для краткости. Я создаю 3 потока и ставлю в очередь 10 задач.
import java.io.File;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.ximpleware.AutoPilot;
import com.ximpleware.EOFException;
import com.ximpleware.EncodingException;
import com.ximpleware.EntityException;
import com.ximpleware.ParseException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
public class MultiThread {
public static void main(String args[]) throws InterruptedException, ExecutionException, EncodingException, EOFException, EntityException, ParseException
{
String str="<library><booked>book</booked> <book id=\"1\"> <title>Googled By God</title> </book> </library>";
File f = new File("/home/cloudera/wos.xml");
byte[] ba =null;;
ExecutorService executor = Executors.newFixedThreadPool(3);
List<Task> extractorTasks = new ArrayList<Task>();
VTDGen vg = new VTDGen();
vg.setDoc(str.getBytes());
vg.parse(false);
//add 10 tasks
for(int i=0;i<10;i++)
{
Task d = new Task(str.getBytes(),vg,"/library/book/title");
extractorTasks.add(d);
}
List<Future<String>> output = executor.invokeAll(extractorTasks);
executor.shutdown();
}
}
class Task implements Callable<String> {
VTDGen vg = null;
String xpath = "";
byte [] ba=null;
AutoPilot ap = null;
Task(byte[] _ba,VTDGen _vg,String _xpath)
{
ba = _ba;
vg = _vg;
xpath = _xpath;
}
public String call() throws Exception
{
String title = "";
try
{
/* if we uncomment below 3 lines, all works well, thats becuase we are reparsing the whole document*/
//vg = new VTDGen();
//vg.setDoc(ba);
//vg.parse(false);
VTDNav vn = vg.getNav();
ap = new AutoPilot(vn);
ap.selectXPath(xpath);
//Get all the titles and print each of those
while(ap.evalXPath() != -1)
{
//getText will return the index of the VTDRecord
int titleIndex = vn.getText();
//Get the text of the VTDRecord
title = vn.toNormalizedString(titleIndex);
System.out.println("Title is "+title);
}
vn.toElement(VTDNav.ROOT);
}
catch (Exception e) {
e.printStackTrace();
}
return title;
}
}