我有一个很大的文本文件要处理。方法parse()尝试使用多个线程将请求发送到静态服务以获取结果。对于每个inputInfo,处理后应返回ExtractedInfo。
private ExecutorService executorService = Executors.newFixedThreadPool(10);
public ExtractedInfo parse(InputInfo inputInfo) {
ExtractedInfo extractedInfo = new ExtractedInfo();
String inputText = inputInfo.getText();
Callable<String> parseTask = () -> this.executeTask(inputText);
Future<String> parseService = executorService.submit(parseTask);
String result = null;
try {
result = parseService.get(Parameters.getParserTimeLimit(), TimeUnit.MILLISECONDS);
} catch (ExecutionException | InterruptedException e) {
LOGGER.warn("ExecutionException | InterruptedException");
e.printStackTrace();
} catch (TimeoutException te) {
parseService.cancel(true);
LOGGER.warn("NLP parser time out for {} {} {}", sku, inputText, msgId);
}
if (result != null) {
extractedInfo.setResult(result);
}
return extractedInfo;
}
私有方法“ executeTask”正在将请求发送到由python应用程序处理的静态服务。
private String executeTask(String inputText) {
String jsonOutput = null;
String urlStr = null;
try {
String encodedInputText = URLEncoder.encode("\"" + inputText + "\"", "UTF-8");
urlStr = parserServerSelection();
URL url = new URL(urlStr + encodedInputText);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
jsonOutput = in.readLine();
in.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonOutput;
}
这是正确的方法吗?