Best way to communicate with a Java Agent?

177 views
Skip to first unread message

yoann buch

unread,
Nov 11, 2015, 3:59:41 PM11/11/15
to Byte Buddy
It's not really a question specific to Byte Buddy but I feel like some of you may have run into the same problem.

The problem:
  • Application Java Foo is executed with my java agent
  • Application Bar is executed on the same machine
  • How can I send commands to my java agent from Bar?
Options I have in mind:
  • File sharing: application Bar writes to file, java agent polls file
  • Shared database: application Bar and java agent communicate through the database
  • RPC: run a lightweight server (i.e. TCP or HTTP, maybe JMX?) in the java agent, application Bar sends requests to server
  • Messaging: application Bar and java agent communicate through a message broker (i.e. RabbitMQ)
Any other ideas? Something that doesn't impact too much the performance of application Foo?

Thanks,

Yoann

Rafael Winterhalter

unread,
Nov 12, 2015, 4:03:15 AM11/12/15
to yoann buch, Byte Buddy
I would actually recommend you JMX in this case. The advantage with JMX is that the interface is standardized and accessible to many tools and other applications. If this is not powerful enough for your purposes, you can always fall back to using a socket connection. For example:

1. Application A wants to install an agent and opens port 1234 for communication.
2. Application A installs a ready-made Java agent and gives 1234 as an argument.
3. Application B runs the agent and opens a connection to Application A via port 1234.
4. Application B is notified every time there is data incoming from 1234.

Either side can terminate the connection such that the agent in Agent A can dismantle itself once the socket is closed.

With files, message queues or databases, there is always clean up and a thirds component that needs to be installed and accessible. I would avoid these if I were you.

Cheers, Rafael

--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yoann buch

unread,
Nov 12, 2015, 5:16:17 AM11/12/15
to Rafael Winterhalter, Byte Buddy
Thanks Rafael, makes my decision a lot easier!

yoann buch

unread,
Mar 21, 2016, 11:49:09 AM3/21/16
to Rafael Winterhalter, Byte Buddy
So I eventually took the JMX option and later ran into this issue: https://blakboard.wordpress.com/2011/02/22/effects-of-java-rmi-on-jvm-shutdown

I'm starting a JMX server + exporting a remote object from the premain of my java agent.

I can communicate with it which is great, however when the application should normally end, it hangs. This is because there is a non-daemon thread called "RMI Reaper" that is still running. 

"RMI Reaper" dies when all remote objects are unexported. So I should unexport the remote object I exported. The problem is that I don't know when to do it because my java agent is attached to applications I don't know anything about: I don't know when they end.

I know it's not related to Byte Buddy but I just wanted to know if you ever ran into this issue?

If I can't find a solution, I'll have to stop using JMX and instead use a TCP server like Netty.

Thanks,

Yoann 

Rafael Winterhalter

unread,
Mar 21, 2016, 12:38:00 PM3/21/16
to yoann buch, Byte Buddy
Hei Yoann,
I would not know an out of the box solution but one way would be to
regularly deregister the remote object and to check if the VM gets
shut down doing so. You would then have a demon-thread waiting to
re-register the object. But this does of course generate some
overhead. That is an unfortunate implementation detail of JMX I was
not aware of.
Best regards, Rafael

yoann buch

unread,
Mar 21, 2016, 1:07:35 PM3/21/16
to Rafael Winterhalter, Byte Buddy
Thank you Rafael, that's a pretty decent solution until I move away from using JMX.
Reply all
Reply to author
Forward
0 new messages