2

I guess netty is best java networking framework ever i know, after reading and try some sample i have question:

1. What the best way to create Network Server for multi port with different protocol using netty 4.0?

Each server create :

EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)

EventLoopGroup workerGroup = new NioEventLoopGroup();

ServerBootstrap b = new ServerBootstrap(); // (2)

Each Server Running Inside Thread

is that right way?

2. Websocket Server

How to securing Websocket Server for Cross origin case? I don't have any reference about it

Your help very appreciate,

Regards

BC,

binaryCode
  • 25
  • 9

2 Answers2

3

As Norman said, the important thing is that you need to share the event loop groups so that you do not create way too many threads. As long as you share the event loop groups, you can create as many ServerBootstraps as you wish:

EventLoopGroup bossGroup = new NioEventLoopGroup(numBossThreads);
EventLoopGroup workerGroup = new NioEventLoopGroup(numWorkerThreads);

ServerBootstrap sb1 = new ServerBootstrap();
sb1.group(bossGroup, workerGroup);
...
sb1.bind();

ServerBootstrap sb2 = new ServerBootstrap();
sb2.group(bossGroup, workerGroup);
...
sb2.bind();

ServerBootstrap sb3 = new ServerBootstrap();
sb3.group(bossGroup, workerGroup);
...
sb3.bind();

The bossGroup is used to accept the incoming connections, and the workerGroup is used to handle the connections accepted by the bossGroup. Please do some performance tests and specify the optimal numBossThreads and numWorkerThreads.

trustin
  • 12,231
  • 6
  • 42
  • 52
  • Thank you for your answer Norman & Trustin, i will try your suggestion – binaryCode Jul 05 '14 at 13:17
  • I don't understand how you can create multiple ServerBootstraps. According to sample code I see, after bind() you must do this: sb.bind(PORT).sync().channel().closeFuture().sync(); Each of the sync operations blocks. So if you do this to one ServerBootstrap you will never run the others. What am I missing? – Marc Nov 24 '14 at 18:30
  • i have the same questions . Could you provide the example ? – Mikhail Jan 17 '15 at 08:14
  • You don't need to sync(). sync() in the official examples is to prevent the server from shutting itself down and the server shuts itself down gracefully when the server socket is closed. – trustin Jan 19 '15 at 06:32
0

I would share the NioEventLoopGroup between the ServerBootstrap to share the same threads.

Norman Maurer
  • 23,104
  • 2
  • 33
  • 31