我找到了一种方法,但是有点俗气。
首先,将以下帮助程序类添加到项目中:
// other importsimport com.google.appengine.tools.development.DevAppServerMain;public class DevServer { public static void launch(final String[] args) { Logger logger = Logger.getLogger(""); logger.info("Launching AppEngine server..."); Thread server = new Thread() { @Override public void run() { try { DevAppServerMain.main(args); // run DevAppServer } catch (Exception e) { e.printStackTrace(); } } }; server.setDaemon(true); // shut down server when rest of app completes server.start(); // run server in separate thread URLConnection cxn; try { cxn = new URL("http://localhost:8888").openConnection(); } catch (IOException e) { return; } // should never happen boolean running = false; while (!running) { // maybe add timeout in case server fails to load try { cxn.connect(); // try to connect to server running = true; // Maybe limit rate with a Thread.sleep(...) here } catch (Exception e) {} } logger.info("Server running."); }}然后,将以下行添加到条目类:
public static void main(String[] args) { DevServer.launch(args); // launch AppEngine Dev Server (blocks until ready) // Do everything else}最后,创建适当的运行配置:
- 只需单击“运行方式”->“ Web应用程序”。创建默认的运行配置。
- 在创建的运行配置中,在“主”选项卡下,选择您自己的条目类作为“主类”,而不是默认的“ com.google.appengine.tools.development.DevAppServerMain”。
现在,如果您启动此运行配置,它将首先启动AppEngine服务器,然后继续
main(...)在入口类中继续其余方法。由于服务器线程被标记为守护线程,因此其他代码
main(...)完成后,应用程序将正常退出,并关闭服务器。
不知道这是否是最优雅的解决方案,但它是否有效。如果其他人有没有
DevServer帮助类的方法来实现此目的,请发布它!
此外,除了像上面我一样使用URL连接ping它之外,可能还有一种更优雅的方法来检查AppEngine服务器是否正在运行。
注意:
AppEngine开发服务器会自行注册
URLStreamHandlerFactory以自动映射
Http(s)URLConnections到AppEngine的URL提取基础结构。这意味着如果您随后
HttpURLConnections在客户端代码中使用url-
fetch功能,则您会抱怨错误。幸运的是,可以通过以下两种方式解决此问题:引用Java的默认httpURLStreamHandler。



