Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running script on server start on Google App Engine, in Java

My question is similar to this one, but with regard to Java instead of Python.

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

Thanks!

like image 418
ptdev Avatar asked Jun 11 '11 01:06

ptdev


People also ask

Which programming environment is used for Google App Engine?

js, Java, Ruby, C#, Go, Python, or PHP. A fully managed environment lets you focus on code while App Engine manages infrastructure concerns. Use Cloud Monitoring and Cloud Logging to monitor the health and performance of your app and Cloud Debugger and Error Reporting to diagnose and fix bugs quickly.

How do I run the Google App Engine in eclipse?

Run eclipse. To download and install the Cloud Tools for Eclipse plugin, select Help > Eclipse Marketplace... and search for Google Cloud. After installation, restart Eclipse when prompted to do so. In Eclipse, select the File menu > New > Google App Engine Standard Java Project.


1 Answers

in google app engine, your java code is executed within the servlet environment. thus, you could define listeners to boostrap your startup code. to do this, you need to implement your startup code in the listener and define the listener in your web.xml:

listner class:

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>
like image 80
happymeal Avatar answered Sep 28 '22 08:09

happymeal