设为首页 加入收藏

TOP

2.4.2 AIO创建的TimeClient源码分析(1)
2014-07-09 13:01:21 来源: 作者: 【 】 浏览:163
Tags:2.4.2 AIO 创建 TimeClient 源码 分析

2.4.2  AIO创建的TimeClient源码分析(1)

首先看下客户端主函数的实现。

代码清单2-15  AIO时间服务器客户端  TimeClient
 

  1. 16.         try {  
  2. 17.         port = Integer.valueOf(args[0]);  
  3. 18.         } catch (NumberFormatException e) {  
  4. 19.         // 采用默认值  
  5. 20.         }  
  6. 21.     }  
  7. 22.     new Thread(new AsyncTimeClientHandler("127.0.0.1", port),  
  8. 23.         "AIO-AsyncTimeClientHandler-001").start();  
  9. 24.   
  10. 25.     }  
  11. 26. } 

第22行我们通过一个独立的I/O线程创建异步时间服务器客户端handler,在实际项目中,我们不需要独立的线程创建异步连接对象,因为底层都是通过JDK的系统回调实现的,在后面运行时间服务器程序的时候,我们会抓取线程调用堆栈给大家展示。

继续看代码,AsyncTimeClientHandler的实现类源码如下。

代码清单2-16  AIO时间服务器客户端AsyncTimeClientHandler
 

  1. 1.  package com.phei.netty.aio;  
  2. 2.    
  3. 3.  import java.io.IOException;  
  4. 4.  import java.io.UnsupportedEncodingException;  
  5. 5.  import java.net.InetSocketAddress;  
  6. 6.  import java.nio.ByteBuffer;  
  7. 7.  import java.nio.channels.AsynchronousSocketChannel;  
  8. 8.  import java.nio.channels.CompletionHandler;  
  9. 9.  import java.util.concurrent.CountDownLatch;  
  10. 10.   
  11. 11. /**  
  12. 12.  * @author Administrator  
  13. 13.  * @date 2014年2月16日  
  14. 14.  * @version 1.0  
  15. 15.  */  
  16. 16. public class AsyncTimeClientHandler implements  
  17. 17.     CompletionHandler<Void, AsyncTimeClientHandler>, Runnable {  
  18. 18.   
  19. 19.     private AsynchronousSocketChannel client;  
  20. 20.     private String host;  
  21. 21.     private int port;  
  22. 22.     private CountDownLatch latch;  
  23. 23.   
  24. 24.     public AsyncTimeClientHandler(String host, int port) {  
  25. 25.     this.host = host;  
  26. 26.     this.port = port;  
  27. 27.     try {  
  28. 28.         client = AsynchronousSocketChannel.open();  
  29. 29.     } catch (IOException e) {  
  30. 30.         e.printStackTrace();  
  31. 31.     }  
  32. 32.     }  
  33. 33.   
  34. 34.     @Override  
  35. 35.     public void run() {  
  36. 36.     latch = new CountDownLatch(1);  
  37. 37.     client.connect(new InetSocketAddress(host, port), this, this);  
  38. 38.     try {  
  39. 39.         latch.await();  
  40. 40.     } catch (InterruptedException e1) {  
  41. 41.         e1.printStackTrace();  
  42. 42.     }  
  43. 43.     try {  
  44. 44.         client.close();  
  45. 45.     } catch (IOException e) {  
  46. 46.         e.printStackTrace();  
  47. 47.     }  
  48. 48.     }  
  49. 49.   
  50. 50.     @Override  
  51. 51.     public void completed(Void result, AsyncTimeClientHandler attachment) {  
  52. 52.     byte[] req = "QUERY TIME ORDER".getBytes();  
  53. 53.     ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);  
  54. 54.     writeBuffer.put(req);  
  55. 55.     writeBuffer.flip();  
  56. 56.     client.write(writeBuffer, writeBuffer,  
  57. 57.         new CompletionHandler<Integer, ByteBuffer>() {  
  58. 58.             @Override  
  59. 59.             public void completed(Integer result, ByteBuffer buffer) {  
  60. 60.             if (buffer.hasRemaining()) {  
  61. 61.                 client.write(buffer, buffer, this);  
  62. 62.             } else {  
  63. 63.                 ByteBuffer readBuffer = ByteBuffer.allocate(1024);  
  64. 64.                 client.read(  
  65. 65.                     readBuffer,  
  66. 66.                     readBuffer,  
  67. 67.                     new CompletionHandler<Integer, ByteBuffer>() {  
  68. 68.                     @Override  
  69. 69.                     public void completed(Integer result,  
  70. 70.                         ByteBuffer buffer) {  
  71. 71.                         buffer.flip();  
  72. 72.                         byte[] bytes = new byte[buffer  
  73. 73.                             .remaining()];  
  74. 74.                         buffer.get(bytes);  
  75. 75.                         String body;  
  76. 76.                         try {  
  77. 77.                         body = new String(bytes,  
  78. 78.                             "UTF-8");  
  79. 79.                         System.out.println("Now is : "  
  80. 80.                             + body);  
  81. 81.                         latch.countDown();  
  82. 82.                         } catch (UnsupportedEncodingException e) {  
  83. 83.                         e.printStackTrace();  
  84. 84.                         }  
  85. 85.                     }  
  86. 86.   
  87. 87.                     @Override  
  88. 88.                     public void failed(Throwable exc,  
  89. 89.                         ByteBuffer attachment) {  
  90. 90.                         try {  
  91. 91.                         client.close();  
  92. 92.                         latch.countDown();  
  93. 93.                         } catch (IOException e) {  
  94. 94.                         // ingnore on close  
  95. 95.                         }  
  96. 96.                     }  
  97. 97.                     });  
  98. 98.             }  
  99. 99.             }  
  100. 100.      
  101. 101.                @Override  
  102. 102.                public void failed(Throwable exc,ByteBuffer attachment) {  
  103. 103.                try {  
  104. 104.                    client.close();  
  105. 105.                    latch.countDown();  
  106. 106.                } catch (IOException e) {  
  107. 107.                    // ingnore on close  
  108. 108.                }  
  109. 109.                }  
  110. 110.            });  
  111. 111.        }  
  112. 112.      
  113. 113.        @Override  
  114. 114.        public void failed(Throwable exc, AsyncTimeClientHandler attachment) {  
  115. 115.        exc.printStackTrace();  
  116. 116.        try {  
  117. 117.            client.close();  
  118. 118.            latch.countDown();  
  119. 119.        } catch (IOException e) {  
  120. 120.            e.printStackTrace();  
  121. 121.        }  
  122. 122.        }  
  123. 123.    } 

喜欢的朋友可以添加我们的微信账号:

51CTO读书频道二维码


51CTO读书频道活动讨论群:342347198

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.4.1 AIO创建的TimeServer源码分.. 下一篇2.4.2 AIO创建的TimeClient源码分..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言指针从入门到基 (2025-12-26 05:21:36)
·【C语言指针初阶】C (2025-12-26 05:21:33)
·C语言指针的定义和使 (2025-12-26 05:21:31)
·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)