Home

読み込み中...

Google App Engine Javaでcron実行

2010/12/01

このエントリーをはてなブックマークに追加

Google App Engineは無料でJavaが使えてcronもバンバン使える最強の無料サーバーなのですよ。

下準備

/war/WEB-INF/cron.xmlを書く。

<?xml version="1.0" encoding="UTF-8"?>
  <cronentries>
    <cron>
      <url>/cron/foo</url>
      <description>foo description</description>
      <schedule>every 2 minutes</schedule>
      <timezone>Tokyo</timezone>
    </cron>
    <cron>
      <url>/cron/bar</url>
      <description>bar description</description>
      <schedule>every 15 minutes</schedule>
      <timezone>Tokyo</timezone>
    </cron>
</cronentries>

まぁ、上のxmlはそれぞれ

  • url = たたきたいURL
  • description = cronの説明
  • schedule = スケジューリング
  • timezone = タイムゾーン

って感じです。まぁ、上のサンプルだとtimezoneいらないですね。

scheduleのフォーマット

こんな感じ。

every 5 minutes
every 12 hours
2nd,third mon,wed,thu of march 17:00
every monday of month 09:00
1st monday of sep,oct,nov 17:00

その他は、Java 用の cron を使用したスケジュール タスク – Google App Engine – Google Codeを参照。

セキュリティの設定

adminしかそのURLにアクセスできないようにする。

/war/WEB-INF/web.xmlの最後のほうに以下の記述を追加する。

<security-constraint>
  <web-resource-collection>
    <url-pattern>/cron/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>admin</role-name>
  </auth-constraint>
</security-constraint>

※ url-pattern/cron/*は適宜修正。

参考リンク

  1. Java 用の cron を使用したスケジュール タスク – Google App Engine – Google Code
  2. Google App EngineのCronでBOTを自動実行させる方法 – | Kotobuki Lab.

Leave a comment