Why is App Engine calling contextInitialized repeatedly?

73 views
Skip to first unread message

Joshua Fox

unread,
Jun 7, 2017, 2:37:11 AM6/7/17
to google-a...@googlegroups.com
In my  contextInitialized(ServletContextEvent) we  set some global variables --- static fields. Yet surprisingly, sometimes these are already set when contextInitialized is called.

I thought that contextInitialized is called when, and only when, the JVM is initialized.

Is it possible that GAE Standard Environment is initializing already initialized instances? Does GAE somehow keep old instances around and reinitialize them? 

(I note some documentation on "resident instances", but I am not sure if that is connected.)


(The config, as seen in the Cloud Console, includes

  automatic_scaling:
    min_idle_instances: 1
    max_idle_instances: 1
)

George (Cloud Platform Support)

unread,
Jun 7, 2017, 11:01:04 AM6/7/17
to Google App Engine
Hello Joshua, 

This is potentially a quite interesting issue. Which global variables or static fields do you intend to set? How were these sometimes already set? Some examples and relevant detail would come handy. In fact, the contextInitialized method receives notification that the web application initialization process is starting; this is the general description.

Joshua Fox

unread,
Jun 7, 2017, 11:19:13 AM6/7/17
to google-a...@googlegroups.com
On Wed, Jun 7, 2017 at 6:01 PM, 'George (Cloud Platform Support)' via Google App Engine <google-a...@googlegroups.com> wrote:
Hello Joshua, 

This is potentially a quite interesting issue. Which global variables or static fields do you intend to set?
Just some static variables which are set from Datastore to reflect some global project-level state. Actually, it's a Singleton.
 
How were these sometimes already set?

Some code detects if the variable is nonnull and balks,and prints a log.

Some examples and relevant detail would come handy. In fact, the contextInitialized method receives notification that the web application initialization process is starting; this is the general description.

Right -- but  is contextInitialized called only once per JVM initialization?  Or do  "resident instances" get "put to sleep" and then awakened by calling contextInitialized?

What detail can I provide? The following should be a good summary:  In  contextInitialized , we set a static field of the class to nonnull. At the point right before it is set, it should always be null.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine+unsubscribe@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/3d76f372-6d20-48a8-bf3f-22b101a08f98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
JOSHUA FOX
Principal Software Architect | Freightos


 (Israel): +972-545691165 |  (US):  +1-3123400953 | Skypejoshuafox.freightos
Smoother shipping with the world's online freight marketplace.

Joshua Fox

unread,
Jun 8, 2017, 2:54:33 AM6/8/17
to google-a...@googlegroups.com
The concept "Idle  Resident instance" might be relevant here.

 Are instances switched from active to idle to active -- not actually stopping the JVM on switch to idle, and invoking contextInitialized on switch to  active?


On Wed, Jun 7, 2017 at 6:17 PM, Joshua Fox <jos...@freightos.com> wrote:

George Suceveanu

unread,
Jun 8, 2017, 9:20:28 AM6/8/17
to google-a...@googlegroups.com
There are no "idle" instances, and one of the reasons is precisely what we speak of: possible initialization issues. Instances are either wholly new and spun up to secure scalability, or totally destroyed, in other words either alive or dead, nothing in-between. 

You received this message because you are subscribed to a topic in the Google Groups "Google App Engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-appengine/Zy6xZioG6UQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-appengine+unsubscribe@googlegroups.com.

To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
Reply all
Reply to author
Forward
0 new messages