本文主要是介绍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内部模块对接的难度。
这篇关于Netty之Jboss Marshalling编解码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!