I have a service that's can be accessed via jconsole/Visual VM.
@ManagedResource
public class MyService
{
private String foo;
@ManagedAttribute
public void setFoo(String newVal) { this.foo = newVal; }
@ManagedAttribute
public String getFoo() { return this.foo; }
//some other things here that access foo
}
But looks like the value of foo
received by the web app controller doesn't always match the value I get when I click getFoo() in either jconsle or visual VM.
Also, the debugger shows me that the value my controller get is not what I see in jconsole.
Any idea?
But looks like the value of foo received by the web app controller doesn't always match the value I get when I click getFoo() in either jconsle or visual VM. Also, the debugger shows me that the value my controller get is not what I see in jconsole.
I'm not sure but I suspect that the value foo
is not being properly memory synchronized between the various threads. You should make foo
be volatile
if it being updated by a different thread than the one displaying the value -- or put up with JMX being out of date.
private volatile String foo;
Certainly the JMX request is being made from a different thread that your web-app would be handled by. I would have thought, however, that the debugger would not have had the problem.
Edit:
After some back and forth I asked if it was possible that 2 instances of the MyService
class were getting created/used. @abcXYZ adding something like System.out.println("getting foo in " + System.identityHashCode(this) + " = " + foo);
to the getter and setter methods which showed that there were actually 2 different instances of the class for some reason.
So the JMX thread was looking at one while the web app was using the other. Ouch.