Rys
Rys

Reputation: 5164

How can I start web containter and netty server in different ports with spring boot?

I want to make a web on port 9061 and in the same application start a socket server with netty on other port (1066 for example) in order to listen for some information I need from other systems. I tryed something but I binded the servlet port with the netty server port, something that I don't know.

Here is the code:

@Component
public class TCPServer {

    @Autowired
    @Qualifier("serverBootstrap")
    private ServerBootstrap serverBootstrap;

    @Autowired
    @Qualifier("tcpSocketAddress")
    private InetSocketAddress tcpPort;

    private Channel serverChannel;

    @PostConstruct
    public void start() throws Exception {
        serverChannel =  serverBootstrap.bind(tcpPort).sync().channel().closeFuture().sync().channel();
    }

    @PreDestroy
    public void stop() throws Exception {
        serverChannel.close();
        serverChannel.parent().close();
    }

    public ServerBootstrap getServerBootstrap() {
        return serverBootstrap;
    }

    public void setServerBootstrap(ServerBootstrap serverBootstrap) {
        this.serverBootstrap = serverBootstrap;
    }

    public InetSocketAddress getTcpPort() {
        return tcpPort;
    }

    public void setTcpPort(InetSocketAddress tcpPort) {
        this.tcpPort = tcpPort;
    }
}

And the configuration:

spring:
  application:
    name: MyServer
  main:
    banner-mode: off    

server:
  port: 9061

nettytcp:
  puerto: 1066

I never see the 9061 port.

Upvotes: 0

Views: 1903

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

Its because you call .closeFuture().sync() which will block until the ServerChannel is closed, which will never here. I think you only want to call serverBootstrap.bind(tcpPort).sync().channel().

Upvotes: 2

Related Questions