Google App Engine for Java を触ってみた

移転しました。

以下ページを見ながら、leopardEclipseプラグイン入れて触ってみた。
このページ見れば、簡単にインスコできる。
http://code.google.com/intl/ja/appengine/docs/java/tools/eclipse.html
GAEのアプリケーションを既に作成しているものとして話を進める。

作成方法は【このあたり】を参考に。

アクティベイト

【ここ】にアクセスして「sign me up!」ボタンを押す。
すると、後で「Welcome to an early look at Google App Engine for Java!」という件名のメールが届く。
このメールが届くと、Google App Engine for Javaの環境が使えるようになる。

さっそくプラグイン使ってみる

Eclipseをインスコ

【Ecliseのページ】からMac用のEclipse 3.4 (Ganymede)をダウンロードして、インスコ。

Google Plugin for Eclipse をインスコ

以下文章に従って、GWT SDKとか全てインスコした。

To install the plugin, using Eclipse 3.4 (Ganymede):

   1. Select the Help menu > Software Updates... The "Software Updates and Add-ons" window opens.
   2. Select the Available Software tab. Click the Add Site... button. The "Add Site" window opens. For "Location," enter the install location for the Eclipse 3.4 version of the plugin:

      http://dl.google.com/eclipse/plugin/3.4

      Click OK. The dialog closes, and the new location is added to the list of available software.
   3. Click the triangle next to the new install location, then click the triangle next to "Google" to display installation options. Check the boxes next to "Google Plugin for Eclipse 3.4" and "Google App Engine Java SDK". You can also select the "Google Web Toolkit SDK" if you'd like to use Google Web Toolkit with your apps. Click the Install... button. Follow the prompts to accept the terms of service and install the plugin.
   4. When the installation is complete, Eclipse prompts you to restart. Click Yes. Eclipse restarts. The plugin is installed.
プロジェクト作成

以下に従ってプロジェクト作成。プロジェクト名、パッケージ名はページに従い以下のようにし、

プロジェクト名:Guestbook
パッケージ名:guestbook

GWTにあまり良い思いでがないので、チェックを外した後、ページとおり進めてみた。

To create a new App Engine project:
   1. Select the File menu > New > Web Application Project (If you do not see this menu option, select the Window menu > Reset Perspective..., click OK, then try the File menu again.) Alternatively, click the New Web Application Project button in the toolbar: The New Web Application Project button.
   2. The "Create a Web Application Project" wizard opens. For "Project name," enter a name for your project, such as Guestbook for the project described in the Getting Started Guide. For "Package," enter an appropriate package name, such as guestbook.
   3. If you're not using Google Web Toolkit, uncheck "Use Google Web Toolkit." Verify that "Use Google App Engine" is checked.
   4. If you installed the App Engine SDK using Software Update, the plugin is already configured to use the SDKs that were installed. If you would like to use a separate installation of the App Engine SDK, click Configure SDKs..., and follow the prompts to add a configuration with your SDK's appengine-java-sdk/ directory.
   5. Click Finish to create the project.
プロジェクト実行

さっそくプロジェクト実行してみる。
上部Runメニューから Debug As - Web Application を選択。
すると以下メッセージが出力される。

2009-04-09 05:04:57.961 java[2368:80f] [Java CocoaComponent compatibility mode]: Enabled
2009-04-09 05:04:57.962 java[2368:80f] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000
The server is running at http://localhost:8080/

上記URLにアクセスすると「Hello App Engine!」が表示されればオK!
ページ内のGuestbookServletを押すと「Hello, world」が表示される。
GuestbookServlet.doGetが実行されている事がわかる。
ブレークポイント設定して、デバッグもちゃんとできた。

ちなみにWEBサーバー実行中にソース修正してセーブした後、再度ページにアクセスするとソース修正が反映されていた。
ホットデプロイですな。JSP, appengine-web.xmlとかもサーバー再起動無しに反映されるみたい。
web.xmlは再起動しないと反映されないだとさ(以下は原文)

With Eclipse, you can leave the server running in the debugger while you make changes to source code, 
JSPs, static files and appengine-web.xml.
When you save changes to source code, Eclipse compiles the class automatically, 
then attempts to insert it into the running web server dynamically. 
In most cases, you can simply reload the page in your browser to test the new version of the code. 
Changes to JSPs, static files and appengine-web.xml are recognized by the development server automatically, 
and also take effect without restarting the server. 
If you change web.xml or other configuration files, 
you must stop and start the server for the changes to take effect.
いよいよGAEにアップロード

1.設定ファイル変更
appengine-web.xmlの... の部分を、変更する。
事前に[shohu33]というアプリケーションIDをGAE上で作成していたので、
shohu33
のように変更。

その後、Eclipse上からGoogle App Engine ボタン(飛行機のようなタービン?の画像)を押すと、Email, と passwordを聞かれるので、googleアカウントのものを入れる。
その後deployボタンを押すと、以下のように表示されればデプロイ成功。

Creating staging directory
Scanning for jsp files.
Scanning files on local disk.
Initiating update.
Cloning 1 static files.
Cloning 13 application files.
Uploading 0 files.
Deploying new version.
Will check again in 1 seconds
Closing update: new version is ready to start serving.
Uploading index definitions.
Deployment completed successfully
動作確認

以下にアクセスして動作確認。Hello World が表示された!!
http://shohu33.appspot.com/guestbook

とりあえず動かせたので、この後簡単なアプリ作りたい所。今日はここまでにしよう。

というか、すごいなこれ。
簡単にJava WEBサイトが公開できてしまう。