欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Netty学习教程之Netty与Marshalling结合发送对象

程序员文章站 2024-02-22 14:58:52
前言 之前的是netty简单的学习,我们可以传递一个字符串,那么如果我们想要在netty中传递一个对象该怎么办呢 ? 那么这个时候我们可以结合marshalling来传...

前言

之前的是netty简单的学习,我们可以传递一个字符串,那么如果我们想要在netty中传递一个对象该怎么办呢 ?

那么这个时候我们可以结合marshalling来传递。

方法如下:

首先需要导入两个marshalling的依赖包

jboss-marshalling-1.3.0.cr9.jar
jboss-marshalling-serial-1.3.0.cr9.jar

注意:我开始学习的时候只导入了第一个jar包,没有导入第二个,结果是不报错,但是客户端和服务端之间传递不了消息。所以两个包一定要都导入才行。

marshallingcodecfactory工具类

public class marshallingcodecfactory { 
 
 public static marshallingdecoder buildmarshallingdecoder() { 
  final marshallerfactory factory = marshalling.getprovidedmarshallerfactory("serial"); 
  final marshallingconfiguration configuration = new marshallingconfiguration(); 
  configuration.setversion(5); 
  unmarshallerprovider provider = new defaultunmarshallerprovider(factory, configuration); 
  marshallingdecoder decoder = new marshallingdecoder(provider, 1024*1024); 
  return decoder; 
 } 
 
 public static marshallingencoder buildmarshallingencoder() { 
  final marshallerfactory factory = marshalling.getprovidedmarshallerfactory("serial"); 
  final marshallingconfiguration configuration = new marshallingconfiguration(); 
  configuration.setversion(5); 
  marshallerprovider provider = new defaultmarshallerprovider(factory, configuration); 
  marshallingencoder encoder = new marshallingencoder(provider); 
  return encoder; 
 } 
} 

server端

public class server { 
  
 public static void main(string[] args) throws interruptedexception { 
  //1.第一个线程组是用于接收client端连接的 
  eventloopgroup bossgroup = new nioeventloopgroup();  
  //2.第二个线程组是用于实际的业务处理的 
  eventloopgroup workergroup = new nioeventloopgroup(); 
  serverbootstrap b = new serverbootstrap(); 
  b.group(bossgroup, workergroup);//绑定两个线程池 
  b.channel(nioserversocketchannel.class);//指定nio的模式,如果是客户端就是niosocketchannel 
  b.option(channeloption.so_backlog, 1024);//tcp的缓冲区设置 
  b.option(channeloption.so_sndbuf, 32*1024);//设置发送缓冲的大小 
  b.option(channeloption.so_rcvbuf, 32*1024);//设置接收缓冲区大小 
  b.option(channeloption.so_keepalive, true);//保持连续 
  b.childhandler(new channelinitializer<socketchannel>() { 
   protected void initchannel(socketchannel ch) throws exception { 
    //设置marshalling的编码和解码 
    ch.pipeline().addlast(marshallingcodecfactory.buildmarshallingdecoder()); 
    ch.pipeline().addlast(marshallingcodecfactory.buildmarshallingencoder()); 
    ch.pipeline().addlast(new serverthandler()); 
   } 
  }); 
  channelfuture future = b.bind(8765).sync();//绑定端口 
  future.channel().closefuture().sync();//等待关闭(程序阻塞在这里等待客户端请求) 
  bossgroup.shutdowngracefully();//关闭线程 
  workergroup.shutdowngracefully();//关闭线程 
 } 
 
} 

serverhandler处理类

public class serverthandler extends channelhandleradapter { 
 
 @override 
 public void exceptioncaught(channelhandlercontext ctx, throwable cause) 
   throws exception { 
  cause.printstacktrace(); 
 } 
 
 @override 
 public void channelread(channelhandlercontext ctx, object msg) 
   throws exception { 
  send send = (send) msg; 
  system.out.println("client发送:"+send); 
   
  receive receive = new receive(); 
  receive.setid(send.getid()); 
  receive.setmessage(send.getmessage()); 
  receive.setname(send.getname()); 
  ctx.writeandflush(receive); 
 } 
  
} 

由于我们已经在server端和client端定义了传递的类型又marshalling工厂处理,所以此时我们接收的时候直接转成发送的对象类型就行了。

client端

public class client { 
 
 public static void main(string[] args) throws interruptedexception { 
  eventloopgroup worker = new nioeventloopgroup(); 
  bootstrap b = new bootstrap(); 
  b.group(worker) 
  .channel(niosocketchannel.class) 
  .handler(new channelinitializer<socketchannel>() { 
   @override 
   protected void initchannel(socketchannel sc) throws exception { 
    //bytebuf buf = unpooled.copiedbuffer("$_".getbytes()); 
    //sc.pipeline().addlast(new delimiterbasedframedecoder(1024,buf)); 
    //sc.pipeline().addlast(new stringdecoder()); 
    sc.pipeline().addlast(marshallingcodecfactory.buildmarshallingdecoder()); 
    sc.pipeline().addlast(marshallingcodecfactory.buildmarshallingencoder()); 
    sc.pipeline().addlast(new clienthandler()); 
   } 
  }); 
  channelfuture f=b.connect("127.0.0.1",8765).sync(); 
  for(int i=1;i<=5;i++){ 
   send send = new send(); 
   send.setid(i); 
   send.setmessage("message"+i); 
   send.setname("name"+i); 
   f.channel().writeandflush(send); 
  } 
  f.channel().closefuture().sync(); 
  worker.shutdowngracefully(); 
 } 
} 

clienthandler端

public class clienthandler extends channelhandleradapter{ 
  @override 
  public void exceptioncaught(channelhandlercontext ctx, throwable cause) 
    throws exception { 
   cause.printstacktrace(); 
   ctx.close(); 
  } 
 
  @override 
  public void channelread(channelhandlercontext ctx, object msg) 
    throws exception { 
   receive receive = (receive) msg; 
   system.out.println("server反馈:"+receive); 
  } 
} 

send类

public class send implements serializable { 
 
 /** 
  * serialversionuid:todo(用一句话描述这个变量表示什么) 
  * 
  * @since 1.0.0 
  */ 
 
 private static final long serialversionuid = 1l; 
 
 private integer id; 
 private string name; 
 private string message; 
 
 public integer getid() { 
  return id; 
 } 
 
 public void setid(integer id) { 
  this.id = id; 
 } 
 
 public string getname() { 
  return name; 
 } 
 
 public void setname(string name) { 
  this.name = name; 
 } 
 
 public string getmessage() { 
  return message; 
 } 
 
 public void setmessage(string message) { 
  this.message = message; 
 } 
 
 @override 
 public string tostring() { 
  return "send [id=" + id + ", name=" + name + ", message=" + message + "]"; 
 } 
 
} 

receive类

public class receive implements serializable{ 
 
 /** 
  * serialversionuid:todo(用一句话描述这个变量表示什么) 
  * @since 1.0.0 
  */ 
  
 private static final long serialversionuid = 1l; 
 private integer id; 
 private string name; 
 private string message; 
 private byte[] sss; 
  
 public byte[] getsss() { 
  return sss; 
 } 
 public void setsss(byte[] sss) { 
  this.sss = sss; 
 } 
 public integer getid() { 
  return id; 
 } 
 public void setid(integer id) { 
  this.id = id; 
 } 
 public string getname() { 
  return name; 
 } 
 public void setname(string name) { 
  this.name = name; 
 } 
 public string getmessage() { 
  return message; 
 } 
 public void setmessage(string message) { 
  this.message = message; 
 } 
 @override 
 public string tostring() { 
  return "receive [id=" + id + ", name=" + name + ", message=" + message + ", sss=" + arrays.tostring(sss) + "]"; 
 } 
  
} 

注意:send类和receive这两个类,我们再真实环境开发的时候服务器和客户端往往是两个web应用程序,在这里我们要注意服务端和客户端之间的两个类类名和包名在两端要完全相同。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。