0
我想调用我的web服务,它返回一个csv文件。因此,我调用的每个URL都有一个后缀,该后缀是一个字符串,表示要生成哪个csv。然后我想将这个csv保存到一个文件中。有很多要生成,所以我从多个线程调用这个类。每次运行程序时,完成时返回的文件数量都是不同的。该web服务运行良好,因为如果我从浏览器手动调用一个没有创建文件的URL,就会生成并下载csv。总共有大约15,000个URL由12个线程调用。我在输出控制台中没有收到任何错误,只是说构建成功了。通常每个运行生成大约1500-2000个文件而不是15000.我使用apache的通用io FileUtils库将url保存到文件。常见的io copyUrlToFile不能正常工作
final int maxRetries = 5;
HistoryWebservice historyWS = new HistoryWebservice();
String history = historyWS.getHistory(maxRetries);
public class HistoryWebservice {
public String getHistory(int maxRetries, String str) throws Exception {
maxRetries = Math.max(0, maxRetries); //Make sure we don't have a negative retry counter
while (maxRetries >= 0) {
try {
FileUtils.copyURLToFile("http://myservice.myservice/" + str, "/tmp/file" + str);
}
catch(<Catch the timeout exception>) {
maxRetries--;
}
}
throw new TimeoutException("Max retries exceeded");
}
}