Netty之Jboss Marshalling编解码

Netty之JbossMarshalling编解码

    JbossMarshalling是一个java对象序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调的参数和附加的特性,这些参数和特性可通过工厂类进行配置。

一.服务端开发

1.1 SubReqServer实现

package marshalling;

 

import serializable.SubReqClient;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

importio.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

importio.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.handler.logging.LogLevel;

importio.netty.handler.logging.LoggingHandler;

 

public class SubReqServer {

         publicvoid bind(int port) throws Exception{

                   //配置服务端的NIO线程组

                   EventLoopGroupbossGroup=new NioEventLoopGroup();

                   EventLoopGroupworkerGroup=new NioEventLoopGroup();

                   try{

                            ServerBootstrapb=new ServerBootstrap();

                            b.group(bossGroup,workerGroup)

                            .channel(NioServerSocketChannel.class)

                            .option(ChannelOption.SO_BACKLOG,100)

                            .handler(newLoggingHandler(LogLevel.INFO))

                            .childHandler(newChannelInitializer<SocketChannel>() {

                                     @Override

                                     publicvoid initChannel(SocketChannel ch){

                                               ch.pipeline().addLast(

                                                                 MarshallingCodeCFatory.buildMarshallingDecoder());

                                               ch.pipeline().addLast(

                                                                 MarshallingCodeCFatory.buildMarshallingEncoder());

                                               ch.pipeline().addLast(newSubReqServerHandler());

                                     }

                            });

              // 绑定端口,同步等待成功

                            ChannelFuturef=b.bind(port).sync();

                            //等待服务端监听端口关闭

                            f.channel().closeFuture().sync();

 

                   }catch (Exception e) {

                            //TODO: handle exception

                   }

                   finally{

                            //优雅退出,释放线程池资源

                            bossGroup.shutdownGracefully();

                            workerGroup.shutdownGracefully();

                   }

         }

        

         publicstatic void main(String[] args) throws Exception{

                   intport=8080;

                   if(args!=null&&args.length>0){

                            try{

                                     port=Integer.valueOf(args[0]);

                                    

                            }catch (NumberFormatException e) {

                                     //TODO: handle exception

                                    

                            }

                            newSubReqClient().connect(port,"127.0.0.1");

                   }

         }

 

}    

   通过MarshallingCodeCFactory工厂类创建了MarshallingDecoder解码器,并将其加入到ChannelPipeline中;通过工厂类创建MarshallingEncoder编码器,并添加到ChannelPipeline中。

1.2 MarshallingCodeFactory实现

package marshalling;

 

importio.netty.handler.codec.marshalling.DefaultMarshallerProvider;

importio.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;

importio.netty.handler.codec.marshalling.MarshallerProvider;

importio.netty.handler.codec.marshalling.MarshallingDecoder;

importio.netty.handler.codec.marshalling.MarshallingEncoder;

importio.netty.handler.codec.marshalling.UnmarshallerProvider;

 

importorg.jboss.marshalling.MarshallerFactory;

import org.jboss.marshalling.Marshalling;

importorg.jboss.marshalling.MarshallingConfiguration;

 

public class MarshallingCodeCFactory {

        

         publicstatic MarshallingDecoder buildMarshallingDecoder() {

         //参数serial表示创建的是java序列化工厂对象

                   finalMarshallerFactory marshallerFactory=Marshalling.

                                     getProvidedMarshallerFactory("serial");

                   finalMarshallingConfiguration configuration=new MarshallingConfiguration();

                   //设置版本号为5

                   configuration.setVersion(5);

                   UnmarshallerProviderprovider=new

                                     DefaultUnmarshallerProvider(marshallerFactory,configuration);

                   //最大长度为1M

                   MarshallingDecoderdecoder=new MarshallingDecoder(provider,1024);

                   returndecoder;

         }

        

         /*/

          * 创建jboss marshalling编码器marshallingencoder

          */

   public static MarshallingEncoder buildMarshallingEncoder() {

             final MarshallerFactorymarshallerFactory=Marshalling.

                                     getProvidedMarshallerFactory("serial");

                   finalMarshallingConfiguration configuration=new MarshallingConfiguration();

                   configuration.setVersion(5);

                   MarshallerProviderprovider=new DefaultMarshallerProvider(

                                     marshallerFactory,configuration);

                   MarshallingEncoderencoder=new MarshallingEncoder(provider);

                   returnencoder;

                                    

    }

}

   首先通过Marshalling工具类的getProvidedMarshallerFactory静态方法获取MarshallerFactory实例,参数“serial”表示创建的是java序列化工厂对象。创建MarshallingConfiguration对象,将它的版本号设置为5,然后根据MarshallerFactory和MarshallingConfiguration创建UnmarshallerProvider实例,最后通过构造函数创建netty的marshallingDecoder对象。

二.客户端开发

2.1 SubReqClient实现

package marshalling;

 

import serializable.SubReqClient;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

importio.netty.channel.nio.NioEventLoopGroup;

importio.netty.channel.socket.SocketChannel;

importio.netty.channel.socket.nio.NioSocketChannel;

 

public class SubReqClient {

         publicvoid connect(int port,String host) throws Exception{

                   //配置客户端NIO线程组

                   EventLoopGroupgroup=new NioEventLoopGroup();

                   try{

                            Bootstrapb=new Bootstrap();

                            b.group(group).channel(NioSocketChannel.class)

                            .option(ChannelOption.TCP_NODELAY,true)

                            .handler(newChannelInitializer<SocketChannel>() {

                                     @Override

                                     publicvoid initChannel(SocketChannel ch) throws Exception{

                                               ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());

                                               ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());

                                               ch.pipeline().addLast(newSubReqClientHandler());

                                     }

                            });

                            //发起异步连接操作

                            ChannelFuturef=b.connect(host,port).sync();

                            //等待客户端链路关闭

                            f.channel().closeFuture().sync();

                   }catch (Exception e) {

                            //TODO: handle exception

                   }finally{

                            //优雅退出,释放NIO线程组

                            group.shutdownGracefully();

                   }

         }

  

        

         publicstatic void main(String[] args) throws Exception{

                   intport=8080;

                   if(args!=null&&args.length>0){

                            try{

                                     port=Integer.valueOf(args[0]);

                                    

                            }catch (NumberFormatException e) {

                                     //TODO: handle exception

                                    

                            }

                            newSubReqClient().connect(port,"127.0.0.1");

                   }

         }

}

    客户端成功接收到了服务端返回的10条应答消息,subReqID为从0到9,与服务端发送的应答消息完全一致。

    利用netty的marshalling编解码器,可以轻松地开发出与jboss内部模块进行远程通信的程序,而且支持异步非阻塞,这无疑降低了基于netty开发应用程序与jboss内部模块对接的难度。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值