开发使用AppEngine数据库的Java应用程序 [英] Developing a Java Application that uses an AppEngine database

查看:117
本文介绍了开发使用AppEngine数据库的Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常微不足道的问题,但我无法找到答案:



使用Google Plugin for Eclipse,我想开发一个普通旧的Java应用程序(不是web应用程序),它使用AppEngine进行云存储。



为此,我可以简单地创建两个项目,一个包含AppEngine服务器和一个包含Java应用程序的服务器。



但是我想知道是否可以在Eclipse中设置包含服务器和客户端代码的单个项目(如GWT项目)。为了执行本地调试,我想让Eclipse启动Tomcat来使我的servlet可用,然后从项目的客户端目录启动我的Main.java,就好像项目只是一个简单的Java应用程序一样。这是从这个目录启动和部署复选框是用于Google - >Web应用程序设置?如果是这样,我该如何使用它?

解决方案

我发现了一种方法,但它有点俗气。 p>

首先,将下面的助手类添加到项目中:

  / / 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(启动AppEngine服务器...);
Thread server = new Thread(){
@Override
public void run(){
try {
DevAppServerMain.main(args); //运行DevAppServer
} catch(Exception e){e.printStackTrace(); }
}
};
server.setDaemon(true); //当应用程序的其余部分完成时关闭服务器
server.start(); //在单独的线程中运行服务器
URLConnection cxn;
尝试{
cxn = new URL(http:// localhost:8888).openConnection();
} catch(IOException e){return; } //应该永远不会发生
布尔运行= false;
while(!running){//可能会在服务器加载失败的情况下添加超时
try {
cxn.connect(); //尝试连接到服务器
running = true;
//使用Thread.sleep(...)在这里限制速度
} catch(Exception e){}
}
logger.info(服务器正在运行。 );


然后,将以下行添加到条目类中:

  public static void main(String [] args){
DevServer.launch(args); //启动AppEngine Dev Server(block until ready)
//其他所有
}

最后,创建相应的运行配置:


  • 只需点击运行方式 - >Web应用程序。创建一个默认的运行配置。

  • 在创建的运行配置中,在主选项卡下选择您自己的输入类作为主类,而不是默认的com.google .appengine.tools.development.DevAppServerMain。



现在,如果启动此运行配置,它将首先启动AppEngine服务器然后继续输入类中的 main(...)方法的其余部分。由于服务器线程被标记为守护线程,一旦 main(...)中的其他代码完成,应用程序正常退出,同时关闭服务器。 / p>

不确定这是否是最优雅的解决方案,但是可行。如果其他人有办法在没有 DevServer helper-class的情况下实现这个功能,请张贴它!



另外,可能有一种更优雅的方式来检查AppEngine服务器是否正在运行,而不是像上面那样使用URL连接来ping它。



注意: AppEngine Dev Server 注册自己的 URLStreamHandlerFactory 自动映射 Http(s)URLConnections 放到AppEngine的网址提取基础架构。这意味着如果您在客户端代码中使用 HttpURLConnections ,则会出现抱怨缺少url抓取功能的错误。幸运的是,这可以通过两种方式解决,如下所述:获取对Java的默认http(s)URLStreamHandler的引用


This might be a very trivial question, but I'm having trouble finding an answer:

Using the Google Plugin for Eclipse, I would like to develop a plain old Java application (not a web-app), 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'm wondering whether it is possible to set up a single project in Eclipse that contains both the server and the client code (like for a GWT project). To execute it for local debugging, I would then want Eclipse to launch Tomcat to make my servlets available and then launch my Main.java from the client directory of the project as if the project was just a simple Java application. Is this what the "Launch and deploy from this directory" checkbox is for in the "Google" -> "Web Application" settings? If so, how do I use it?

解决方案

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.

这篇关于开发使用AppEngine数据库的Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆