亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Netty學習教程之Netty與Marshalling結合發送對象

發布時間:2020-08-24 23:48:51 來源:腳本之家 閱讀:124 作者:荒野的塵埃 欄目:編程語言

前言

之前的一篇文章是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應用程序,在這里我們要注意服務端和客戶端之間的兩個類類名和包名在兩端要完全相同。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

镶黄旗| 沙湾县| 夹江县| 沁源县| 谢通门县| 天水市| 福清市| 庐江县| 天津市| 繁峙县| 乌鲁木齐县| 中宁县| 乐清市| 墨脱县| 东山县| 永平县| 平顺县| 浙江省| 蒙自县| 木兰县| 田东县| 榆中县| 文水县| 临江市| 射阳县| 顺昌县| 铜鼓县| 西华县| 赣榆县| 乌拉特中旗| 磐安县| 吉林省| 朔州市| 正定县| 库尔勒市| 皮山县| 双牌县| 丹棱县| 叙永县| 即墨市| 平远县|