netty 的 JBoss Marshalling 编码解码
一. JBoss Marshalling 简介.
JBoss Marshalling 是一个Java 对象序列化包,对 JDK 默认的序列化框架进行了优化,但又保持跟 Java.io.Serializable 接口的兼容,同时增加了一些可调的参数和附件的特性, 这些参数和附加的特性, 这些参数和特性可通过工厂类进行配置.
二. JBoss Marshalling 的使用.
1. 下载 org.jboss.marshalling
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>2.0.0.Beta2</version>
</dependency>
2. 定义 POJO 对象,进行编解码.
SubScriptReq
package object.server.impl; import java.io.Serializable; public class SubScriptReq implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4686274228090335845L;
private Integer subReq;
private String userName;
private String productName;
private String address; public Integer getSubReq() {
return subReq;
} public void setSubReq(Integer subReq) {
this.subReq = subReq;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "SubScriptReq [subReq=" + subReq + ", userName=" + userName
+ ", productName=" + productName + ", address=" + address + "]";
} }
SubscriptResp
package object.server.impl; import java.io.Serializable; public class SubscriptResp implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4923081103118853877L;
private Integer subScriptID;
private String respCode;
private String desc; public Integer getSubScriptID() {
return subScriptID;
} public void setSubScriptID(Integer subScriptID) {
this.subScriptID = subScriptID;
} public String getRespCode() {
return respCode;
} public void setRespCode(String respCode) {
this.respCode = respCode;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} @Override
public String toString() {
return "SubscriptResp [subScriptID=" + subScriptID + ", respCode="
+ respCode + ", desc=" + desc + "]";
} }
3. Marshalling 构造工具
package object.server.impl; import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallingDecoder;
import io.netty.handler.codec.marshalling.MarshallingEncoder;
import io.netty.handler.codec.marshalling.UnmarshallerProvider; import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration; public class MarshallingCodeCFactory {
public static MarshallingDecoder buildMarshallingDecoder() {
/*
* 通过 Marshalling 工具类的 getProvidedMarshallerFactory
* 静态方法获取MarshallerFactory 实例, , 参数 serial 表示创建的是 Java 序列化工厂对象.它是由
* jboss-marshalling-serial 包提供
*/
final MarshallerFactory marshallerFactory = Marshalling
.getProvidedMarshallerFactory("serial");
/*
* 创建
*/
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5); UnmarshallerProvider provider = new DefaultUnmarshallerProvider(
marshallerFactory, configuration);
/*
* provider : 提供商 maxSize : 单个对象最大尺寸
*/
int maxSize = 1024 << 2;
MarshallingDecoder decoder = new MarshallingDecoder(provider, maxSize);
return decoder;
} public static MarshallingEncoder buildMarshallingEncoder() {
final MarshallerFactory marshallerFactory = Marshalling
.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
MarshallerProvider provider = new DefaultMarshallerProvider(
marshallerFactory, configuration);
MarshallingEncoder decoder = new MarshallingEncoder(provider);
return decoder;
} }
4. Netty 服务端代码:
package object.server.impl; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.marshalling.MarshallingDecoder; public class SubReqServer {
public void start(int port) {
NioEventLoopGroup workGroup = new NioEventLoopGroup();
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup);
bootstrap.channel(NioServerSocketChannel.class);
// 配置 NioServerSocketChannel 的 tcp 参数, BACKLOG 的大小
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
MarshallingCodeCFactory.buildMarshallingDecoder());
ch.pipeline().addLast(
MarshallingCodeCFactory.buildMarshallingEncoder());
ch.pipeline().addLast(new SubReqHandler());
}
});
// 绑定端口,随后调用它的同步阻塞方法 sync 等等绑定操作成功,完成之后 Netty 会返回一个 ChannelFuture
// 它的功能类似于的 Future,主要用于异步操作的通知回调.
ChannelFuture channelFuture;
try {
channelFuture = bootstrap.bind(port).sync();
// 等待服务端监听端口关闭,调用 sync 方法进行阻塞,等待服务端链路关闭之后 main 函数才退出.
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
} public static void main(String[] args) {
SubReqServer server = new SubReqServer();
server.start(9091);
}
}
serverHandler
package object.server.impl; import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext; public class SubReqHandler extends ChannelHandlerAdapter { @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
super.exceptionCaught(ctx, cause);
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception { System.out.println(msg);
SubscriptResp sub = new SubscriptResp();
sub.setDesc("desc");
sub.setSubScriptID(999);
sub.setRespCode("0");
ctx.writeAndFlush(sub);
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} }
5. client
package object.client.impl; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import object.server.impl.MarshallingCodeCFactory; public class SubReqClient {
public void connect(String host, int port) {
NioEventLoopGroup workGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
MarshallingCodeCFactory.buildMarshallingDecoder());
ch.pipeline().addLast(
MarshallingCodeCFactory.buildMarshallingEncoder());
ch.pipeline().addLast(new SubReqClientHandler());
}
}); // 发起异步链接操作
ChannelFuture future;
try {
future = bootstrap.connect(host, port).sync();
// 等待客户端链路关闭
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workGroup.shutdownGracefully();
}
} public static void main(String[] args) {
new SubReqClient().connect("localhost", 9091);
}
}
clientHandler
package object.client.impl; import object.server.impl.SubScriptReq;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext; public class SubReqClientHandler extends ChannelHandlerAdapter { @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
} @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
SubScriptReq req = new SubScriptReq();
for (int i = 0; i < 100; i++) { req.setSubReq(999);
req.setProductName("productName");
req.setUserName("userName");
req.setAddress("address");
ctx.writeAndFlush(req);
}
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println(msg);
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} }
注 : MarshallingDecoder 是自带半包处理的.
netty 的 JBoss Marshalling 编码解码的更多相关文章
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V3 -- 编码解码
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- pipeline.addLast(io.netty.handler.codec.MessageToMessageCodec ...
- Netty 粘包 & 拆包 & 编码 & 解码 & 序列化 介绍
目录: 粘包 & 拆包及解决方案 ByteToMessageDecoder 基于长度编解码器 基于分割符的编解码器 google 的 Protobuf 序列化介绍 其他的 前言 Netty 作 ...
- 一个低级错误引发Netty编码解码中文异常
前言 最近在调研Netty的使用,在编写编码解码模块的时候遇到了一个中文字符串编码和解码异常的情况,后来发现是笔者犯了个低级错误.这里做一个小小的回顾. 错误重现 在设计Netty的自定义协议的时候, ...
- netty: 编解码之jboss marshalling, 用marshalling进行对象传输
jboss marshalling是jboss内部的一个序列化框架,速度也十分快,这里netty也提供了支持,使用十分方便. TCP在网络通讯的时候,通常在解决TCP粘包.拆包问题的时候,一般会用以下 ...
- netty权威指南学习笔记八——编解码技术之JBoss Marshalling
JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...
- 架构师养成记--21.netty编码解码
背景 作为网络传输框架,免不了哟啊传输对象,对象在传输之前就要序列化,这个序列化的过程就是编码过程.接收到编码后的数据就需要解码,还原传输的数据. 代码 工厂类 import io.netty.han ...
- 理解netty对protocol buffers的编码解码
一,netty+protocol buffers简要说明 Netty是业界最流行的NIO框架之一优点:1)API使用简单,开发门槛低:2)功能强大,预置了多种编解码功能,支持多种主流协议:3)定制能力 ...
- netty系列之:使用Jboss Marshalling来序列化java对象
目录 简介 添加JBoss Marshalling依赖 JBoss Marshalling的使用 总结 简介 在JAVA程序中经常会用到序列化的场景,除了JDK自身提供的Serializable之外, ...
- Netty5+Jboss(Marshalling)完成对象序列化传输
TCP在网络通讯的时候,通常在解决TCP粘包.拆包问题的时候,一般会用以下几种方式: 1. 消息定长 例如每个报文的大小固定为200个字节,如果不够,空位补空格: 2. 在消息尾部添加特殊字符进行分割 ...
随机推荐
- Django restful
1.restful api的规范 API与用户的通信协议,总是使用HTTPs协议. 域名 https://api.example.com 尽量将API ...
- Spring AOP 配置通知方法的时候如何处理方法重载
如何在method属性里指定重载方法中的某一个?
- [Python]网络爬虫( 连载:大牛汪海 )
汪海个人博客:http://blog.callmewhy.com/ Python爬虫专栏,汪海专栏 Python爬虫入门教程 简单的介绍如何使用Python的相关模块如urllib2来实现网络爬虫的基 ...
- JS_高程1.javascript简介
1.javaScript诞生于1995年,最开始是为了处理以前由服务器端语言负责的一些诸如验证的操作. 2.一些名词: ECMA:European Computer Manufacturers Ass ...
- pygame-KidsCanCode系列jumpy-part7-游戏启动/结束画面
通常一个游戏启动(start)或结束(game over)时,都会显示一个画面,来引导用户.这节,我们学习如何处理这块逻辑. 其实之前,我们已经预留了2个函数,只要把它实现即可: def show_s ...
- Using async-await on .net 4
I'm currently starting to create an application that would profit a lot from C# 5's async-await feat ...
- x264阅读记录-1
x264阅读记录-1 采用x264版本是x264-snapshot-20060316-2245. 1. main函数 x264的main函数位于x264.c中,下面是main函数调用情况: (1)_s ...
- update使用inner join
一.update 基础语法 UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 上面是我们常见的更新表的方式,其实我们还可以去另外一张表的数据来更新当前的表数据,如现在就有这 ...
- angular 2 - 006 change detection 脏治检查 - DC
ANGULAR CHANGE DETECTION EXPLAINED 引发脏治检查有三种方式: Events - click, submit, - XHR - Fetching data from a ...
- linux 命令之 dmidecode
Dmidecode 这款软件同意你在 Linux 系统下获取有关硬件方面的信息.Dmidecode 遵循 SMBIOS/DMI 标准.其输出的信息包含 BIOS.系统.主板.处理器.内存.缓存等等. ...