Android 基於Netty的訊息推送方案之對象的傳遞(四)

來源:互聯網
上載者:User

標籤:android   blog   http   io   os   ar   java   for   strong   

在上一篇文章中《Android 基於Netty的訊息推送方案之字串的接收和發送(三)》我們介紹了Netty的字串傳遞,我們知道了Netty的訊息傳遞都是基於流,通過ChannelBuffer傳遞的,那麼自然,Object也需要轉換成ChannelBuffer來傳遞。好在Netty本身已經給我們寫好了這樣的轉換工具。ObjectEncoder和ObjectDecoder,下面我們介紹一個案例。

1. 我們構造一個用來傳輸的對象(JavaBean)

 

[java] view plaincopy 
  1. @SuppressWarnings("serial")  
  2. public class Command implements Serializable {  
  3.    
  4.    
  5.     private String actionName;  
  6.    
  7.     public String getActionName() {  
  8.         return actionName;  
  9.     }  
  10.    
  11.     public void setActionName(String actionName) {  
  12.         this.actionName = actionName;  
  13.     }  
  14. }  

2.我們先看一下Client的代碼

 

[java] view plaincopy 
  1. public class ObjectClient {  
  2.     public static void main(String args[]) {  
  3.         ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));  
  4.         bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
  5.             @Override  
  6.             public ChannelPipeline getPipeline() throws Exception {  
  7.                 return Channels.pipeline(new ObjectEncoder(), new ObjectClientHandler());  
  8.             }  
  9.         });  
  10.   
  11.         bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));  
  12.     }  
  13. }  
  14.   
  15. class ObjectClientHandler extends SimpleChannelHandler {  
  16.     /** 
  17.      * 當綁定到服務端的時候觸發,給服務端發訊息。 
  18.      */  
  19.     @Override  
  20.     public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {  
  21.         // 向服務端發送Object資訊  
  22.         sendObject(e.getChannel());  
  23.     }  
  24.     /** 
  25.      * 發送Object 
  26.      * @param channel 
  27.      */  
  28.     private void sendObject(Channel channel) {  
  29.         Command command = new Command();  
  30.         command.setActionName("Hello action.");  
  31.         channel.write(command);  
  32.     }  
  33. }  

3.再看一下服務端的代碼

 

 

[java] view plaincopy 
  1. public class ObjectServer {  
  2.     public static void main(String args[]) {  
  3.         // Server服務啟動器  
  4.         ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));  
  5.         // 設定一個處理用戶端訊息和各種訊息事件的類(Handler)  
  6.         bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
  7.             @Override  
  8.             public ChannelPipeline getPipeline() throws Exception {  
  9.                 //先編碼 --> 後處理自己的業務  
  10.                 return Channels.pipeline(new ObjectDecoder(ClassResolvers.cacheDisabled(this.getClass().getClassLoader())), new ObjectServerHandler());  
  11.             }  
  12.         });  
  13.         bootstrap.bind(new InetSocketAddress(8000));  
  14.     }  
  15. }  
  16.   
  17. class ObjectServerHandler extends SimpleChannelHandler {  
  18.     /** 
  19.      * 當接受到訊息的時候觸發 
  20.      */  
  21.     @Override  
  22.     public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {  
  23.         Command command = (Command) e.getMessage();  
  24.         // 列印看看是不是我們剛才傳過來的那個  
  25.         System.out.println(command.getActionName());  
  26.     }  
  27. }  

先運行服務端,再運行用戶端,然後在服務端的控制台中列印如下字串

 

 

[java] view plaincopy 
  1. Hello action.  

Android 基於Netty的訊息推送方案之對象的傳遞(四)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.