3
votes

I have a new project and I'll be using Netty for the first time (v4.0.4). I'll have a server with tens of thousands of connected clients. The server will send commands to these clients and should receive responses.

Looking through the API and the online examples I am not sure how to model my clients from the server's perspective. I have organized my channels into various ChannelGroups, and I can send commands just fine.

I'll need to know if a command times out or if an error code is returned. I may need to maintain a command queue for each client as well. Should I use a ScheduledExecutorService to set a timeout handler for each command then just cancel the Future when/if the response comes in?

Would it be a good idea to subclass Channel to encapsulate this logic? Or should I use some type of session storage and put the logic in my inbound channel handler?

I'm sure these methods will work but they seem a bit awkward and I want to make sure I'm doing things the netty way.

Thanks!

1

1 Answers

2
votes

To schedule an event (such as a timeout) you can access the ChannelHandlerContext's EventExecutor to schedule any tasks using the same thread pool netty uses. Alternatively you can use a ReadTimeoutHandler to manage the timeout. The ReadTimeoutHandler would need to be added to the channel's pipeline when a command is sent, and removed when the response is received.

Per-connection data, or session data is easily handled by netty as member variables of a ChannelHandler object. In cases where a shared ChannelHander is used, there is also a way of storing these data in the ChannelHanderContext using attachments. Both methods are documented in the ChannelHandler interface.

These are the answers I came up with to my own question, comments or better answers are welcomed.