javaWeb中任务调度
程序员文章站
2022-07-15 07:51:28
...
package com.application;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener {
private java.util.Timer timer = null;
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);
event.getServletContext().log("定时器已启动");
timer.schedule(new TaskTime(event.getServletContext()), 0,
1000); //60 * 60
event.getServletContext().log("已经添加任务调度表");
}
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
event.getServletContext().log("定时器销毁");
}
}
package com.application;
import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
public class TaskTime extends TimerTask {
private static final int C_SCHEDULE_HOUR = 0;
private static boolean isRunning = false;
private ServletContext context = null;
public TaskTime(ServletContext context) {
this.context = context;
}
public void run() {
Calendar cal = Calendar.getInstance();
if (!isRunning) {
isRunning = true;
context.log("开始执行指定任务");
// TODO 添加自定义的详细任务,以下只是示例
int i = 0;
while (i++ < 10) {
context.log("已完成任务的" + i + "/" + 10);
}
isRunning = false;
context.log("指定任务执行结束");
// if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) {
// isRunning = true;
// context.log("开始执行指定任务");
//
// // TODO 添加自定义的详细任务,以下只是示例
// int i = 0;
// while (i++ < 10) {
// context.log("已完成任务的" + i + "/" + 10);
// }
//
// isRunning = false;
// context.log("指定任务执行结束");
// }
} else {
context.log("上一次任务执行还未结束");
}
}
}
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener {
private java.util.Timer timer = null;
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);
event.getServletContext().log("定时器已启动");
timer.schedule(new TaskTime(event.getServletContext()), 0,
1000); //60 * 60
event.getServletContext().log("已经添加任务调度表");
}
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
event.getServletContext().log("定时器销毁");
}
}
package com.application;
import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
public class TaskTime extends TimerTask {
private static final int C_SCHEDULE_HOUR = 0;
private static boolean isRunning = false;
private ServletContext context = null;
public TaskTime(ServletContext context) {
this.context = context;
}
public void run() {
Calendar cal = Calendar.getInstance();
if (!isRunning) {
isRunning = true;
context.log("开始执行指定任务");
// TODO 添加自定义的详细任务,以下只是示例
int i = 0;
while (i++ < 10) {
context.log("已完成任务的" + i + "/" + 10);
}
isRunning = false;
context.log("指定任务执行结束");
// if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) {
// isRunning = true;
// context.log("开始执行指定任务");
//
// // TODO 添加自定义的详细任务,以下只是示例
// int i = 0;
// while (i++ < 10) {
// context.log("已完成任务的" + i + "/" + 10);
// }
//
// isRunning = false;
// context.log("指定任务执行结束");
// }
} else {
context.log("上一次任务执行还未结束");
}
}
}
上一篇: Dozer使用总结
下一篇: C3P0 与 DBCP 的区别
推荐阅读
-
mac系统任务栏中搜索快捷键不见了怎么办?
-
ASP.NET MVC 5 实现基于Quartz.net 的任务调度管理平台(二)
-
C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是...
-
Asp.Net Core 轻松学-基于微服务的后台任务调度管理器
-
yii框架中创建定时任务
-
一个简单的定时任务调度中心设计方案
-
详解MySQL用事件调度器Event Scheduler创建定时任务
-
基于Quartz定时调度任务(详解)
-
Java中消息队列任务的平滑关闭详解
-
SpringBoot2 task scheduler 定时任务调度器四种方式