Monday, April 13, 2009

The JVM Dance: Follow your app from JVM to JVM on GAE/J

When you upload an app to the Google App Engine/J environment HTTP-requests sent to your app will be served by multiple JVM:s (or more specifically, requests will be served by one or more JVM:s).

This means that two consecutive HTTP-requests sent by the same user to the same GAE/J-app may very well be served by two different JVM:s.

The fact that multiple JVM:s are serving your requests might not be apparent since the GAE/J infrastructure makes sure that all objects you store in the session (HttpSession) are available across all JVM:s serving your app (this is done by temporarily storing your session objects in the datastore). On the other hand, you might notice that code within static blocks will be executed once for each JVM loading your code.

To identify which JVM that is serving a request, use the following code:
int currentJvm = Runtime.getRuntime().hashCode();

I've written a small JSP-file that will show you which JVM that is serving the current request, and alert you when two consecutive HTTP-requests are being served by different JVM:s -- let's term this jumping between JVM:s the "GAE/J JVM dance" :-)
jvmdance.jsp: http://pastebin.com/f127200f1

3 comments:

  1. How many differents JVMs did you see ?
    ReplyDelete
  2. Three JVM:s -- see my follow up post for further details.
    ReplyDelete
  3. I've seen the follow up but too late ;-)
    Thanks for these posts.
    ReplyDelete