如何在运行时实现可配置的Java代理

发布于 2025-01-30 03:48:32 字数 1154 浏览 4 评论 0原文

在静态Java代理中,我们预先定义了截获的类,方法和相关建议,代码可能看起来像以下内容,这意味着当Enter and Exit class1.method1()时,MyAdvice中的代码将被执行。

    public static void premain(String arguments, Instrumentation instrumentation) {
        new AgentBuilder.Default()
                .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
                .type("class1")
                .transform((builder, typeDescription, classLoader, module) -> builder
                        .method("method1")
                        .intercept(Advice.to(MyAdvice.class))
                ).installOn(instrumentation);

    Class MyAdvice{

      @Advice.OnMethodEnter
      public static void onBeforeExecute(@Advice.AllArguments Object[] params,
                                     @Advice.This Object agr0) {
      }
      @Advice.OnMethodExit
      public static void onAfterExecute() {
      }
}

但是,在实际应用启动之前,截取的类和方法是预定义的和硬编码的。是否有任何方法或框架可以预先定义一些建议,然后启动应用程序,以及我们在哪里可以通过在运行时通过配置文件动态配置的方法?配置文件可能看起来像这样:

{
   method: "method2",
   class: "class2",
   advice: "MyAdvice2"
}

这意味着我要使用intercept class2.method2()和myadvice2.class。

In a static java agent, we predefine the intercepted class and methods and related advice, the code may look like follows, which means when enter and exit class1.method1(), code in MyAdvice will be executed.

    public static void premain(String arguments, Instrumentation instrumentation) {
        new AgentBuilder.Default()
                .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
                .type("class1")
                .transform((builder, typeDescription, classLoader, module) -> builder
                        .method("method1")
                        .intercept(Advice.to(MyAdvice.class))
                ).installOn(instrumentation);

    Class MyAdvice{

      @Advice.OnMethodEnter
      public static void onBeforeExecute(@Advice.AllArguments Object[] params,
                                     @Advice.This Object agr0) {
      }
      @Advice.OnMethodExit
      public static void onAfterExecute() {
      }
}

However, the intercepted class and methods are predefined and hard-coded before the actual application start. Is there any approach or framework that, predefine some advice, then the application start, and where we what to intercept can be dynamically configured through a config file in runtime? The config file may look like this:

{
   method: "method2",
   class: "class2",
   advice: "MyAdvice2"
}

which means I want intercept class2.method2() and MyAdvice2.class will be used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-02-06 03:48:32

至于霍尔格的评论,他当然是正确的,您需要找到一种方法来发现您的配置已经改变,这与代理无关。您当然可以从某些线程定期扫描文件,也可以注册HTTP端点以提交新数据。

至于字节好友,AgentBuilder返回ResettableClassFileTransFormer您可以将其传递给新的AgentBuilder在其Patchon Method中。这将使字节伙伴计算一个最小的类,这些课程需要在更改后需要更新。它将返回一个新的类文件变压器,该变压器可让您稍后使用新的更新重复该过程。

As for Holger's comment, he is of course right that you need to find a way to discover that your configuration has changed which has nothing to do with an agent. You can of course scan the file regularly from some thread, or register an HTTP endpoint to submit new data.

As for Byte Buddy, the AgentBuilder returns a ResettableClassFileTransformer which you can pass to a new AgentBuilder in its patchOn method. This will make Byte Buddy compute a minimal subset of classes that need to be updated after your changes. It will return a new class file transformer that allows you to repeat the process later with a new update.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文