Zorka源碼閱讀與講解(2)怎樣實現Online Reconfiguration?,reconfiguration

來源:互聯網
上載者:User

Zorka源碼閱讀與講解(2)怎樣實現Online Reconfiguration?,reconfiguration

也同樣是想通過這篇日誌,講講在Java中怎麼實現插樁的線上配置和管理。在之前這篇日誌中,我簡單介紹了Online Reconfiguration的基本效果,這裡講講Zorka是怎麼實現的(其實遠比大家想想的要簡單)。

之前那篇日誌中我講過,我們就從reload這個方法入手。最簡單的方法是,直接在Eclipse中搜尋reload這個方法。Search——File——Containing Text中填入reload,然後在Scope中選擇Enclosing Projects,點Search,就會出現一些match的結果,我們簡單瀏覽一下,發現有個類叫ZorkaControlMBean,點開一看,發現只是個介面。我們在這個介面的名字上點右鍵——References——Project,就可以看到原來只有一個ZorkaControl類執行個體化了這個介面,看到這個類的reload方法只是調用了AgentInstance的對象的同名方法。

所以我們只需要看看AgentInstance這個類的reload方法:這個方法讀起來也很簡單,我就不詳細展開介紹了,唯一需要注意的就是這條語句:

getRetransformer().retransform(oldSet, newSet, false);

我們可以看到,實際上就是這條語句實現了真正的Reconfiguration,按照一樣的方法,可以找到retransform這個方法實際上是在RealSpyRetransformer的retransform這個方法中進行了實現,AgentInstance這個類中的成員變數:

private SpyRetransformer retransformer;

實際上是RealSpyRetransformer這個類的對象。我們看一下RealSpyRetransformer這個類中的retransform這個方法,重點看一下這一條語句:

instrumentation.retransformClasses(classes.toArray(new Class[0]));

原來直接調用的是instrumentation對象的retransformClasses這個方法。關於這個方法,我們可以參看這裡:http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html 對這個方法的介紹:This function facilitates the instrumentation of already loaded classes. When classes are initially loaded or when they are redefined, the initial class file bytes can be transformed with the ClassFileTransformer. This function reruns the transformation process (whether or not a transformation has previously occurred). 看到這裡相信大家就都明白了,原來Zorka說到底只是調用JDK的標準API實現了Online Reconfiguration。需要指出的是,這個方法是JDK 1.6之後才增加的,並不適用於JDK 1.5。

關於這些方法從JDK 1.5到8.0的演化,大家可以參看一下連結:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/instrument/Instrumentation.html

http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html

http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html

可以看到,從JDK 1.5中,這個介面只有7個方法,從JDK 6開始穩定在15個方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.