{"rsdb":{"rid":"242485","subhead":"","postdate":"0","aid":"166959","fid":"54","uid":"1","topic":"1","content":"

Netty\u4f5c\u4e3a\u5f88\u591a\u9ad8\u6027\u80fd\u7684\u5e95\u5c42\u901a\u8baf\u5de5\u5177\uff0c\u88ab\u5f88\u591a\u5f00\u53d1\u6846\u67b6\u5e94\u7528\u518d\u5e95\u5c42\uff0c\u4eca\u5929\u6765\u8bf4\u8bf4\u5e38\u7528\u7684\u5e8f\u5217\u5316\u5de5\u5177\uff0c\u7528Jboss\u7684Marshalling\u3002<\/p>

\u76f4\u63a5\u4e0a\u4ee3\u7801\uff0cMarshalling\u7684\u5de5\u5382\u7c7b<\/p>

package com.netty.serialize.marshalling;<\/p>

import io.netty.handler.codec.marshalling.*;<\/p>

import org.jboss.marshalling.MarshallerFactory;<\/p>

import org.jboss.marshalling.Marshalling;<\/p>

import org.jboss.marshalling.MarshallingConfiguration;<\/p>

 <\/p>

\/**<\/p>

 * Created by sdc on 2017\/8\/28.<\/p>

 *\/<\/p>

public class MarshallingCodeCFactory {<\/p>

 <\/p>

    \/**<\/p>

    * \u89e3\u7801<\/p>

    * @return<\/p>

    *\/<\/p>

    public static MarshallingDecoder buildMarshallingDecoder() {<\/p>

        \/\/\u9996\u5148\u901a\u8fc7Marshalling\u5de5\u5177\u7c7b\u7684\u7cbe\u901a\u65b9\u6cd5\u83b7\u53d6Marshalling\u5b9e\u4f8b\u5bf9\u8c61 \u53c2\u6570serial\u6807\u8bc6\u521b\u5efa\u7684\u662fjava<\/a>\u5e8f\u5217\u5316\u5de5\u5382\u5bf9\u8c61\u3002<\/p>

        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");<\/p>

        \/\/\u521b\u5efa\u4e86MarshallingConfiguration\u5bf9\u8c61\uff0c\u914d\u7f6e\u4e86\u7248\u672c\u53f7\u4e3a5<\/p>

        final MarshallingConfiguration configuration = new MarshallingConfiguration();<\/p>

        configuration.setVersion(5);<\/p>

        \/\/\u6839\u636emarshallerFactory\u548cconfiguration\u521b\u5efaprovider<\/p>

        UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);<\/p>

        \/\/\u6784\u5efaNetty\u7684MarshallingDecoder\u5bf9\u8c61\uff0c\u4fe9\u4e2a\u53c2\u6570\u5206\u522b\u4e3aprovider\u548c\u5355\u4e2a\u6d88\u606f\u5e8f\u5217\u5316\u540e\u7684\u6700\u5927\u957f\u5ea6<\/p>

        MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024);<\/p>

        return decoder;<\/p>

    }<\/p>

 <\/p>

    \/**<\/p>

    * \u7f16\u7801<\/p>

    * @return<\/p>

    *\/<\/p>

    public static MarshallingEncoder buildMarshallingEncoder() {<\/p>

        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");<\/p>

        final MarshallingConfiguration configuration = new MarshallingConfiguration();<\/p>

        configuration.setVersion(5);<\/p>

        MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);<\/p>

        \/\/\u6784\u5efaNetty\u7684MarshallingEncoder\u5bf9\u8c61\uff0cMarshallingEncoder\u7528\u4e8e\u5b9e\u73b0\u5e8f\u5217\u5316\u63a5\u53e3\u7684POJO\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u6570\u7ec4<\/p>

        MarshallingEncoder encoder = new MarshallingEncoder(provider);<\/p>

        return encoder;<\/p>

    }<\/p>

 <\/p>

}<\/p>

\u8fd9\u4e2a\u662fMarshalling\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\uff0cMarshalling\u81ea\u5e26\u7f16\u89e3\u7801\uff0c\u6240\u4ee5\u4e0d\u7528\u62c5\u5fc3\u4e2d\u9014\u7f16\u89e3\u7801\u534a\u5305\u7684\u95ee\u9898\u3002<\/p>

\u670d\u52a1\u7aef\u7684Server\u5b9e\u73b0\uff1a<\/p>

package com.netty.serialize.server;<\/p>

 <\/p>

import com.netty.serialize.coder.MsgDecoder;<\/p>

import com.netty.serialize.coder.MsgEncoder;<\/p>

import com.netty.serialize.handler.ServerHandler;<\/p>

import com.netty.serialize.marshalling.MarshallingCodeCFactory;<\/p>

import io.netty.bootstrap.ServerBootstrap;<\/p>

import io.netty.channel.*;<\/p>

import io.netty.channel.nio.NioEventLoopGroup;<\/p>

import io.netty.channel.socket.SocketChannel;<\/p>

import io.netty.channel.socket.nio.NioServerSocketChannel;<\/p>

 <\/p>

\/**<\/p>

 * Created by sdc on 2017\/8\/26.<\/p>

 *\/<\/p>

public class MsgServer {<\/p>

 <\/p>

    public void bind(int port) throws Exception {<\/p>

        EventLoopGroup bossGroup = new NioEventLoopGroup();<\/p>

        EventLoopGroup workerGroup = new NioEventLoopGroup();<\/p>

        try {<\/p>

            ServerBootstrap sb = ne","orderid":"0","title":"Netty5 \u5e8f\u5217\u5316\u65b9\u5f0f\uff08Jboss Marshalling\uff09(\u4e00)","smalltitle":"","mid":"0","fname":"linux\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"987","pages":"6","comments":"0","posttime":"2017-12-14 14:32:09","list":"1513233129","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Netty5<\/A> \u5e8f\u5217\u5316<\/A> \u65b9\u5f0f<\/A> Jboss<\/A> Marshalling<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.110.149.170","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Netty5 \u5e8f\u5217\u5316\u65b9\u5f0f\uff08Jboss Marshalling\uff09","lastview":"1713514826","digg_num":"2","digg_time":"1711309075","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}