开发

把它当作特殊的 JAR 包进行开发即可, 新建 maven 项目并在 resources/META-INF/ 下新建 MANIFEST.MF 文件,指明必要信息 Premain-Class 和 Can-Redefine-Classes

Manifest-Version: 1.0
Premain-Class: com.zeng.jvm.monitoring.Agent
Can-Redefine-Classes: true

在 pom 指明打包方式和 manifestFile 位置

<packaging>jar</packaging>
<build>
    <finalName>my-agent</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifestFile>
                        src/main/resources/META-INF/MANIFEST.MF
                    </manifestFile>
                    <manifest>
                        <addDefaultImplementationEntries/>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

最后,使用 mvn 进行打包,获得 jar 包

使用 | 调试

使用

在 IDEA VM Options 中添加参数

-javaagent:my-agent-1.0.0-SNAPSHOT.jar=threshold=1000
# 或直接启动
java -javaagent:my-agent-1.0.0-SNAPSHOT.jar=threshold=1000,debug ...rest of command

通过重复使用javaagent命令,能够添加多个 agent。

调试

在项目 pom 中添加依赖即可当作普通包一样进行调试

<dependency>
    <groupId>com.lls.it</groupId>
    <artifactId>my-agent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

Reference

  1. https://github.com/toptal/jvm-monitoring-agent
  2. https://blog.csdn.net/qq_26761587/article/details/78817745