基本介紹
- 外文名:Marshalling
- 領域:計算機科學
簡介,用途,例子,與序列化的比較,JBoss的Marshalling序列化框架,
簡介
用途
Marshalling被用於實現不同的遠程過程調用(RPC)機制,這必須用到進程間的數據傳輸。Microsoft的組件對象模型(COM)中,當跨COM的套間(apartment)傳遞接口指針時,接口指針必須被marshalled。在.NET Framework下,在非受管(unmanaged)類型與CLR類型間,例如P/Invoke過程,必須做marshalling。例如,C#程式調用C語言寫的DLL,其中函式參數使用字元串,就需要做marshalling。
使用Mozilla應用程式框架的XPCOM的腳本或應用程式,廣泛使用marshalling。
例子
Microsoft Windows系列作業系統,Direct3D設備驅動程式是核心態驅動程式。DirectX運行時處理用戶態API。用戶態執行系統調用來執行核心態操作,需要CPU切換為核心態,這將耗費微秒級的時間來完成。在此期間,CPU不能執行任何操作。為最佳化性能,必須極小化CPU這種模式切換。 LinuxOpenGL驅動程式分為兩部分:核心驅動與用戶空間驅動。用戶空間驅動把所有OpenGL命令翻譯為機器碼提交給GPU。為減少系統調用,用戶空間驅動實現marshalling。當GPU的命令緩衝區(command buffer)裝滿了繪圖數據,API簡單地把請求繪製的調用存放在一個臨時緩衝區;當命令緩衝區接近為空,執行核心模式切換一次性增加被存儲的命令。
與序列化的比較
術語“marshal”被Python標準庫認為與“序列化”同義。但與Java相關的RFC 2713不認為二者是同義:
"marshal"一個對象意味著記錄下它的狀態與codebase(s)在這種方式,以便當這個marshal對象在被"unmarshalled"時,可以獲得最初代碼的一個副本,可能會自動裝入這個對象的類定義。可以marshal任何能被序列化或遠程(即實現java.rmi.Remote接口)。Marshalling類似序列化,除了marshalling也記錄codebases。Marshalling不同於序列化是marshalling特別處理遠程對象。
JBoss的Marshalling序列化框架
JBoss Marshalling是一個Java對象序列化包,對JDK默認的序列化框架進行了最佳化,但又保持跟java.io.Serializable接口的兼容,同時增加了一些可調的參數和附加的特性,這些參數和特性可通過工廠類進行配置。
import lombok.Data; import java.io.Serializable; @Datapublic class SubscribeReq implements Serializable { /** * 默認的序列號ID */ private static final long serialVersionUID = 1L; private int subReqID; private String userName; private String productName; private String phoneNumber; private String address; @Override public String toString() { return "SubscribeReq [subReqID=" + subReqID + ", userName=" + userName + ", productName=" + productName + ", phoneNumber=" + phoneNumber + ", address=" + address + "]"; }} import lombok.Data; import java.io.Serializable; @Datapublic class SubscribeResp implements Serializable { /** * 默認序列ID */ private static final long serialVersionUID = 1L; private int subReqID; private int respCode; private String desc; @Override public String toString() { return "SubscribeResp [subReqID=" + subReqID + ", respCode=" + respCode + ", desc=" + desc + "]"; } }
編解碼工廠類:
import io.netty.handler.codec.marshalling.*;import org.jboss.marshalling.MarshallerFactory;import org.jboss.marshalling.Marshalling;import org.jboss.marshalling.MarshallingConfiguration;public final class MarshallingCodeCFactory { /** * 創建Jboss Marshalling解碼器MarshallingDecoder */ public static MarshallingDecoder buildMarshallingDecoder() { //首先通過Marshalling工具類的getProvidedMarshallerFactory靜態方法獲取MarshallerFactory實例 //參數“serial”表示創建的是Java序列化工廠對象,它由jboss-marshalling-serial-1.3.0.CR9.jar提供。 final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial"); //創建了MarshallingConfiguration對象 final MarshallingConfiguration configuration = new MarshallingConfiguration(); //將它的版本號設定為5 configuration.setVersion(5); //然後根據MarshallerFactory和MarshallingConfiguration創建UnmarshallerProvider實例 UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration); //最後通過構造函式創建Netty的MarshallingDecoder對象 //它有兩個參數,分別是UnmarshallerProvider和單個訊息序列化後的最大長度。 MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024); return decoder; } /** * 創建Jboss Marshalling編碼器MarshallingEncoder */ public static MarshallingEncoder buildMarshallingEncoder() { final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial"); final MarshallingConfiguration configuration = new MarshallingConfiguration(); configuration.setVersion(5); //創建MarshallerProvider對象,它用於創建Netty提供的MarshallingEncoder實例 MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration); //MarshallingEncoder用於將實現序列化接口的POJO對象序列化為二進制數組。 MarshallingEncoder encoder = new MarshallingEncoder(provider); return encoder; }}
服務端代碼示例:
import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;public class SubReqServer { public void bind(int port) throws Exception { // 配置服務端的NIO執行緒組 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer() { @Override public void initChannel(Channel ch) { //通過工廠類創建MarshallingEncoder解碼器,並添加到ChannelPipeline. ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder()); //通過工廠類創建MarshallingEncoder編碼器,並添加到ChannelPipeline中。 ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder()); ch.pipeline().addLast(new SubReqServerHandler()); } }); // 綁定連線埠,同步等待成功 ChannelFuture f = b.bind(port).sync(); // 等待服務端監聽連線埠關閉 f.channel().closeFuture().sync(); } finally { // 優雅退出,釋放執行緒池資源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException e) { // 採用默認值 } } new SubReqServer().bind(port); }}import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;@ChannelHandler.Sharablepublic class SubReqServerHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //經過解碼器handler ObjectDecoder的解碼, //SubReqServerHandler接收到的請求訊息已經被自動解碼為SubscribeReq對象,可以直接使用。 SubscribeReq req = (SubscribeReq) msg; if ("Lilinfeng".equalsIgnoreCase(req.getUserName())) { System.out.println("Service accept client subscribe req : [" + req.toString() + "]"); //對訂購者的用戶名進行合法性校驗,校驗通過後列印訂購請求訊息,構造訂購成功應答訊息立即傳送給客戶端。 ctx.writeAndFlush(resp(req.getSubReqID())); } } private SubscribeResp resp(int subReqID) { SubscribeResp resp = new SubscribeResp(); resp.setSubReqID(subReqID); resp.setRespCode(0); resp.setDesc("Netty book order succeed, 3 days later, sent to the designated address"); return resp; } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close();// 發生異常,關閉鏈路 }}
客戶端代碼示例:
import io.netty.bootstrap.Bootstrap;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioSocketChannel;public class SubReqClient { public void connect(int port, String host) throws Exception { // 配置客戶端NIO執行緒組 EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer() { @Override public void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder()); ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder()); ch.pipeline().addLast(new SubReqClientHandler()); } }); // 發起異步連線操作 ChannelFuture f = b.connect(host, port).sync(); // 等待客戶端鏈路關閉 f.channel().closeFuture().sync(); } finally { // 優雅退出,釋放NIO執行緒組 group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException e) { // 採用默認值 } } new SubReqClient().connect(port, "127.0.0.1"); }}import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;public class SubReqClientHandler extends ChannelHandlerAdapter { public SubReqClientHandler() { } @Override public void channelActive(ChannelHandlerContext ctx) { //在鏈路激活的時候循環構造10條訂購請求訊息,最後一次性地傳送給服務端。 for (int i = 0; i < 10; i++) { ctx.write(subReq(i)); } ctx.flush(); } private SubscribeReq subReq(int i) { SubscribeReq req = new SubscribeReq(); req.setAddress("南京市江寧區方山國家地質公園"); req.setPhoneNumber("138xxxxxxxxx"); req.setProductName("Netty For Marshalling"); req.setSubReqID(i); req.setUserName("Lilinfeng"); return req; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //由於對象解碼器已經對訂購請求應答訊息進行了自動解碼, //因此,SubReqClientHandler接收到的訊息已經是解碼成功後的訂購應答訊息。 System.out.println("Receive server response : [" + msg + "]"); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); }}
運行結果:
服務端結果:
14:48:45.475 [nioEventLoopGroup-2-1] INFO i.n.handler.logging.LoggingHandler - [id: 0x71ea5def, /0:0:0:0:0:0:0:0:8080] RECEIVED: [id: 0x876eb7b4, /127.0.0.1:57423 => /127.0.0.1:8080]14:48:45.707 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetectionLevel: simpleService accept client subscribe req : [SubscribeReq [subReqID=0, userName=Lilinfeng, productName=Netty For Marshalling, phoneNumber=138xxxxxxxxx, address=南京市江寧區方山國家地質公園]]Service accept client subscribe req : [SubscribeReq [subReqID=1, userName=Lilinfeng, productName=Netty For Marshalling, phoneNumber=138xxxxxxxxx, address=南京市江寧區方山國家地質公園]]..........................................................................Service accept client subscribe req : [SubscribeReq [subReqID=9, userName=Lilinfeng, productName=Netty For Marshalling, phoneNumber=138xxxxxxxxx, address=南京市江寧區方山國家地質公園]]
客戶端結果:
Receive server response : [SubscribeResp [subReqID=0, respCode=0, desc=Netty book order succeed, 3 days later, sent to the designated address]]..........................................................................Receive server response : [SubscribeResp [subReqID=9, respCode=0, desc=Netty book order succeed, 3 days later, sent to the designated address]]