>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring》、《Spring Cloud Alibaba微服务开发零基础入门到实操》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 19800 个阅读者 刷新本主题
 * 贴子主题:  MessagePack反序列化使用示例 回复文章 点赞(0)  收藏  
作者:mary    发表时间:2021-06-24 03:01:42     消息  查看  搜索  好友  邮件  复制  引用

    

1.MessagePack简单示例

1.1 引入jar包

  <dependency>

<groupId>org.msgpack</groupId>

<artifactId>msgpack</artifactId>

<version>0.6.12</version>

</dependency>

1.2 传输对象必须要引入注解@Message,且要有默认构造函数

@Message
public class MsgEntity {

    private String id;
    private String name;

    public MsgEntity() {

    }
……
}

1.3 demo演示

// 创建MessagePack
        MessagePack messagePack = new MessagePack();
        MsgEntity meite = new MsgEntity(UUID.randomUUID().toString(), "我是jarye");
        // 序列化
        byte[] bs = messagePack.write(meite);
        Value read1 = messagePack.read(bs);
        System.out.println(read1);
//        // 反序列化
        MsgEntity read = messagePack.read(bs, MsgEntity.class);
        System.out.println(read);

1.4 结果打印

  ["e1efc641-469d-4c6e-a765-e46edf0052e0","我是jarye"]

com.jgspx.serialize.messagepack.MsgEntity@62e136d3

2.MessagePack在netty中的使用

2.1 netty中编码器和解码器定义

public class MsgpackDecoder extends MessageToMessageDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        final int length = byteBuf.readableBytes();
        byte[] b = new byte[length];
        byteBuf.getBytes(byteBuf.readerIndex(), b, 0, length);
        MessagePack msgpack = new MessagePack();
        list.add(msgpack.read(b));
    }
}
public class MsgpackEncoder extends MessageToByteEncoder {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object msg, ByteBuf byteBuf) throws Exception {
        MessagePack msgpack = new MessagePack();
        byteBuf.writeBytes(msgpack.write(msg));
    }
}

2.2 注册到netty中

ch.pipeline().addLast(new MsgpackDecoder());
ch.pipeline().addLast(new MsgpackEncoder());

2.3 数据传输和接收可以直接使用对象形式了

public class ClientHandler extends SimpleChannelInboundHandler<Object> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        MsgEntity msgEntity = new MsgEntity(UUID.randomUUID().toString(), "我是jarye");
        ctx.writeAndFlush(msgEntity);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        System.out.println("resp:"+o);
    }
}
public class ServerHandler extends SimpleChannelInboundHandler<Object> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        System.out.println("o:" + o);
    }
}

2.4 结果打印

  o:["22c1a6ba-cbc1-4a88-874d-470fed3cde5d","我是jarye"]
     ----------------------------
原文链接:https://www.jianshu.com/p/937a3a457c75

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2021-08-30 20:52:28 重新编辑]
  Java面向对象编程-->流程控制
  JavaWeb开发-->自定义JSP标签(Ⅱ)
  JSP与Hibernate开发-->域对象在持久化层的四种状态
  Java网络编程-->对象的序列化与反序列化
  精通Spring-->
  Vue3开发-->Vue Router路由管理器
  git 仓库常用指令
  阿里巴巴为什么能抗住90秒100亿?看完这篇你就明白了!
  SpringMVC下的JUnit4单元测试
  Spring MVC服务器端推送的两种方式
  在Spring MVC中配置线程池,进行异步请求处理
  Spring MVC实现国际化的几种方式
  Spring MVC异常处理机制
  Spring Boot 基于 JUnit 5 实现单元测试
  Spring MVC 通过@Value注解读取.properties文件中的内容
  Nginx安装及配置
  使用 Flask-RESTful 设计 RESTful API
  spring cloud分布式微服务的概览
  Kafka笔记整理
  nginx详解反向代理、负载均衡、LNMP架构上线动态网站
  springboot —— 多数据源
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。