日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

动态Java代码注入

發布時間:2023/12/3 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动态Java代码注入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在本文中,我們將研究如何將Java代碼動態加載到正在運行的jvm中。 該代碼可能是全新的,或者我們可能想更改程序中某些現有代碼的功能。

(在開始之前,您可能想知道為什么到底有人會這樣做。顯而易見的示例是規則引擎之類的東西。規則引擎希望為用戶提供添加或更改規則的能力,而不必重新啟動規則。您可以通過將DSL腳本作為規則注入,由規則引擎調用此方法,這種方法的真正問題是必須對DSL腳本進行解釋,使其運行起來非常緩慢。然后可以像程序中的任何其他代碼一樣編譯和運行該程序,效率將提高幾個數量級。

在《紀事報》中,我們在新的微秒微服務/算法容器的核心中使用了這個想法。

我們將要使用的庫是Chronicle開源庫Java-Runtime-Compiler 。

從下面的代碼中您將看到,該庫的使用極其簡單-實際上,它實際上只需要幾行。 創建一個CachedCompiler,然后調用loadFromJava。 (有關實際最簡單的用例,請參見此處的文檔。)

下面列出的程序執行以下操作:

  • 創建一個線程,該線程每秒調用一次Strategy。 該戰略的投入為10和20。
  • 加載將兩個數字相加的策略
  • 等待3秒
  • 加載從另一個數中減去一個數的策略
  • 這是完整的代碼清單:

    package test;import net.openhft.compiler.CachedCompiler;/*** Loads the addingStrategy and then after 3s replaces it with the * subtractingStrategy.*/ public class DynamicJavaClassLoading {private final static String className = "test.MyClass";private final static String addingStrategy = "package test;\n" +"import test.DynamicJavaClassLoading.Strategy;\n" +"public class MyClass implements Strategy {\n" +" public int compute(int a, int b) {\n" +" return a+b;\n" +" }\n" +"}\n";private final static String subtractingStrategy = "package test;\n" +"import test.DynamicJavaClassLoading.Strategy;\n" +"public class MyClass implements Strategy {\n" +" public int compute(int a, int b) {\n" +" return a-b;\n" +" }\n" +"}\n";public static void main(String[] args) throws Exception {StrategyProxy strategy = new StrategyProxy();//Thread calling the strategy once a secondThread t = new Thread(() -> {while (true) {System.out.println(strategy.compute(10,20));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();{ClassLoader cl = new ClassLoader() {};CachedCompiler cc = new CachedCompiler(null, null);Class aClass = cc.loadFromJava(cl, className, addingStrategy);Strategy runner = (Strategy) aClass.newInstance();strategy.setStratgey(runner);}Thread.sleep(3000);{ClassLoader cl = new ClassLoader() {};CachedCompiler cc = new CachedCompiler(null, null);Class aClass = cc.loadFromJava(cl, className, subtractingStrategy);Strategy runner = (Strategy) aClass.newInstance();strategy.setStratgey(runner);}}public interface Strategy{int compute(int a, int b);}public static class StrategyProxy implements Strategy{private volatile Strategy underlying;public void setStratgey(Strategy underlying){this.underlying = underlying;}public int compute(int a, int b){Strategy underlying = this.underlying;return underlying == null ? Integer.MIN_VALUE : underlying.compute(a, b);}} }

    這是輸出(藍色注釋):

    The strategy has not been loaded yet. underlying in the StrategyProxy is null so Integer.MIN_VALUE is returned -2 1 4 7 4 8 3 6 4 8 The adding strategy has been loaded 10+20=30 30 30 30 After 3s the subtracting strategy is loaded. It replaces the adding strategy. 10-20=-10 -10 -10 -10 -10-10

    請注意,在每次加載策略時,在代碼中我們都創建了一個新的ClassLoader和一個CachedCompiler。 這樣做的原因是,ClassLoader一次只能加載一個特定類的一個實例。

    如果僅使用該庫來加載新代碼,則可以這樣做,而無需創建ClassLoader(即使用默認的ClassLoader)和CachedCompiler。

    Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);

    翻譯自: https://www.javacodegeeks.com/2015/10/dynamic-java-code-injection.html

    總結

    以上是生活随笔為你收集整理的动态Java代码注入的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。