Skip to content

Commit

Permalink
使用线程池发送Webhook消息
Browse files Browse the repository at this point in the history
Fixes #210
  • Loading branch information
Xujiayao committed Jan 17, 2024
1 parent 9fc8423 commit 1107a38
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
//$$ import java.util.UUID;
//#endif

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
import static com.xujiayao.mcdiscordchat.Main.HTTP_CLIENT;
Expand Down Expand Up @@ -78,12 +81,16 @@ private void sendMessage(String content, String username) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}

if (CONFIG.multiServer.enable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
Expand Down Expand Up @@ -293,12 +295,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
Expand Down Expand Up @@ -286,12 +288,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
//$$ import java.util.Objects;
//#endif

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
import static com.xujiayao.mcdiscordchat.Main.HTTP_CLIENT;
Expand Down Expand Up @@ -93,12 +96,16 @@ private void sendMessage(String content, String username) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}

if (CONFIG.multiServer.enable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
Expand Down Expand Up @@ -297,12 +299,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
Expand Down Expand Up @@ -295,12 +297,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import com.xujiayao.mcdiscordchat.utils.MarkdownParser;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
Expand Down Expand Up @@ -243,12 +245,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.xujiayao.mcdiscordchat.utils.MarkdownParser;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
import static com.xujiayao.mcdiscordchat.Main.CONFIG;
Expand Down Expand Up @@ -257,12 +259,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

import java.time.Instant;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import static com.xujiayao.mcdiscordchat.Main.CHANNEL;
Expand Down Expand Up @@ -259,12 +261,16 @@ private void sendMessage(String message, boolean escapeMarkdown) {
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
.build();

try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
Response response = HTTP_CLIENT.newCall(request).execute();
response.close();
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
executor.shutdown();
}
}

Expand Down

0 comments on commit 1107a38

Please sign in to comment.