Let object serialisation exceptions propagate in the Object Echo example (#10807)

Motivation:
People may use the object serialisation example as a vehicle to test out sending their own objects across the wire.
If those objects are not actually serialisable for some reason, then we need to let the exception propagate so that this becomes obvious to people.

Modification:
Add a listener to the future that sends the first serialisable message, so that we ensure that any exceptions that shows up during serialisation becomes visible.
Without this, the state of the future that sent the first message was never checked or inspected anywhere.

Result:
Serialisation bugs in code derived from the Object Echo example are much easier to diagnose.

This fixes #10777
This commit is contained in:
Chris Vest 2020-11-19 08:10:17 +01:00 committed by GitHub
parent 1c230405fd
commit 3354c7b0bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,12 +15,16 @@
*/ */
package io.netty.example.objectecho; package io.netty.example.objectecho;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static io.netty.channel.ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE;
/** /**
* Handler implementation for the object echo client. It initiates the * Handler implementation for the object echo client. It initiates the
* ping-pong traffic between the object echo client and server by sending the * ping-pong traffic between the object echo client and server by sending the
@ -43,7 +47,8 @@ public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelActive(ChannelHandlerContext ctx) { public void channelActive(ChannelHandlerContext ctx) {
// Send the first message if this handler is a client-side handler. // Send the first message if this handler is a client-side handler.
ctx.writeAndFlush(firstMessage); ChannelFuture future = ctx.writeAndFlush(firstMessage);
future.addListener(FIRE_EXCEPTION_ON_FAILURE); // Let object serialisation exceptions propagate.
} }
@Override @Override