开发者

Adding Metadata to Java RMI

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, w开发者_运维技巧ithout 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜