Mr.Wang from Next Door
Mr.Wang from Next Door

Reputation: 14790

Netty 4 : share handler in different pipeline

The ChannelInitializer.initChannel method is called for every new connection,

pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new ServerWebSocketServerProtocolHandler("/api", "*", false));

Can I reuse the same instance for all connections? How to tell if specific handler is reusable cross multiple connections?

Upvotes: 1

Views: 362

Answers (1)

Herr Derb
Herr Derb

Reputation: 5357

A handler that you can reuse, is annotated with @Sharable, which means that is guaranteed stateless.

Just go ahead and check the documentation for the different handlers.

For example HttpServerCodec and HttpObjectAggregatorare sharable.

The last handler is probably a custom handler that you've implemented, at least I'm not familiar with it. There it depends on your implementation.

And otherwise, you can just reuse the the first two handlers by making a static instance of them and recreate only the last handler for every connection.

Upvotes: 1

Related Questions