Search code examples
javarmi

Java RMI server: method to be called on events


I was wondering whether there are some kinds of callback mechanisms in the Java RMI server API. What I want to achieve is that I get a call on events like:

  • Opened connection
  • Method was invoked
  • Closed connetion
  • ...

The RMI stuff itself runs fine, and I also know about the logging possibilities of RMI. But I'm interested in gaining more insight on how the server performs, create some statistics etc.

One possibility would be of course to add a method call to each of my remote object's methods, but that's ugly since my statistics part is independent of the actual implementation of the provided functionality.


Solution

  • Callbacks on method invocation is fairly straightfoward, just an application of java.lang.reflect.Proxy.

    However RMI goes to a lot of trouble to insulate you from connection and disconnection events. You won't be able to get to those without quite a lot of trouble, and because of client-side connection pooling the events wouldn't reveal quite as much as you might think anyway. You would have to implement an RMIClientSocketFactory that provided callbacks on socket creation, and that returned a wrapped socket that provided a callback on close(), and as the RMIClientSocketFactory is downloaded from the server you would also have to devise a way for all this code to become accessible to your client so it could register its callbacks. The latter alone is non-trivial.