简体   繁体   中英

Instrumentate a distant java application via javassist+javaagent+jmx

Actually i want to developpe a java application witch should instrumentate another java application witch i don't have its source code..

I tried to create an agent and attach it to the jvm.. then i created an mbean and tried to connect to it.. it works when i try to instrumentate a class in my project.. but i don't know how could i instrument a distant application with my application..

here is my code: https://github.com/ammouna8ammouna/Monitoring.git

i am really new at the instrumentation world and i really need help.

If you can get the processID of the VM that you are targeting you can attach your agent using

com.sun.tools.attach.VirtualMachine

For example if you have pid, the path of your JAR target and the loader that handle the JAR you can try something like this:

private static void attach(String pid, String jarPath,
        ClassLoader toolLoader) throws Exception {
    Class<?> attacherLib = toolLoader.loadClass("com.sun.tools.attach.VirtualMachine");
    Class<?> string = toolLoader.loadClass("java.lang.String");

    Method attach = attacherLib.getMethod("attach", string);

    Object instance = attach.invoke(null, pid);

    Method loadAgent = attacherLib.getMethod("loadAgent", string, string);
    loadAgent.invoke(instance, jarFilePath, "");

    Method detach = attacherLib.getMethod("detach");
    detach.invoke(instance);
}

Let me know if it's clear or you need other help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM