0

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?

0x56794E
  • 20,883
  • 13
  • 42
  • 58

1 Answers1

2

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.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Totally forgot!! I'll try that. Thanks!! – 0x56794E Sep 18 '13 at 21:51
  • 1
    By the debugger, I meant the debugger for the web app. NOT for the jmx. I'd be even more freaked out if the value from debugger for the web app matches the jmx and my web app shows otherwise. – 0x56794E Sep 18 '13 at 21:54
  • too bad. it doesn't work for me. :( I make `foo` `volatile` and still, the value in the jmx is still different from that in my web app – 0x56794E Sep 18 '13 at 22:39
  • Any chance there are 2 instances of this MyService getting published @abcXYZ? How about some printf (or logger) debugging here and be sure to print _which_ MyService instance you are doing. Something like `System.out.println("getting foo in " + System.identityHashCode(this) + " = " + foo);` – Gray Sep 18 '13 at 22:45
  • Yeah I checked that. Print out the value of toString() on both instance. Same memory address. – 0x56794E Sep 18 '13 at 22:46
  • Wow. I'm about out of ideas. Is there a chance that something in the view is getting and then setting the value back or something? Can you log each of the calls to the getter _and_ the setter @abcXYZ? – Gray Sep 18 '13 at 22:49
  • I'm absolutely positively sure that no, there's not any thing that could possibly change that value. :( – 0x56794E Sep 18 '13 at 23:40
  • Shoot!! Now the 2 instances seem to be different. `toString()` on the to instances gave me 2 different values: MyService@31ea681 and MyService@267279d3 – 0x56794E Sep 19 '13 at 00:38
  • So looks like the 2 instances are different. But I thought it's supposed to be the same. – 0x56794E Sep 19 '13 at 01:46
  • 1
    Yeah I suspected something like that @abcXYZ. Btw, that's not a "memory address" nor does it have to be unique. Just for the record. It's the instance hashCode is usually unique. :-) – Gray Sep 19 '13 at 15:22
  • yeah i too much C stuff in my head. Anyways, so that really what happened. We had multiple instances running. – 0x56794E Sep 19 '13 at 18:04