Обработка остановки сервера, фикс EventManager
This commit is contained in:
@@ -2,6 +2,8 @@ package io.orprotocol;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -194,6 +196,7 @@ public class Server extends WebSocketServer {
|
||||
* Настраиваем планировщик для проверки активности клиентов.
|
||||
*/
|
||||
this.inactivityShedulerTweaker();
|
||||
this.wakeupShutdownHook();
|
||||
|
||||
int port = this.getPort();
|
||||
System.out.println("\u001B[32mServer started at x.x.x.x:" + port + "\u001B[0m");
|
||||
@@ -203,6 +206,18 @@ public class Server extends WebSocketServer {
|
||||
this.listener.onServerStart(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить список подключенных клиентов
|
||||
* @return множество клиентов
|
||||
*/
|
||||
public Set<Client> getClients() {
|
||||
Set<Client> clients = new HashSet<>();
|
||||
for(WebSocket socket : this.getConnections()) {
|
||||
clients.add(socket.getAttachment());
|
||||
}
|
||||
return clients;
|
||||
}
|
||||
|
||||
/**
|
||||
* Планировщик для проверки активности клиентов.
|
||||
* Если планировщик обнаруживает неактивного клиента, он отключает его с соответствующим кодом ошибки.
|
||||
@@ -218,4 +233,19 @@ public class Server extends WebSocketServer {
|
||||
}, this.settings.heartbeatInterval, this.settings.heartbeatInterval, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Пробуждает хук завершения для корректной остановки сервера.
|
||||
*/
|
||||
private void wakeupShutdownHook() {
|
||||
if(this.listener == null){
|
||||
return;
|
||||
}
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
/**
|
||||
* Останавливаем сервер при завершении работы и вызываем слушатели остановки сервера.
|
||||
*/
|
||||
System.out.println("JVM Shutdown detected, stopping server...");
|
||||
this.listener.onServerStop(this);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user