>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 14313 个阅读者 刷新本主题
 * 贴子主题:  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开发-->Web运作原理(Ⅰ)
  JSP与Hibernate开发-->Java对象持久化技术概述
  Java网络编程-->基于MVC和RMI的分布式应用
  精通Spring-->Vue Router路由管理器
  Vue3开发-->绑定CSS样式
  Spring Boot 入门,用 Spring Boot 写第一个 HelloWorld 程序
  NIO的几道常见面试题
  git 常用指令总结
  微服务架构模型
  几种常见的MAVEN仓库地址
  SpringMVC下的JUnit4单元测试
  Spring MVC实现国际化的几种方式
  使用Spring MVC处理404错误的方法
  springMVC:HandlerInterceptor拦截器的使用
  @Resource注解的用法
  Spring+JPA+ehcache开启二级本地缓存
  酒店评论数据分析和挖掘-展现数据分析全流程:报告展示篇
  再谈响应式流(结合制奶厂业务的案例)
  springmvc+ajax异步上传图片
  RESTful 架构详解
  更多...
 IPIP: 已设置保密
树形列表:   
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


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