Developing a Java Application that uses an AppEngine database

我的未来我决定 提交于 2019-12-02 07:57:00
Markus A.

I found one way to do it, but it's a bit cheesy.

First, add the following helper-class to the project:

// other imports
import 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.");
    }
}

Then, add the following line to the entry class:

public static void main(String[] args) {
    DevServer.launch(args);  // launch AppEngine Dev Server (blocks until ready)
    // Do everything else
}

Finally, create the appropriate Run Configuration:

  • Simply click "Run As" -> "Web Application". To create a default Run Configuration.
  • In the created Run Configuration, under the "Main"-tab select your own entry class as the "Main class" instead of the default "com.google.appengine.tools.development.DevAppServerMain".

Now, if you launch this Run Configuration, it will first bring up the AppEngine server and then continue with the rest of the main(...) method in the entry class. Since the server thread is marked as a daemon thread, once the other code in main(...) completes, the application quits normally, shutting down the server as well.

Not sure if this is the most elegant solution, but it works. If someone else has a way to achieve this without the DevServer helper-class, please do post it!

Also, there might be a more elegant way to check whether the AppEngine server is running, other than pinging it with a URL connection as I did above.

Note: The AppEngine Dev Server registers its own URLStreamHandlerFactory to automatically map Http(s)URLConnections onto AppEngine's URL-fetch infrastructure. This means that you get errors complaining about missing url-fetch capabilities if you then use HttpURLConnections in your client code. Luckily, this can be fixed in two way as described here: Getting a reference to Java's default http(s) URLStreamHandler.

If you definitely want to use appengine, then you will end up creating two projects, one on appengine and another a standalone (no servlets). In this case you can take a look at appengine Remote API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!