简体   繁体   English

在 Google App Engine 上启动服务器上的运行脚本,在 Java

[英]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.我的问题与类似,但关于 Java 而不是 Python。

How can I force some Java code to run whenever a new instance of a Google App Engine server starts?每当 Google App Engine 服务器的新实例启动时,如何强制运行一些 Java 代码?

Thanks!谢谢!

in google app engine, your java code is executed within the servlet environment.在谷歌应用引擎中,您的 java 代码在 servlet 环境中执行。 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:为此,您需要在监听器中实现启动代码并在 web.xml 中定义监听器:

listner class:监听器 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.xml:

<web-app>
    <listener>
        <listener-class>test.MyContextListener</listener-class>
    </listener>

<!-- your other web configuration -->

</web-app>

Same answer, put a call in your main function to whatever setup you need to run, but make sure you allow checks where you won't run that setup if you already have a server instance running.同样的答案,在你的主 function 中调用你需要运行的任何设置,但如果你已经有一个服务器实例正在运行,请确保你允许检查你不会运行该设置的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM