ptd*_*dev 8 java google-app-engine
我的问题类似于这个问题,但是关于Java而不是Python.
每当Google App Engine服务器的新实例启动时,如何强制运行某些Java代码?
谢谢!
hap*_*eal 15
在谷歌应用引擎中,您的Java代码在servlet环境中执行.因此,您可以定义侦听器以提升您的启动代码.为此,您需要在侦听器中实现启动代码并在web.xml中定义侦听器:
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
}
}
Run Code Online (Sandbox Code Playgroud)
web.xml中:
<web-app>
<listener>
<listener-class>test.MyContextListener</listener-class>
</listener>
<!-- your other web configuration -->
</web-app>
Run Code Online (Sandbox Code Playgroud)