`
javaeyetodj
  • 浏览: 425443 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

定时执行案例二

阅读更多

 

http://blog.sina.com.cn/s/blog_401a71d1010005cc.html

ScheduleIterator.java
import java.util.Calendar;
import java.util.Date;

public class ScheduleIterator {
private final int hourOfDay, minute, second;
private final Calendar calendar = Calendar.getInstance();

public ScheduleIterator(int month, int day, int hourOfDay, int minute,
int second) {
this(month, day, hourOfDay, minute, second, new Date());
}

public ScheduleIterator(int month, int day, int hourOfDay, int minute,
int second, Date date) {
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
calendar.setTime(date);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, 0);
if (!calendar.getTime().before(date)) {
calendar.add(Calendar.DATE, -1);
}
}

public Date next() {
calendar.add(Calendar.MINUTE, 1);
return calendar.getTime();
}

}

Scheduler.java
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Scheduler {

class SchedulerTimerTask
extends TimerTask {
private SchedulerTask schedulerTask;
private ScheduleIterator iterator;
public SchedulerTimerTask(SchedulerTask schedulerTask,
ScheduleIterator iterator) {
this.schedulerTask = schedulerTask;
this.iterator = iterator;
}

public void run() {
schedulerTask.run();
reschedule(schedulerTask, iterator);
}
}

private final Timer timer = new Timer();

public Scheduler() {
}

/**
* Terminates this <code>Scheduler</code>, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a scheduler has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.
* <p>
* Note that calling this method from within the run method of a scheduler task that was invoked by this scheduler absolutely guarantees that the ongoing task execution is the last task execution that will ever be performed by this scheduler.
* <p>
* This method may be called repeatedly; the second and subsequent calls have no effect.
*/

public void cancel() {
timer.cancel();
}

/**
* Schedules the specified task for execution according to the specified schedule. If times specified by the <code>ScheduleIterator</code> are in the past they are scheduled for immediate execution.
* <p>
* @param schedulerTask task to be scheduled
* @param iterator iterator that describes the schedule
* @throws IllegalStateException if task was already scheduled or cancelled, scheduler was cancelled, or scheduler thread terminated.
*/

public void schedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
}
else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.VIRGIN) {
throw new IllegalStateException("Task already scheduled " +
"or cancelled");
}
schedulerTask.state = SchedulerTask.SCHEDULED;
schedulerTask.timerTask =
new SchedulerTimerTask(schedulerTask, iterator);
System.out.println("time:s=" + schedulerTask.timerTask + "time:t=" +
time.toLocaleString());
timer.schedule(schedulerTask.timerTask, time);
}
}
}

private void reschedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
}
else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.CANCELLED) {
schedulerTask.timerTask =
new SchedulerTimerTask(schedulerTask, iterator);
System.out.println("time1:s=" + schedulerTask.timerTask + "time1:t=" +
time.toLocaleString());
timer.schedule(schedulerTask.timerTask, time);
}
}
}
}

}

SchedulerTask.java
import java.util.TimerTask;

/**
* A task that can be scheduled for recurring execution by a {@link Scheduler}.
*/
public abstract class SchedulerTask
implements Runnable {

final Object lock = new Object();
int state = VIRGIN;
static final int VIRGIN = 0;
static final int SCHEDULED = 1;
static final int CANCELLED = 2;
TimerTask timerTask;
protected SchedulerTask() {
}

public abstract void run();

public boolean cancel() {
synchronized (lock) {
if (timerTask != null) {
timerTask.cancel();
}
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}

public long scheduledExecutionTime() {
synchronized (lock) {
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
}
}

}

AlarmClock.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.*;

public class AlarmClock {
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
private final Scheduler scheduler = new Scheduler();
private final SimpleDateFormat dateFormat =
new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
private final int month, day, hourOfDay, minute, second;
private final String content;

public AlarmClock(int month, int day, int hourOfDay, int minute, int second,
String content) {
this.month = month;
this.day = day;
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
this.content = content;
}

public void start() {
scheduler.schedule(new SchedulerTask() {
public void run() {
soundAlarm();
}

private void soundAlarm() {
// 在这里放要执行的东西
System.out.println( "我* . 执行到这儿了. "+content);
}
}

, new ScheduleIterator(month, day, hourOfDay, minute, second));
}

public static void main(String[] args) {
AlarmClock alarmClock = new AlarmClock(3, 14, 16, 31, 40,
"hello world !!!");
alarmClock.start();
}

}

分享到:
评论

相关推荐

    定时任务+案例

    定时启动任务线程,执行任务。通过设置calendar.add(Calendar.SECOND, 900);//15分钟 隔多少秒执行扫描一次

    Java定时Quartz测试案例

    Quartz定时执行提醒,间断执行测试案例

    Python代码源码-实操案例-框架案例-如何在Windows系统下定时执行Python程序.zip

    Python代码源码-实操案例-框架案例-如何在Windows系统下定时执行Python程序.zip

    C#实现的自定义定时任务 可定时运行 多任务运行

    C#实现的自定义定时任务 可定时运行 多任务运行

    简洁的定时任务实例

    一般定时任务配置都需要相互的依赖,代码量多也不简洁,也得有任务管理器管理,此版本的定时任务本着简洁而去配置只需两步 ...二,相对的类和方法 &lt;bean id="myTaskXml" class="test.task"&gt;&lt;/bean&gt;

    PHP计划任务、定时执行任务的实现代码

    不知道能程序的性能会不会影响很大! 复制代码 代码如下: ignore_user_abort();... 您可能感兴趣的文章:php定时计划任务的实现方法详解php中定时计划任务的实现原理PHP定时执行计划任务的多种方法小结PHP定时执行

    C#设计经典案例设计与实现

    案例1O 动态调用可执行EXE文件 案例11 动态查看和修改文件属性 案例12 动态比较文件 本章小结 第9章 VisualC#2008算法及控件的应用 案例1 利用冒泡算法实现从小到大排序 案例2 利用希尔算法实现从大到小排序 案例3 ...

    Visual+C#+2008程序设计经典案例设计与实现.rar

    案例1O 动态调用可执行EXE文件 案例11 动态查看和修改文件属性 案例12 动态比较文件 本章小结 第9章 VisualC#2008算法及控件的应用 案例1 利用冒泡算法实现从小到大排序 案例2 利用希尔算法实现从大到小排序 案例3 ...

    powerbuilder案例开发集锦(源码光盘)1

    &lt;br&gt;二、内容 &lt;br&gt; 本光碟中包含图书中的每个案例的源代码、可执行文件和所有资源文件,具体组织结构如下: 第1章 窗体与控件 案例1 创建应用、窗口与控件 案例2 目录浏览器 案例3 文件...

    Visual C# 2008程序设计经典案例设计与实现

    案例10 动态调用可执行EXE文件 案例11 动态查看和修改文件属性 案例12 动态比较文件 本章小结 第9章 Visual C#2008算法及控件的应用 案例1 利用冒泡算法实现从小到大排序 案例2 利用希尔算法实现从大到小排序 ...

    java 定时器任务timer 随tomcat启动而启动 同步启动

    我们通常写的timer都是用main方法写的定时器,但同样我们也需要根据服务器启动后定时器也启动的 定时执行任务。不过有个致命伤就是集群方面跟quartz不能比,此方法代码做参考不错,如果要用,那么这个方法在一台...

    C# 源码 C#2008经典案例

    案例1O 动态调用可执行EXE文件 案例11 动态查看和修改文件属性 案例12 动态比较文件 本章小结 第9章 VisualC#2008算法及控件的应用 案例1 利用冒泡算法实现从小到大排序 案例2 利用希尔算法实现从大到小排序 案例3 ...

    PHP定时执行计划任务的多种方法小结

    PHP定时执行的三种方式实现 1、windows 的计划任务 2、linux的脚本程序 3、让web浏览器定时刷新 具体实现 windows计划任务 PHP很少在win服务器上跑,具体实现也不再深究,看网上实现的原理大概是写bat脚本,然后让...

    Spring定时任务(Web项目)

    本案例实现利用Spring技术实现定时任务功能,用于Web项目

    quartz集合spring实现定时生成器

    自己用Spring、Quartz集合freemarker 实现了一个定时执行模板或者jsp等文件转化为html页面的案例,里面每步都写了注释,比较适合没有接触过,但是又想了解定时生成页面的朋友.希望对大家有帮助,谢谢!

    Quartz简单案例

    简单案例 定时任务执行带参数的方法 通过NuGet安装Install-Package Quartz

    定时调度器 xxl-job

    这个文件包含了xxl-job的定时调度中心源码、执行器demo源码,需要的可以下载。

    Visual c++案例开发集锦

    第二章 捕获鼠标绘图 捕捉屏幕图像 获取屏幕上颜色 界面上实现3D文字 列举当前系统的字体 判知BMP图片的大小 图片的伸缩显示 显示资源位图 游览大图 第三章 CArray类的应用 CListCtrl 控件的应用 IP Address控件编辑...

    Tomcat集群案例

    spring+Quartz定时任务:主要是白天的一些不是需要很实时性的东西放到夜间执行、还有一些统计性的问题。 OsCache缓存框架:是一款相对来说比较不错的缓存框架、可以缓存对象、也可以缓存单个页面、可以很好的减少...

Global site tag (gtag.js) - Google Analytics