1

I have the following oql query running on visualvm against a heap dump and would like the creationTime field formatted as a date time field (its stored as Long).

select { id: s.id.toString(), createdAt: new Date(s.creationTime) }
from org.apache.catalina.session.StandardSession s

The above query lists the following output (snipped)

{
 id = 1010827848,
 createdAt = sun.org.mozilla.javascript.internal.NativeDate@66106135
}
...

So clearly its been "converted" into a Date but does not display it a human readable format. Doing a toString() on the date object just results in the field being displayed as Invalid Date.

  1. Is it possible to format the the Long field as a Date field?
  2. The id field's value also is off when queried using VisualVM. When I query the same heap dump using Eclipse Analyser I see the right value (which is BE27C51E8BF185A2FB3AA9164EC0C647). What could be happening to that?
calvinkrishy
  • 3,798
  • 8
  • 30
  • 45

1 Answers1

2
  1. The output shows that you are creating JavaScript Date object. The correct part of you OQL should be: createdAt: new java.util.Date(s.creationTime)
  2. There is a known problem with a field, which is named id. See Retrieve "id" field values via VisualVM OQL query for more details. As a workaround you can use s["wrapped-object"].getValueOfField("id") instead of s.id.toString()

With above changes your query should be:

select { id: s["wrapped-object"].getValueOfField("id"),
createdAt: new java.util.Date(s.creationTime).toString() }
from org.apache.catalina.session.StandardSession s
Tomas Hurka
  • 6,723
  • 29
  • 38
  • Thanks for the pointer for the id field. The Date field turned out to be related to the version of Java I was using. Using Java 8 the field showed up fine irrespective of java.util.Date or the JavaScript Date! Only when using Java 7 the field did not return the Date value. Using java.util.Date with Java 7 resulted in a syntax error being returned. – calvinkrishy May 16 '18 at 17:11
  • Oh, you did not mention that you are still using JDK 7. JDK 7 uses Rhino to execute JavaScript, while JDK 8 uses Nashorn. `new java.util.Date(s.creationTime.longValue())` works for me on JDK 7. – Tomas Hurka May 16 '18 at 19:27
  • `new Date(s.creationTime)` does not work for me on with VisualVM 1.4.1 on JDK 8u172. It returns `createdAt = Invalid Date`. – Tomas Hurka May 16 '18 at 19:39