一、定义类,继承MessageToMessageCodec,重写encode和decode方法
package cn.edu.tju;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import java.util.List;
public class MyMessageToMessageCodec extends MessageToMessageCodec<ByteBuf, String> {
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, String s, List<Object> list) throws Exception {
System.out.println("encode was called......");
ByteBuf buf = Unpooled.copiedBuffer(s.getBytes());
list.add(buf);
}
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
System.out.println("decode was called......");
int length = byteBuf.readableBytes();
byte[] bytes = new byte[length];
byteBuf.readBytes(bytes);
String str = new String (bytes);
list.add(str);
System.out.println("codec received message: " + str);
}
}
二、定义一个简单的ChannelInboundHandlerAdapter
package cn.edu.tju;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class MySimpleHandler2 extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String str = (String)msg;
System.out.println("handler received message: " + msg);
ctx.channel().writeAndFlush(str.toUpperCase());
}
}
三、定义netty TCP server 主类,在ChannelPipeline中加入上述两个handler
package cn.edu.tju;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import java.net.InetSocketAddress;
public class NettyTcpServer11 {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(16);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new MyMessageToMessageCodec());
pipeline.addLast(new MySimpleHandler2());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(8899))
.sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception ex){
System.out.println(ex.getMessage());
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
MessageToMessageCodec的子类可以用@Sharable注解标记。
ByteToMessageCodec的子类不能用@Sharable注解标记,因为在调用构造方法时会判断是否有@Sharable注解,如果有,会报错。