I'm new to Netty and the title might not make much sense but here's what I'm trying to do. We have an existing Netty server for normal REST APIs. We're trying to experiment with Protobuf and we thought we would just hack the existing server to add ProtobufEncoder/Decoder and we would be able to just run them both in parallel. It doesn't seem to work that way.
Here's my initChannel
@Override
protected void initChannel(SocketChannel ch) throws Exception
{
ChannelPipeline pipeline = ch.pipeline();
/**
* Protobuf
*/
pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, 0, 4));
//Decode the message sent by the client
pipeline.addLast(new ProtobufDecoder(HelloRequest.getDefaultInstance()));
// encoded
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new ProtobufEncoder());
// Register the handler
pipeline.addLast(new ServerHandler());
/**
* End Protobuf
*/
pipeline.addLast(CODEC, new HttpServerCodec());
pipeline.addLast(DECOMPRESSOR, new HttpContentDecompressor());
if (serverCompressHttpResponses_) {
pipeline.addLast(COMPRESSOR, new HttpContentCompressor());
}
pipeline.addLast(AGGREGATOR, new HttpObjectAggregator(maxContentLength_));
if (maxKeepAliveRequests_ >= 0 || maxKeepAliveTTLSeconds_ >= 0) {
pipeline.addLast(KA_HANDLER, new KeepAliveHandler(maxKeepAliveRequests_, maxKeepAliveTTLSeconds_));
}
pipeline.addLast(ROUTER, routerProvider_.get());
if (countActiveConnections_) {
pipeline.addLast(CONNECTION_COUNTER, new ConnectionCounter());
}
}
We have a HttpRouter which extends ChannelDuplexHandler to handle path like /query and /ping
The problem is if I have the section for Protobuf before all of the Http stuff, the server will hang on /ping but the protobuf works as expected. However, if I moved the section Protobuf to be after line pipeline.addLast(ROUTER, routerProvider_.get()); then /ping works as usual but the server would hang on RPC call from Client with Protobuf.
Here's the code for ServerHandler
public class ServerHandler extends SimpleChannelInboundHandler<Message>
{
@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception
{
// Print out the message directly
System.out.println(msg.getClass());
HelloReply response = null;
if (msg instanceof HelloRequest) {
HelloRequest clientMsg = (HelloRequest) msg;
System.out.println(ctx.channel().remoteAddress() + " Say : " + clientMsg.getName());
response = HelloReply.newBuilder().setMessage("test").build();
} else {
response = HelloReply.newBuilder().setMessage("client message is illegal").build();
System.out.println("client message is illegal");
}
// Return to the client message-I have received your message
ctx.writeAndFlush(response);
}
}
My question is if I will be able to have the same Netty server does REST API and Protobuf on the same port? Or it's just not possible?