Netty中的MessageToMessageDecoder用法2022-01-11

在《Netty实战》实战一书中,介绍MessageToMessageDecoder的用法时,书中给的例子是这样的

image.png

我的实现

/**
 * @author frend
 * @version v0.0.1
 * @apiNote 将一种类型转换为另外一种类型
 * 
 * @time 2022/1/10 22:59
 * @parjectName netty-demo
 */
public class IntegerToStringDecoder extends MessageToMessageDecoder {

    protected void decode(ChannelHandlerContext ctx, Integer msg, List out)
            throws Exception {
        out.add(String.valueOf(msg));
    }
}
 
 

测试调用

    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer();
        for (int i = 0; i < 9 ; i ++) {
            buffer.writeInt(i);
        }
        ByteBuf in = buffer.duplicate();
        EmbeddedChannel channel = new EmbeddedChannel(new IntegerToStringDecoder());
        channel.writeInbound(in);
        channel.finish();

        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
    }

上述代码中,给buffer添加了0~8的数字,但是在main方法执行后,读取时的结果是这样的

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=57943:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;I:\dev\workspace\java\netty-demo\netty-test\target\classes;I:\dev\tools\apache-maven-3.5.3\repository\junit\junit\4.12\junit-4.12.jar;I:\dev\tools\apache-maven-3.5.3\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;I:\dev\tools\apache-maven-3.5.3\repository\io\netty\netty-all\4.1.53.Final\netty-all-4.1.53.Final.jar" org.rate.netty.decode.IntegerToStringDecoderTEst
null
null
null
null
null

经过一番测试后发现,MessageToMessageDecoder不能直接对ByteBuf的数据进行处理,因此在调用 MessageToMessageDecoder的实现之前,先需要将ByteBuf转化为一个Message
改造后的代码

public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer();
        for (int i = 0; i < 9 ; i ++) {
            buffer.writeInt(i);
        }
        ByteBuf in = buffer.duplicate();
        EmbeddedChannel channel = new EmbeddedChannel(new IntegerToStringDecoder());
        channel.pipeline()
                .addLast(new MyByteToMessageDecoder())
                .addLast(new IntegerToStringDecoder());
        channel.writeInbound(in);

        channel.finish();

        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
        System.out.println(channel.readInbound());
    }

在channel的pipeline添加一个Bytebuf的前置处理器
执行结果

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=59697:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;I:\dev\workspace\java\netty-demo\netty-test\target\classes;I:\dev\tools\apache-maven-3.5.3\repository\junit\junit\4.12\junit-4.12.jar;I:\dev\tools\apache-maven-3.5.3\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;I:\dev\tools\apache-maven-3.5.3\repository\io\netty\netty-all\4.1.53.Final\netty-all-4.1.53.Final.jar" org.rate.netty.decode.IntegerToStringDecoderTEst
0
1
2
3
4

你可能感兴趣的:(Netty中的MessageToMessageDecoder用法2022-01-11)