微博Motan中的transport模組一些點
首先看一段程式碼
nettyClient = new NettyClient(url); nettyClient.open(); RpcContext.getContext().putAttribute(MotanConstants.ASYNC_SUFFIX, true); Response response; try { response = nettyClient.request(request); Assert.assertTrue(response instanceof ResponseFuture); Object result = response.getValue();//獲取返回結果會阻塞 RpcContext.destroy(); Assert.assertNotNull(result); Assert.assertEquals("method: " + request.getMethodName() + " requestId: " + request.getRequestId(), result); } catch (MotanServiceException e) { e.printStackTrace(); assertTrue(false); } catch (Exception e) { e.printStackTrace(); assertTrue(false); }
主要是建立一個客戶端的連線,傳送請求,然後獲取返回。
nettyClient.open(); open的時候會去建立一個channel的連線池,可以點進去看 所以當呼叫request的時候最終會走到這樣一個方法 private Response request(Request request, boolean async) throws TransportException { Channel channel; Response response; try { channel = getChannel();//從連線池獲取channel if (channel == null) { LoggerUtil.error("NettyClient borrowObject null: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request)); return null; } // async request response = channel.request(request); } catch (Exception e) { } // aysnc or sync result response = asyncResponse(response, async); return response; } 最終發起呼叫是在com.weibo.api.motan.transport.netty4.NettyChannel中的request方法 @Override public Response request(Request request) throws TransportException { int timeout = nettyClient.getUrl().getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.requestTimeout.getName(), URLParamType.requestTimeout.getIntValue()); if (timeout
當服務端返回資料後會呼叫處理返回的handler
com.weibo.api.motan.transport.netty4.NettyClient中的handler new MessageHandler() { @Override public Object handle(Channel channel, Object message) { Response response = (Response) message; System.out.println("獲取到message"); ResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId()); if (responseFuture == null) { LoggerUtil.warn("NettyClient has response from server, but responseFuture not exist, requestId={}", response.getRequestId()); return null; } if (response.getException() != null) { responseFuture.onFailure(response); } else { responseFuture.onSuccess(response); } return null; } } 可以看到如果服務端返回了,就會獲取到響應的DefaultResponse 然後將返回結果傳入到之前構建的responseFuture if (response.getException() != null) { responseFuture.onFailure(response); } else { responseFuture.onSuccess(response); } onSuccess中會呼叫之前的listener,執行listen回撥方法關於
DefaultResponseFuture 透過物件鎖protected Object lock = new Object();來保證等待結果的返回 獲取返回結果會阻塞,如果服務端還沒有返回 Object result = response.getValue();//獲取返回結果會阻塞 @Override public Object getValue() { System.out.println("getValue"); synchronized (lock) { if (!isDoing()) { return getValueOrThrowable();//如果不是在執行就報錯 } System.out.println("timeout:"+timeout); if (timeout 0) { for (; ; ) { try { lock.wait(waitTime); } catch (InterruptedException e) { } if (!isDoing()) { break; } else { waitTime = timeout - (System.currentTimeMillis() - createTime); if (waitTime
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1747/viewspace-2812489/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 模組化的一些小研究
- FreeSwitch一些模組的安裝
- vscode中隱藏節點模組的外掛VSCode
- node中事件(events)模組一些用法和原理事件
- 微軟Sharepoint的一些缺點微軟
- JavaScript中的模組匯入有一個缺點JavaScript
- mongodb核心transport_layer網路傳輸層模組原始碼實現三MongoDB原始碼
- mongodb核心transport_layer 網路傳輸層模組原始碼實現四MongoDB原始碼
- 關於ApiCloud的Superwebview在androidstudio中整合微信支付模組,提示模組未繫結的問題APICloudWebViewAndroid
- 關於ApiCloud的Superwebview在androidstudio中整合微信支付模組,提示模組未繫結的問題...APICloudWebViewAndroid
- 聊聊中後臺前端應用:模組相關的一些事前端
- Python模擬微博登陸,親測有效Python
- PHP基於laravel框架獲取微博資料之一 模擬新浪微博登入PHPLaravel框架
- python中的chardet模組Python
- Python中模組的使用Python
- JavaScript 中的模組化JavaScript
- Python中的abc模組Python
- Nodejs中的stream模組NodeJS
- 微人事(一)登入模組
- Laravel——商品模組難點Laravel
- spring+motan+zookeeper入門Spring
- 探索 JS 中的模組化JS
- ruby 中的 forwardable 模組(1)Forward
- Python 中argparse模組的使用Python
- Magix中的快取模組快取
- Python中paramiko 模組的用法Python
- Python中operator 模組的用法Python
- Python中pathlib 模組的用法Python
- Python中itertools 模組的用法Python
- python中重要的模組--asyncioPython
- javascript中的模組系統JavaScript
- SpringBoot多模組專案中無法注入其他模組中的spring beanSpring BootBean
- pickle模組 collections模組在物件導向中的應用物件
- 前端微模組更值得被推崇!前端
- 模切ERP系統的功能模組分析——點晴ERP
- 微博收集
- Python中爬蟲模組有哪些?優缺點介紹!Python爬蟲
- 基於JSP的微博JS