创建代理程序
public static void agentmain(String agentArgs, Instrumentation inst){
inst.addTransformer(new SimClassFileTransformer(),true);
try {
inst.retransformClasses(Class.forName("sim.com.AgentTargetSample"));
}
catch (Exception e) {
e.printStackTrace();
}
}
public class SimClassFileTransformer implements ClassFileTransformer {
@Override
public byte[] transform(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
System.out.println(String.format("Agentmain Process by SimClassFileTransformer,target class = %s",className));
return classfileBuffer;
}
}
配置maven package
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifestEntries>
<!--<Premain-Class>com.sim.PremainAgent</Premain-Class>-->
<Agent-Class>sim.com.AgentmainAgent</Agent-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
配置被代理程序
public class AgentTargetSample {
public void sayHello(String name) {
System.out.println(String.format("%s 我是被代理程序 通过agent模式代理!", name));
}
public static void main(String[] args) throws InterruptedException {
AgentTargetSample targetSample = new AgentTargetSample();
while (true){
Thread.sleep(1000);
targetSample.sayHello(Thread.currentThread().getName());
}
}
}
配置Attach激活程序
public static void main(String[] args) throws Exception {
/**
* 获取所有VM实例
*/
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
vmds.stream().filter(x-> x.displayName().endsWith("AgentTargetSample")).forEach(x-> {
try {
/**
* 目标VM
*/
VirtualMachine virtualMachine = VirtualMachine.attach(x.id());
/**
* 目标VM加载Agent
* 参数1 代理Jar路径
* 参数2 命令参数
*/
virtualMachine.loadAgent("/Users/mac/Documents/Gitee/spring-design-pattern-demo/spring-Instrumentation-demo/java-agentmain-Instrumentation/target/java-agentmain-Instrumentation-1.0-SNAPSHOT.jar","arg1");
virtualMachine.detach();
} catch (Exception e) {
e.printStackTrace();
}
});
}
执行顺序
1 启动被代理程序
2 启动attach激活程序