jvm sandbox實現字節碼增強

1.安裝jvm-sandbox
2.引入依賴

<parent>
    <groupId>com.alibaba.jvm.sandbox</groupId>
    <artifactId>sandbox-module-starter</artifactId>
    <version>1.2.0</version>
</parent>

3.編寫腳本

@MetaInfServices( Module.class )
@Information( id = "run-function-agent" )// 模塊名
public class BrokenClockTinkerModule implements Module{

	@Resource
	private ModuleEventWatcher moduleEventWatcher;

	@Command( "runFunctionAgent" )// 模塊命令名
	public void runFunctionAgent(){

		new EventWatchBuilder( moduleEventWatcher ).onClass( "com.bj58.sandbox.Base" )// 想要對 Base 這個類進行切面
				.onBehavior( "run" )
				.onWatch( new AdviceListener(){

					@Override
					protected void before( Advice advice ) throws Throwable{

						System.out.println( "run() start" );
						super.before( advice );
					}

					@Override
					protected void afterReturning( Advice advice ) throws Throwable{
						System.out.println( "run() end" );
						super.afterReturning( advice );
					}
				} );

	}

}
  1. 編譯打包
mvn clean package
  1. 將jar包複製到用戶目錄
cp target/sandbox-base-jar-with-dependencies.jar ~/.sandbox-module/
  1. 進入sandbox安裝目錄,執行命令
#將沙箱掛載到進程號爲 15631 的 JVM 進程上,#運行指定模塊, 模塊功能生效 
./sandbox.sh -p 15631 -d 'run-function-agent/runFunctionAgent'
  1. 卸載沙箱
./sandbox.sh -p 15631 -S  

執行結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章