main
黄海 5 months ago
parent 9bb35884d3
commit ab08621b5d

@ -15,6 +15,8 @@ import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static Tools.Crawler.Util.Util.doRequestWithRetry;
public class BookLesson {
// 使用线程安全的集合
private static final CopyOnWriteArrayList<Record> subjectList = new CopyOnWriteArrayList<>();
@ -65,40 +67,6 @@ public class BookLesson {
System.out.println(DateTime.now() + " " + msg);
}
/**
*
*
* @param url
* @param jsonBody
* @param isGet
* @param maxRetries
* @return
*/
public static String doRequestWithRetry(String url, String jsonBody, boolean isGet, int maxRetries) {
int retries = 0;
while (retries < maxRetries) {
try {
if (isGet) {
return Util.doGet(url);
} else {
return Util.doPost(url, jsonBody);
}
} catch (Exception e) {
retries++;
if (retries == maxRetries) {
print("请求失败: " + url + " 重试次数: " + retries);
throw new RuntimeException("Failed after " + maxRetries + " retries", e);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
}
}
return null;
}
/**
*

@ -14,6 +14,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import static Tools.Crawler.Util.Util.doRequestWithRetry;
public class KnowledgeLesson {
// 用于存储课程信息的线程安全列表
public static List<Record> lessonList = Collections.synchronizedList(new ArrayList<>());
@ -107,7 +109,7 @@ public class KnowledgeLesson {
argBook.put("sortType", 2);
argBook.put("stageCode", stageCode);
argBook.put("subjectCode", subjectCode);
String respBook = BookLesson.doRequestWithRetry(url, argBook.toString(), false, 3);
String respBook = doRequestWithRetry(url, argBook.toString(), false, 3);
if (respBook != null) {
JSONArray jsonArrSource = JSONObject.parseObject(respBook)
@ -202,7 +204,7 @@ public class KnowledgeLesson {
subjectCode, subjectMap.get(subjectCode));
String url = "https://yx.ccsjy.cn/api/business/v1/knowledge/tree";
String respBook = BookLesson.doRequestWithRetry(url, argBook.toString(), false, 3);
String respBook = doRequestWithRetry(url, argBook.toString(), false, 3);
if (respBook != null) {
try {
List<Record> allRecords = new ArrayList<>();
@ -242,13 +244,11 @@ public class KnowledgeLesson {
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (Record record : listKnowledge) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
collectLesson(
record.getStr("node_id"),
record.getStr("stage_id"),
record.getStr("subject_id")
);
}, executorService);
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> collectLesson(
record.getStr("node_id"),
record.getStr("stage_id"),
record.getStr("subject_id")
), executorService);
futures.add(future);
}

@ -1,5 +1,6 @@
package Tools.Crawler.Util;
import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpRequest;
public class Util {
@ -62,4 +63,38 @@ public class Util {
return null; // 这个返回语句实际上不会被执行到,但需要为了编译通过
}
/**
*
*
* @param url
* @param jsonBody
* @param isGet
* @param maxRetries
* @return
*/
public static String doRequestWithRetry(String url, String jsonBody, boolean isGet, int maxRetries) {
int retries = 0;
while (retries < maxRetries) {
try {
if (isGet) {
return Util.doGet(url);
} else {
return Util.doPost(url, jsonBody);
}
} catch (Exception e) {
retries++;
if (retries == maxRetries) {
System.out.println(DateTime.now() + " 请求失败: " + url + " 重试次数: " + retries);
throw new RuntimeException("Failed after " + maxRetries + " retries", e);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
}
}
return null;
}
}

Loading…
Cancel
Save