服务器上的运行脚本在Google App Engine上以Java开始

时间:2011-06-11 01:38:47

标签: java google-app-engine

我的问题类似于this one,但是关于Java而不是Python。

如果Google App Engine服务器的新实例启动,我如何强制运行某些Java代码?

谢谢!

2 个答案:

答案 0 :(得分:15)

在谷歌应用引擎中,您的Java代码在servlet环境中执行。因此,您可以定义侦听器以提升您的启动代码。为此,您需要在侦听器中实现启动代码并在web.xml中定义侦听器:

列表类:

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>

答案 1 :(得分:0)

同样的答案,在您的主函数中调用您需要运行的任何设置,但是如果您已经运行了服务器实例,请确保允许检查您不会运行该设置的位置。