JBoss Marshalling 是一个Java对象序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调的参数和附加的特性。
Marshalling开发环境准备
下载相关的Marshalling类库:地址,将该jar导入项目即可。
创建Marshalling编解码器
通过创建MarshallingCodeCFactory工厂类来创建MarshallingDecoder解码器与MarshallingEncoder编码器。
package com.ph.Netty;
import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
/**
* Create by PH on 2018/11/10
*/
public final class MarshallingCodeCFactory {
/**
* JBoss Marshalling 解码器
* @return
*/
public static MarshallingDecoder buildMarshallingDecoder() {
//参数“serial”表示创建的是Java序列化工厂对象
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
//1024表示单个消息序列化后的最大长度
MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024);
return decoder;
}
/**
* JBoss Marshalling 编码器
* @return
*/
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 encoder = new MarshallingEncoder(provid