Skip to content

feat: adding stop() method #100

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

Closed
wants to merge 13 commits into from
Closed
Changes from 2 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
29 changes: 27 additions & 2 deletions core/src/main/java/com/javadiscord/jdi/core/Discord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Discord {
private static final Logger LOGGER = LogManager.getLogger();
Expand All @@ -45,6 +47,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 +137,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 +156,27 @@ public void start() {
EXECUTOR.execute(discordRequestDispatcher);
}

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

ThreadPoolExecutor threadPool = (ThreadPoolExecutor) EXECUTOR;
if (threadPool.getPoolSize() > 0) threadPool.shutdown();

try {
if (!threadPool.awaitTermination(30, TimeUnit.SECONDS)) {
threadPool.shutdownNow();
if (!threadPool.awaitTermination(30, TimeUnit.SECONDS)) {
LOGGER.error("Threads not terminated properly");
}
}
} catch (InterruptedException ie) {
threadPool.shutdownNow();
Thread.currentThread().interrupt();
}
}

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