将元数据添加到 Java RMI

发布于 2024-11-17 13:48:37 字数 367 浏览 0 评论 0原文

我正在尝试创建一个系统,在每次 RMI 调用时,

1)收集有关当前本地系统状态的一些数据(比如说系统时间)

2)将其序列化并透明地将其添加到通过线路发送的数据中调用(即,不更改被调用的存根方法的签名)

3)在另一端反序列化它并采取一些操作(假设将其记录到文件中)

4)当方法返回时反向执行相同的操作

I已经起初尝试使用 AspectJ 来做到这一点,在 java.rmi.server.RemoteRef 的 invoke 方法中添加一个切入点,这将允许我将元数据添加到 params Object 数组中,但我现在发现 AspectJ 已经无法提供建议- 编译后的代码,这很有意义。

那么,这样做的正确方法是什么?

I'm trying to create a system that will, on every RMI call,

1) gather some data about the current local system state (let's say the system time)

2) serialize it and transparently add it to the data sent over the wire with the call (ie, without changing the signature of the stub method being called)

3) deserialize it on the other side and take some action (let's say logging it to a file)

4) do the same thing in reverse when the method returns

I've was trying at first to do this with AspectJ, adding a pointcut at java.rmi.server.RemoteRef's invoke method that would allow me to add the metadata to the params Object array, but I've now discovered that AspectJ can't advise already-compiled code, which makes a lot of sense.

So, what's the right way to do this?

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

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

发布评论

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

评论(2

牛↙奶布丁 2024-11-24 13:48:38

好吧,我不确定我是否从您所说的内容中获得了足够的上下文,但我认为您可以在传递到服务器和从服务器接收的对象的序列化/反序列化时编写元数据。

例如,假设您的服务器正在返回 Jedi 实例。 Jedi 是一个可序列化的类。然后,您可以使用 writeObject() 和 readObject() 方法(如 Java 序列化规范中所述)来写入客户端/服务器端可能需要的任何特殊附加信息。

例如:

public class Jedi {
  ....
        private void writeObject(ObjectOutputStream stream) throws IOException {
        stream.writeObject(new Date());
        stream.defaultWriteObject();
    }

    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        Date sysDate = (Date) stream.readObject();
        System.out.println(sysDate);
        stream.defaultReadObject();
    }
}

唯一的问题是,您将被迫对与服务器交换的每个可序列化对象执行此操作。

Well, I am not sure if I am getting enough context from what you are saying, but I think you could write the metadata upon serialization/deserialization of objects passed to and received from the server.

For instance, let's say you server is returning Jedi instances. And Jedi is a Serializable class. Then you could use the writeObject() and readObject() methods (as explained in the Java Serialization Specification) to write whatever special additional information that you may need at the client/server side.

For instance:

public class Jedi {
  ....
        private void writeObject(ObjectOutputStream stream) throws IOException {
        stream.writeObject(new Date());
        stream.defaultWriteObject();
    }

    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        Date sysDate = (Date) stream.readObject();
        System.out.println(sysDate);
        stream.defaultReadObject();
    }
}

The only problem as that you would be forced to do this for every serializable object you interchange with your server.

夏雨凉 2024-11-24 13:48:38

您还可以研究 Jini 2 项目中的 RMI/JERI。 JERI 代表 Java 可扩展远程调用协议,即您可以通过多种方式对其进行自定义。

You could also investigate RMI/JERI in the Jini 2 project. JERI stands for Java Extensible Remote Invocation protocol, i.e. you can customize it in numerous ways.

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