giro
giro

Reputation: 431

How can I make my netty handler continue reading data after catching an exception?

In my netty handler I override the exceptionCaught method to catch a TooLongFrameException which is thrown by the netty ObjectDecoder. In case of that exception I send a message to the server with the information about that error. The server receives that message, and then attempts to continue to send more data. The problem is, that it seems that my handler does not continue to read that data.

I use netty 4.1.18.Final

import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.TooLongFrameException;

public final class MyHandler extends SimpleChannelInboundHandler<MyObject> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MyObject obj) throws Exception {

    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (cause instanceof TooLongFrameException) {
            ctx.writeAndFlush( messageAboutError );
            return;
        } else
            ctx.writeAndFlush( messageAboutUnexpectedError );
        super.exceptionCaught(ctx, cause);
    }
}

How can I make my Handler continue to read data after I caught the exception thrown by the netty ObjectDecoder?

Upvotes: 0

Views: 452

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23557

The problem really is that once you are in this state the ObjectDecoder will have a messed up "decoding state" and so there is not much you can do beside dropping the connection. If you would use another message format what you could do is just discard the rest of the message and only start parsing again once a new message was received. How this can be done etc really depends on the protocol itself.

Upvotes: 1

Related Questions