Skip to content

feat: adding stop() method refering to PR#100 #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

public class DiscordRequestDispatcher implements Runnable {
Expand All @@ -22,6 +23,7 @@ public class DiscordRequestDispatcher implements Runnable {
private final HttpClient httpClient;
private final BlockingQueue<DiscordRequestBuilder> queue;
private final String botToken;
private AtomicBoolean running = new AtomicBoolean(false);
private int numberOfRequestsSent;
private long timeSinceLastRequest;

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

@Override
public void run() {
while (true) {
running.set(true);

while (running.get()) {
long currentTime = System.currentTimeMillis();
long elapsed = currentTime - timeSinceLastRequest;

Expand All @@ -62,6 +66,10 @@ public void run() {
}
}

public void stop() {
running.set(false);
}

private void sendRequest(DiscordRequestBuilder discordRequestBuilder) {
try {
HttpRequest.Builder requestBuilder =
Expand Down
34 changes: 30 additions & 4 deletions core/src/main/java/com/javadiscord/jdi/core/Discord.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Discord {
private static final Logger LOGGER = LogManager.getLogger();
private static final Executor EXECUTOR = Executors.newCachedThreadPool();
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String WEBSITE = "https://javadiscord.com/";

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

private WebSocketManager webSocketManager;
private Object listenerLoader;

public Discord(String botToken) {
Expand Down Expand Up @@ -134,13 +136,14 @@ private void loadAnnotations() {
}

public void start() {
WebSocketManager webSocketManager =
this.webSocketManager =
new WebSocketManager(
new GatewaySetting().setApiVersion(10).setEncoding(GatewayEncoding.JSON),
identifyRequest,
cache);

WebSocketManagerProxy webSocketManagerProxy = new WebSocketManagerProxy(webSocketManager);
WebSocketManagerProxy webSocketManagerProxy =
new WebSocketManagerProxy(this.webSocketManager);
ConnectionDetails connectionDetails =
new ConnectionDetails(gateway.url(), botToken, gatewaySetting);
ConnectionMediator connectionMediator =
Expand All @@ -152,6 +155,29 @@ public void start() {
EXECUTOR.execute(discordRequestDispatcher);
}

public void stop() {
if (this.webSocketManager != null) {
this.webSocketManager.stop();
}

discordRequestDispatcher.stop();

EXECUTOR.shutdown();
try {
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
EXECUTOR.shutdownNow();
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
LOGGER.warn(
"Executor failed to shutdown within the specified time limit, some"
+ " tasks may still be running");
}
}
} catch (InterruptedException ie) {
LOGGER.error("Termination was interrupted within {} seconds.", 30);
Thread.currentThread().interrupt();
}
}

public void startWithoutGatewayEvents() {
EXECUTOR.execute(discordRequestDispatcher);
}
Expand Down
Loading