netty-socketio
netty-socketio copied to clipboard
Best way to stop server
I'm using Springboot and as shutdown hook I have the following:
@PreDestroy
fun stopIO() {
server.stop()
}
but when I restart the server I get that the address is in use for about 30s which makes the developing experience very frustrating.
How can I properly shutdown the server so everything closes before the app exits?
Full implementation:
@Service
class SocketIO constructor(
@Value("\${server.port}") val port: Int
) {
lateinit var server: SocketIOServer
@PostConstruct
fun serverBean() {
val configuration = com.corundumstudio.socketio.Configuration()
configuration.port = port + 1
configuration.hostname = "localhost"
configuration.origin = "http://localhost:8080"
server = SocketIOServer(configuration)
server.start()
}
@PreDestroy
fun stopIO() {
server.stop()
}
}
@Component
@Slf4j
public class ServerRunner implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {
@Bean
public SocketIOServer socketIOServer() {
Configuration config = new Configuration();
config.setPort(port);
server = new SocketIOServer(config);
return server;
}
@Override
public void run(String... args) {
server.start();
}
@Override
public void onApplicationEvent(ContextClosedEvent event) {
server.stop();
}
}
You can implement ApplicationListener<ContextClosedEvent> interface and when the container stoped , a ContextClosedEvent event will be published.
@PreDestroy private void autoStop() { Runtime.getRuntime().addShutdownHook(new Thread(() -> { stop(); })); }
None of the proposed answers seem to work. Does stop() actually blocks execution and frees the port?
None of the proposed answers seem to work. Does stop() actually blocks execution and frees the port? Which netty-socketio' version you use? You can upgrade netty-socketin, e.g.
<groupId>com.corundumstudio.socketio</groupId> <artifactId>netty-socketio</artifactId> 1.7.18
No change your code, No problem, Older version may be have bug.
You can try to use, netty-socketio, 1.7.18, older version maybe has bug
You can try to use, netty-socketio, 1.7.18, older version maybe has bug
Using version 1.7.19, the log prints SocketIO server stopped
,but the port have not been freed
Hi there :) Any updates on it? We're using version 2.0.2 and still have this issue...