Question

I'm extremely confused with this sample app that i set up on google app engine. here is what i have:

package com.ha.ha.ha;
import java.io.IOException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class SecondTrialServlet extends HttpServlet {

    private static int sCounter = 0;
    private int mCounter = 0;

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().println("sCounter is : " + (sCounter++));
        resp.getWriter().println("mCounter is : " + (mCounter++));
    }
}

and when i hit my localhost (or deployed version for that matter), on the browser page, and refresh the page 9 times, i see:

sCounter is : 9
mCounter is : 9

Why does this happen? i thought instance variables are supposed to be "cleared away" or something when i do a new request (refreshing the browser page).

how long can i expect the counter to keep on going? like if i set there an keep refreshing the page, will it just keep going up until Integer.MAX_VALUE ?

is this only because i have 1 "instance" up?? if so, how long can i expect this instance to keep running for?

i wanna build a simple application that keeps a few hashmaps around indefinitely. i dont expect the web app to have must traffic at all (maybe like less than 100 requests a day). can i just store everything like i do with the counter variable?

Was it helpful?

Solution

In GooglaAppEngine you cannot rely on the internal state of a servlet.

If you want to have a state, you need to use the provided API from their Cache.

On the mCounter = 9, the same applies: the garbage collector is called when is more appropriate for GAE, and you cannot rely on this.

Moreover, if you are going to scale your instances, no state can be managed in this way, but only through the internal Cache system of GAE.

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top