Running a script when starting a server in Google App Engine, in Java

My question is similar to this , but regarding Java instead of Python.

How can I get some Java code to run whenever a new instance of the Google App Engine server starts?

Thanks!

+7
source share
2 answers

in the google engine, your java code runs in a servlet environment. that way you could define listeners to boost your startup code. To do this, you need to implement your startup code in the listener and define the listener in your web.xml:

class listner:

package test; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { // startup code here } @Override public void contextDestroyed(ServletContextEvent sce) { // shutdown code here } } 

web.xml:

 <web-app> <listener> <listener-class>test.MyContextListener</listener-class> </listener> <!-- your other web configuration --> </web-app> 
+15
source

The same answer, put a call in your main function to any setting that you should run, but make sure that you allow checks where you will not run this setting if you already have a server instance.

0
source

All Articles