Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 如何在Java中实现Job Scheduling?
zhbk





发贴: 10
积分: 0
于 2004-07-13 17:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在大多数项目中,在特定的时间或频率下你需要完成一些特定的任务。在本文中我们将演示如何用标准的Java Timer API来实现。
大多商业应用都会用报表和统计,很难想象没有这些帮助我们分析未来趋势的数据,我们还要这系统干什么。问题是如此多的数据我们该如何触发,如果处理对系统性能影响不大。比较好的方式是避开应用高峰,让服务器在闲暇时完成这些事。
下面是我的程序:
package net.nighttale.scheduling;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author kevin zhou
*
* 在Java中实现Job Scheduling
* 创建日期 2004-7-13
*/
public class ReportGenerator extends TimerTask {

public void run() {
System.out.println("Generating report");
//TODO generate report
}

}

class MainApplication {

public static void main(String[] args) {
Timer timer=new Timer();
Calendar date = Calendar.getInstance();
date.set(
Calendar.DAY_OF_WEEK,
Calendar.SUNDAY
);
date.set(Calendar.HOUR, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// Schedule to run every Sunday in midnight
timer.schedule(
new ReportGenerator(),
date.getTime(),
1000 * 60 * 60 * 24 * 7
);
}
}
我先简单解释一下,在我们的例子中ReportGenerator继承java.util.TimerTask,它又继承了java.lang.Runnable,我们需要覆盖run()方法。
调用时我们用schedule()方法让它每周日0点0分执行,避开服务器高峰,实现Job Scheduling的目的。



作者 Re:如何在Java中实现Job Scheduling? [Re:zhbk]
javaguan2





发贴: 18
积分: 0
于 2004-08-28 20:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错,好帖子!,我更注意的是你对日期的分解处理.谢谢


作者 Re:如何在Java中实现Job Scheduling? [Re:zhbk]
littledeer1974

Garbage Collector

CJSDN高级会员


发贴: 1517
积分: 122
于 2004-08-30 16:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
对不起,有一个小疑问,是不是星期日的0点0分执行,要看你的程序什么时间执行吧,我记的,schedule()里指定的只是时间间隔吧,不知道是不是我记错了


作者 Re:如何在Java中实现Job Scheduling? [Re:zhbk]
sendtome





发贴: 48
积分: 20
于 2004-09-12 23:36 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因为他指定了初始值是星期日的0点0分第一次执行,时间间隔是7天,那结果当然是。。。


作者 Re:如何在Java中实现Job Scheduling? [Re:zhbk]
ouyong





发贴: 9
积分: 0
于 2004-09-14 14:58 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
嗯,我会了^_^


作者 Re:如何在Java中实现Job Scheduling? [Re:sendtome]
littledeer1974

Garbage Collector

CJSDN高级会员


发贴: 1517
积分: 122
于 2004-09-18 21:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
sendtome wrote:
因为他指定了初始值是星期日的0点0分第一次执行,时间间隔是7天,那结果当然是。。。

嗯,那样的话 就是。。。



Surround yourself with people who are open to change


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923