和前面的不一樣,採用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了數組