Search code examples
javanettyspring-shell

How to control netty server from spring-shell


I try to create a console with "spring shell", I would like to run my own netty socket server command. I have a command:

@CliCommand(value = "server", help = "Start socket server")
public String server(
        @CliOption(key = {"port", "p"}, mandatory = true, help = "Port number [49152-65535]") final String port,
        @CliOption(key = {"maxclients", "mc"}, mandatory = false, help = "Max clients", unspecifiedDefaultValue = "50") final String maxClients
)
{
    NettyServer nettyServer = new NettyServer(Integer.parseInt(port), Integer.parseInt(maxClients));
    nettyServer.run();

    return "::CliCommand return::";
}

While the server is running, it blocks the console and can not enter other commands. Should not you have a solution? For example, how to let it run alone and only influence the server by the command. For example, I would like to send messages from my console to clients.


Solution

  • I can't answer for the whole architecture of your application, but your server object should not be created inside your command method. It should be created prior to that (or at the very least, it should not be a local parameter of your command method that dies when your command ends) and acted upon by commands, using a different thread I assume.