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
执行结果: