Netty-Marshalling编解码

和前面的不一样,采用jboss-marshalling进行编解码,优先建立一个编解码的工厂类,供信息通信时候对信息的编解码java

import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
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;
/**
 * @FileName MarshallingFactory.java
 * @Description: 处理通信中消息的编解码
 *
 * @Date 2016年3月7日 
 * @author Administroter
 * @version 1.0
 * 
 */
public final class MarshallingFactory {
 /**
  * @Title: createMarshallingDecoder 
  * @Description:建立Marshalling解码器
  * @return 
  * @author Administroter
  * @date 2016年3月7日
  */
 public static MarshallingDecoder createMarshallingDecoder(){
  //实例化java序列化MarshallingFactory工厂,其中参数serial表示的就是java版的
  final MarshallerFactory mf = Marshalling.getProvidedMarshallerFactory("serial");
  final MarshallingConfiguration mc = new MarshallingConfiguration();
  mc.setVersion(5);
  UnmarshallerProvider up = new DefaultUnmarshallerProvider(mf, mc);
  //这里面的1024表示的是单个消息序列化后的最大长度
  MarshallingDecoder decoder =  new MarshallingDecoder(up,1024);
  return decoder;
 }
 /**
  * @Title: createMarshallingEncoder 
  * @Description:建立Marshalling编码器
  * @return 
  * @author Administroter
  * @date 2016年3月7日
  */
 public static MarshallingEncoder createMarshallingEncoder(){
  final MarshallerFactory mf = Marshalling.getProvidedMarshallerFactory("serial");
  final MarshallingConfiguration mc = new MarshallingConfiguration();
  mc.setVersion(5);
  MarshallerProvider up = new DefaultMarshallerProvider(mf, mc);
  //对象序列化成二进制数组
  MarshallingEncoder me = new MarshallingEncoder(up);
  return me;
 }
}

服务器端和客户端和前面的protobuf同样的,只须要更改下编解码就OK了数组

相关文章
相关标签/搜索