Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty 4.0 on multiple ports with multiple protocols?

Tags:

netty

I'm looking for a server example that would combine a http handler on port 80 and a protobuf handler on another port in the same jar. Thanks!

like image 486
jestro Avatar asked Sep 03 '12 07:09

jestro


2 Answers

For my perspective, create different ServerBootstraps are not fully right way, because it will lead to create unused entities, handlers, double initialization, possible inconsistence between them, EventLoopGroups sharing or cloning, etc.

Good alternative is just to create multiple channels for all required ports in one Bootstrap server. If take "Writing a Discard Server" example from Netty 4.x "Getting Started", we should replace

    // Bind and start to accept incoming connections.
    ChannelFuture f = b.bind(port).sync(); // (7)

    // Wait until the server socket is closed.
    // In this example, this does not happen, but you can do that to gracefully
    // shut down your server.
    f.channel().closeFuture().sync()

With

    List<Integer> ports = Arrays.asList(8080, 8081);
    Collection<Channel> channels = new ArrayList<>(ports.size());
    for (int port : ports) {
        Channel serverChannel = bootstrap.bind(port).sync().channel();
        channels.add(serverChannel);
    }
    for (Channel ch : channels) {
        ch.closeFuture().sync();
    }
like image 187
Dmitry Spikhalskiy Avatar answered Oct 24 '22 03:10

Dmitry Spikhalskiy


I don't know what exactly you are looking for. Its just about creating two different ServerBootstrap instances, configure them and call bind(..) thats it.

like image 39
Norman Maurer Avatar answered Oct 24 '22 05:10

Norman Maurer