Skip to content

Commit 648de54

Browse files
authored
feat: adding stop() method refering to PR#100 (#115)
feat: adding stop() method to interrupt and close all pools and connection sockets
1 parent 587c681 commit 648de54

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

api/src/main/java/com/javadiscord/jdi/internal/api/DiscordRequestDispatcher.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.concurrent.BlockingQueue;
1212
import java.util.concurrent.LinkedBlockingQueue;
13+
import java.util.concurrent.atomic.AtomicBoolean;
1314
import java.util.stream.Stream;
1415

1516
public class DiscordRequestDispatcher implements Runnable {
@@ -22,6 +23,7 @@ public class DiscordRequestDispatcher implements Runnable {
2223
private final HttpClient httpClient;
2324
private final BlockingQueue<DiscordRequestBuilder> queue;
2425
private final String botToken;
26+
private AtomicBoolean running = new AtomicBoolean(false);
2527
private int numberOfRequestsSent;
2628
private long timeSinceLastRequest;
2729

@@ -41,7 +43,9 @@ public DiscordResponseFuture queue(DiscordRequest discordRequest) {
4143

4244
@Override
4345
public void run() {
44-
while (true) {
46+
running.set(true);
47+
48+
while (running.get()) {
4549
long currentTime = System.currentTimeMillis();
4650
long elapsed = currentTime - timeSinceLastRequest;
4751

@@ -62,6 +66,10 @@ public void run() {
6266
}
6367
}
6468

69+
public void stop() {
70+
running.set(false);
71+
}
72+
6573
private void sendRequest(DiscordRequestBuilder discordRequestBuilder) {
6674
try {
6775
HttpRequest.Builder requestBuilder =

core/src/main/java/com/javadiscord/jdi/core/Discord.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import java.net.http.HttpResponse;
2323
import java.util.ArrayList;
2424
import java.util.List;
25-
import java.util.concurrent.Executor;
25+
import java.util.concurrent.ExecutorService;
2626
import java.util.concurrent.Executors;
27+
import java.util.concurrent.TimeUnit;
2728

2829
public class Discord {
2930
private static final Logger LOGGER = LogManager.getLogger();
30-
private static final Executor EXECUTOR = Executors.newCachedThreadPool();
31+
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
3132
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
3233
private static final String WEBSITE = "https://javadiscord.com/";
3334

@@ -45,6 +46,7 @@ public class Discord {
4546
private final List<Object> annotatedEventListeners = new ArrayList<>();
4647
private final List<EventListener> eventListeners = new ArrayList<>();
4748

49+
private WebSocketManager webSocketManager;
4850
private Object listenerLoader;
4951

5052
public Discord(String botToken) {
@@ -134,13 +136,14 @@ private void loadAnnotations() {
134136
}
135137

136138
public void start() {
137-
WebSocketManager webSocketManager =
139+
this.webSocketManager =
138140
new WebSocketManager(
139141
new GatewaySetting().setApiVersion(10).setEncoding(GatewayEncoding.JSON),
140142
identifyRequest,
141143
cache);
142144

143-
WebSocketManagerProxy webSocketManagerProxy = new WebSocketManagerProxy(webSocketManager);
145+
WebSocketManagerProxy webSocketManagerProxy =
146+
new WebSocketManagerProxy(this.webSocketManager);
144147
ConnectionDetails connectionDetails =
145148
new ConnectionDetails(gateway.url(), botToken, gatewaySetting);
146149
ConnectionMediator connectionMediator =
@@ -152,6 +155,29 @@ public void start() {
152155
EXECUTOR.execute(discordRequestDispatcher);
153156
}
154157

158+
public void stop() {
159+
if (this.webSocketManager != null) {
160+
this.webSocketManager.stop();
161+
}
162+
163+
discordRequestDispatcher.stop();
164+
165+
EXECUTOR.shutdown();
166+
try {
167+
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
168+
EXECUTOR.shutdownNow();
169+
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
170+
LOGGER.warn(
171+
"Executor failed to shutdown within the specified time limit, some"
172+
+ " tasks may still be running");
173+
}
174+
}
175+
} catch (InterruptedException ie) {
176+
LOGGER.error("Termination was interrupted within {} seconds.", 30);
177+
Thread.currentThread().interrupt();
178+
}
179+
}
180+
155181
public void startWithoutGatewayEvents() {
156182
EXECUTOR.execute(discordRequestDispatcher);
157183
}

0 commit comments

Comments
 (0)