Skip to content

Commit

Permalink
使用多线程技术并发执行签到
Browse files Browse the repository at this point in the history
  • Loading branch information
anduinnn committed Apr 2, 2024
1 parent 4f7488d commit e1428fe
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions src/main/java/cloud/ohiyou/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
private static final String COOKIE = System.getenv("COOKIE");
//private static final String COOKIE = "bbs_sid=7bl67b8516gvcn3qejbcsnvotf; bbs_token=1sslA6_2B2TGqkFbnc8t0HPuvahctVQURp_2B28V13Nx2eSp0oEGkMjNS5x9ZTxHrPzbvXgFzvVdcHrd_2BNU2Ar_2F_2FL63RjCym7mrj &\n";
// private static final String COOKIE = System.getenv("COOKIE");
private static final String COOKIE = "bbs_sid=7bl67b8516gvcn3qejbcsnvotf; bbs_token=1sslA6_2B2TGqkFbnc8t0HPuvahctVQURp_2B28V13Nx2eSp0oEGkMjNS5x9ZTxHrPzbvXgFzvVdcHrd_2BNU2Ar_2F_2FL63RjCym7mrj&bbs_sid=7bl67b8516gvcn3qejbcsnvotf; bbs_token=1sslA6_2B2TGqkFbnc8t0HPuvahctVQURp_2B28V13Nx2eSp0oEGkMjNS5x9ZTxHrPzbvXgFzvVdcHrd_2BNU2Ar_2F_2FL63RjCym7mrj&bbs_sid=7bl67b8516gvcn3qejbcsnvotf; bbs_token=1sslA6_2B2TGqkFbnc8t0HPuvahctVQURp_2B28V13Nx2eSp0oEGkMjNS5x9ZTxHrPzbvXgFzvVdcHrd_2BNU2Ar_2F_2FL63RjCym7mrj";
private static final String DINGTALK_WEBHOOK = System.getenv("DINGTALK_WEBHOOK"); // 钉钉机器人 access_token 的值
private static final String WXWORK_WEBHOOK = System.getenv("WXWORK_WEBHOOK"); // 企业微信机器人 key 的值
private static final String SERVER_CHAN_KEY = System.getenv("SERVER_CHAN");
Expand All @@ -46,27 +48,48 @@ public static void main(String[] args) {
for (int i = 0; i < cookiesArray.length; i++) {
String cookie = cookiesArray[i];
String cookieName = "Cookie" + (i + 1); // 命名cookie,如Cookie1, Cookie2...
}

try {
long startTime = System.currentTimeMillis();
String formattedCookie = formatCookie(cookie.trim(),i);
if (formattedCookie != null) {
SignResultVO signResultVO = sendSignInRequest(formattedCookie);
long endTime = System.currentTimeMillis();
results.add(new CookieSignResult(signResultVO, endTime - startTime));
ExecutorService executor = Executors.newFixedThreadPool(5); // N is the number of threads
for (int i = 0; i < cookiesArray.length; i++) {
final String cookie = cookiesArray[i];
final int index = i;
executor.submit(() -> {
try {
processCookie(cookie, index, results);
} catch (Exception e) {
log("Error processing cookie at index " + index + ": " + e.getMessage());
}
} catch (Exception e) {
log("处理 " + cookieName + " 时发生错误: " + e.getMessage());
} finally {
// 关闭 OkHttpClient
client.dispatcher().executorService().shutdownNow();
client.connectionPool().evictAll();
});
}

executor.shutdown();
try {
if (!executor.awaitTermination(20, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException ie) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}


client.dispatcher().executorService().shutdownNow();
client.connectionPool().evictAll();

publishResults(results);
}

private static void processCookie(String cookie, int index, List<CookieSignResult> results) {
long startTime = System.currentTimeMillis();
String formattedCookie = formatCookie(cookie.trim(), index);
if (formattedCookie != null) {
SignResultVO signResultVO = sendSignInRequest(formattedCookie);
long endTime = System.currentTimeMillis();
results.add(new CookieSignResult(signResultVO, endTime - startTime));
}
}

private static void publishResults(List<CookieSignResult> results) {
StringBuilder messageBuilder = new StringBuilder();
boolean allSuccess = true; // 假设所有签到都成功,直到发现失败的签到
Expand Down

0 comments on commit e1428fe

Please sign in to comment.