Calling Third‑Party Java Methods with Byteman in Chaos Mesh
This article demonstrates how to use Byteman’s DO execution module on the Chaos Mesh platform to invoke static or instance methods of external Java classes without modifying the original code, covering reflection, ClassLoader tricks, and a complete BTM rule example.
Byteman is widely used for fault injection, and its DO execution module can also execute arbitrary Java methods, which is useful when working with the Chaos Mesh platform across multiple nodes.
Problem Statement
After a service starts, we may need to call a static method of a class to perform data initialization or schedule periodic tasks. Modifying the core code is undesirable, so we look for a way to trigger method execution externally.
Using Chaos Mesh as a Global Injection Point
Chaos Mesh’s TriggerThread runs in an asynchronous thread that repeatedly calls triggerFunc(). By treating this method as a global injection point, we can schedule code execution without manual intervention.
package org.chaos_mesh.chaos_agent;
public class TriggerThread extends Thread {
public void run() { loop(); }
public static void loop() {
while (true) {
try { Thread.sleep(5000); } catch (Exception e) { System.out.println(e.getMessage()); }
triggerFunc();
}
}
public static void triggerFunc() {
// System.out.println("chaos agent trigger function");
}
}Calling a Third‑Party Class Method
We create a simple demo class with a static method to be invoked:
package com.funtest.temp;
public class BytemanDemo {
public static void main(String[] args) {
while (true) {
try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); }
print(234);
}
}
static int print(int i) {
int a = i;
System.out.println("Hello Word from Byteman ,By FunTester !!!");
return a * a;
}
public static void pp() {
System.out.println("33333333");
}
}Direct reflection such as
Class.forName("com.funtest.temp.BytemanDemo").getDeclaredMethod("pp").invoke(null);works in plain Java but fails inside a Byteman .btm file due to compatibility issues with Byteman’s Java‑CUP parser.
Workaround with ClassLoader
Loading the class via the thread’s context ClassLoader avoids the parser conflict:
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
contextClassLoader.loadClass("com.funtest.temp.BytemanDemo")
.getDeclaredMethod("pp")
.invoke(null);Even this approach initially produced errors, so the final working BTM rule binds the class using ClassLoader.getSystemClassLoader() and invokes the method:
RULE testent
CLASS com.funtest.temp.BytemanDemo
METHOD print
BIND buffer = ClassLoader.getSystemClassLoader().loadClass("com.funtest.temp.BytemanDemo");
m = buffer.getDeclaredMethod("pp", new Class[0]);
AT ENTRY
IF TRUE
DO System.out.println("Hello Word,FunTester");
m.invoke(null, null);
ENDRULERunning the program prints the expected messages, confirming that Byteman can be used not only for fault injection but also for executing arbitrary logic such as invoking third‑party methods.
Considerations and Optimizations
In production, repeatedly loading classes can incur overhead; a dedicated helper class that encapsulates the reflection logic would be more efficient, especially in Spring Boot applications where class loading can be optimized.
Overall, Byteman’s extensibility allows developers to automate tasks and perform sophisticated runtime manipulations beyond traditional chaos testing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
