netty-socketio icon indicating copy to clipboard operation
netty-socketio copied to clipboard

Best way to stop server

Open JaviOverflow opened this issue 4 years ago • 7 comments

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()
    }
}

JaviOverflow avatar Aug 19 '20 19:08 JaviOverflow

@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.

merzMario avatar Nov 25 '20 08:11 merzMario

@PreDestroy private void autoStop() { Runtime.getRuntime().addShutdownHook(new Thread(() -> { stop(); })); }

wgzhxy avatar Jan 10 '21 14:01 wgzhxy

None of the proposed answers seem to work. Does stop() actually blocks execution and frees the port?

JaviOverflow avatar Jan 16 '21 16:01 JaviOverflow

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.

wgzhxy avatar Jan 20 '21 16:01 wgzhxy

You can try to use, netty-socketio, 1.7.18, older version maybe has bug

wgzhxy avatar Jan 20 '21 16:01 wgzhxy

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

warriorsfly avatar Jul 13 '21 06:07 warriorsfly

Hi there :) Any updates on it? We're using version 2.0.2 and still have this issue...

desoliture avatar Sep 13 '23 08:09 desoliture