Mike Grundvig wrote:
> ...
>
> public void sessionCreated(IoSession ioSession) throws Exception {
> ProtocolEncoder delimitedEncoder = new
> TextLineEncoder(defaultCharset, terminator);
> ProtocolDecoder delimitedDecoder = new
> TextLineDecoder(defaultCharset, terminator);
> ProtocolCodecFilter codecFilter = new
> ProtocolCodecFilter(delimitedEncoder, delimitedDecoder);
> ioSession.getFilterChain().addLast("protocol", codecFilter);
> }
>
> This all works fine, but it seems horribly inefficient to create a
> filter, encoder and decoder for each and every connection established.
> Particularly when the ioSession provides the concept of attributes
> that can be used to store stateful data. Is my implementation actually
> correct? It seems a better approach (from a performance standpoint)
> would be to create stateless versions of those objects and tie them to
> the IOAcceptor itself unless I'm missing something (which seems likely
> at this point).
>
> I've seen examples that add filters to either the ioSession OR the
> ioAcceptor. What's the difference? Adding to the acceptor adds them to
> all new sessions, is that correct?
Yes, you're correct. Adding filters to the IoAcceptor means that they
will be added to all session created from that IoAcceptor. I'd say that
I would either add the ProtocolCodecFilter to the IoAcceptor or use a
single ProtocolCodecFilter instance in my IoHandler like this:
public MyIoHandler() {
ProtocolEncoder delimitedEncoder = new
TextLineEncoder(defaultCharset, terminator);
ProtocolDecoder delimitedDecoder = new
TextLineDecoder(defaultCharset, terminator);
this.codecFilter = new ProtocolCodecFilter(delimitedEncoder,
delimitedDecoder);
}
public void sessionCreated(IoSession ioSession) throws Exception {
ioSession.getFilterChain().addLast("protocol", codecFilter);
}
Of course, for this to work TextLineEncoder, TextLineDecoder and
ProtocolCodecFilter must be stateless. Luckily they are.
HTH
--
Niklas Therning
www.spamdrain.net