Java+Tomcatでタスクを定期的に実行する方法 実装一例
2011/11/10 19:21Update
Java+Tomcatでタスクを定期的に実行する方法 の一例。
コード例:
web.xml
Quartz Enterprise Job Scheduler - J2SE、J2EEアプリケーション用のオープンソースジョブスケジューラ
package com.syboos.timer;
//import package HERE TODO
...
public class TimerListener implements ServletContextListener {
//タイマーの起動時間:24:00
private static final int HOUR = 24;
private static final int MINUTE = 0;
private static final int SECOND = 0;
private Timer timer = null;
public void contextDestroyed(ServletContextEvent event) {//コンテキストの
timer.cancel();
event.getServletContext().log("Timer is canceled.");
}
public void contextInitialized(ServletContextEvent event) {//コンテキストを初期化時
timer=new Timer(true);
event.getServletContext().log("Timer is started.");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, HOUR);
calendar.set(Calendar.MINUTE, MINUTE);
calendar.set(Calendar.SECOND, SECOND);
//タイマーに実行したいジョブを設定
timer.schedule(new MyJob(event.getServletContext()), calendar.getTime(),
1*24*60*60*1000);
}
}
web.xml
<!-- タイマー設定 --> <listener> <listener-class>com.syboos.timer.TimerListener</listener-class> </listener>
参考資料
Quartz Enterprise Job Scheduler - J2SE、J2EEアプリケーション用のオープンソースジョブスケジューラ
Sponsored Link
Comments
- Relative Articles
- Java開発及び実行環境の構築 | Linux篇 - (2008/08/31 20:34)
- Java開発及び実行環境の構築 | Windows篇 - (2008/08/31 21:18)
- 5分でJava Hello World! - (2008/08/31 22:13)
- Java言語の制御構文 - 条件分岐if/else/else if - (2008/10/01 21:30)
- Java言語の繰り返し制御構文 - for文 - (2008/10/03 18:22)
- Java言語の繰り返し制御構文 - do ... while文 - (2008/10/03 21:23)
- Java言語の繰り返し制御構文 - while文 - (2008/10/03 21:32)
- Java言語の基礎 - javacコマンドによるコンパイル - (2008/10/10 17:58)
- Java アクセス修飾子概要 - (2008/10/29 18:31)
- Javaアノテーション機能 概要 - (2009/02/12 13:06)