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