Developing a Java Application Using the AppEngine Database

This may be a very trivial question, but it's hard for me to find the answer:

Using the Google Plugin for Eclipse, I would like to develop a simple old Java application (and not a web application) that uses AppEngine for cloud storage.

For this, I could, of course, simply create two projects: one containing the AppEngine server, and one containing the Java application.

But I am wondering if it is possible to create one project in Eclipse, which contains both the server and the client code (for example, for a GWT project). To run it for local debugging, I would like Eclipse to run Tomcat to make my servlets available, and then run my Main.java from the project's client directory, as if the project was just a simple Java application. Is this the "Launch and deploy from this directory" checkbox in the settings of "Google" β†’ "Web Application"? If so, how to use it?

0
source share
2 answers

I found one way to do this, but it's a little 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 record class:

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

Finally, create the appropriate launch configuration:

  • Just click "Run As" β†’ "Web Application". To create a default startup configuration.
  • In the created launch configuration, in the "Home" -tab section, select your own recording class as "Main class" instead of the standard "com.google.appengine.tools.development.DevAppServerMain".

Now, if you run this launch configuration, it will first open the AppEngine server, and then continue with the rest of the main(...) method in the input class. Since the server thread is marked as a daemon thread, as soon as the second code in main(...) is executed, the application terminates, also closing the server.

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

In addition, there may be a more elegant way to check if the AppEngine server is working, in addition to pinging it using a URL connection, as I said above.

Note. The AppEngine Dev server registers its own URLStreamHandlerFactory to automatically map Http(s)URLConnections to the AppEngine URL-fetch framework. This means that you get errors complaining about the lack of ability to receive URL data if you use HttpURLConnections in your client code. Fortunately, this can be fixed in two ways, as described here: Getting the default Java link http httpStreamHandler .

+1
source

If you definitely want to use appengine, then in the end you will create two projects: one on appengine and one more stand-alone (without servlets). In this case, you can take a look at the appengine Remote API

0
source

Source: https://habr.com/ru/post/1492454/


All Articles