和前面的不同,采用jboss-marshalling进行编解码,优先创建一个编解码的工厂类,供信息通讯时候对信息的编解码
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了