1、任务基类:
package com.sxit.common; import java.util.Date; import java.util.TimerTask; /** * @功能:任务基类 * @作者: smile * @时间:2013-3-28 下午2:32:29 * @版本:1.0 */ public class BaseTimerTask extends TimerTask{ //任务类型 public int type; //执行周期时间 毫秒 public long period; //指定延迟执行时间 public long delay; //指定运行时间 public Date runTime; /** * 指定的时间运行,仅运行一次 * 对应Timer中的 schedule(TimerTask task, Date time) */ public static final int Schedule_Date=1; /** * 指定时间开始周期运行(基于延迟) * 用固定延迟调度。使用本方法时,在任务执行中的每一个延迟会传播到后续的任务的执行。 * 对应Timer中的schedule(TimerTask task, Date firstTime, long period) */ public static final int Schedule_Date_Period=2; /** * 指定时间开始周期运行(基于固定比率) * 用固定比率调度。使用本方法时,所有后续执行根据初始执行的时间进行调度,从而希望减小延迟 * 如果由于任何原因(如业务逻辑复杂、垃圾回收或其他后台活动)而延迟了某次执行,则将快速连续地出现两次或更多次执行,从而使后续执行能够赶上来 * 对应Tiemr中的scheduleAtFixedRate(TimerTask task, Date firstTime, long period) */ public static final int FixedRate_Date=3; /** * 指定延迟开始运行(基于固定比率) * 用固定比率调度。使用本方法时,所有后续执行根据初始执行的时间进行调度,从而希望减小延迟 * 如果由于任何原因(如业务逻辑复杂、垃圾回收或其他后台活动)而延迟了某次执行,则将快速连续地出现两次或更多次执行,从而使后续执行能够赶上来 * 对应Timer中的scheduleAtFixedRate(TimerTask task, long delay, long period) */ public static final int FixedRate_Period=4; public BaseTimerTask(int type){ this.type = type; } public void run() {} }
2、定时器类:
package com.sxit.common; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Timer; /** * @功能:定时器基类 * @作者: smile * @时间:2013-3-28 下午3:39:15 * @版本:1.0 */ public class BaseTimer extends Timer { // Timer队列 private static Map<String, BaseTimerTask> timerMap = new HashMap<String, BaseTimerTask>(); private static BaseTimer timer = new BaseTimer(); private BaseTimer() { } public static BaseTimer getInstance() { return timer; } // 向队列中添加Timer public void addTimer(String timerName, BaseTimerTask task) { timerMap.put(timerName, task); } // 启动Timer public synchronized void startTask() { if (timerMap != null) { Iterator<String> itr = timerMap.keySet().iterator(); while (itr.hasNext()) { Object obj = itr.next(); BaseTimerTask task = timerMap.get(obj.toString()); if (task.type == BaseTimerTask.Schedule_Date) { this.schedule(task, task.runTime); } else if (task.type == BaseTimerTask.Schedule_Date_Period) { this.schedule(task, task.runTime, task.period); } else if (task.type == BaseTimerTask.FixedRate_Date) { this.scheduleAtFixedRate(task, task.runTime, task.period); } else if (task.type == BaseTimerTask.FixedRate_Period) { this.scheduleAtFixedRate(task, task.delay, task.period); } else { // 配置信息不正确 退出 System.exit(0); } } } } // 销毁Timer public synchronized void stopTask() { if (timerMap != null) { Iterator<String> itr = timerMap.keySet().iterator(); while (itr.hasNext()) { Object obj = itr.next(); BaseTimerTask task = timerMap.get(obj.toString()); if(task != null){ task.cancel(); } } } } }
3、容器启动时加载Timer配置文件
package com.sxit.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Properties; /** * @功能:Web容器启动 装载Timer信息 * @作者: smile * @时间:2013-3-28 下午3:50:25 * @版本:1.0 */ public class TimerConfig { private static TimerConfig timerConfig = new TimerConfig(); // Timer配置文件名 private static String fileName = "mainTimer.properties"; private static Properties config = new Properties(); private TimerConfig() { init(); } public static TimerConfig getInstance() { return timerConfig; } // 加载配置Timer配置文件 public void init() { InputStream is = null; try { URL url = Thread.currentThread().getContextClassLoader().getResource(""); File f = new File(url.getPath() + File.separator + fileName); is = new FileInputStream(f); config.clear(); config.load(is); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 向队列中添加Timer public void setTask() { try { if (config != null && config.size() > 0) { BaseTimer timer = BaseTimer.getInstance(); Iterator itr = config.keySet().iterator(); while (itr.hasNext()) { Object obj = itr.next(); // 配置文件格式 // Timer名 = Timer类路径,Timer类型,执行时间,执行周期,延迟时间 String[] tmp = config.get(obj).toString().split(","); int type = Integer.parseInt(tmp[1]); // 通过反射生成指定Timer类的对象 并给type赋值 BaseTimerTask task = (BaseTimerTask) Class.forName(tmp[0]).getConstructor(new Class[] { Integer.TYPE }).newInstance(new Object[] { type }); if (type == BaseTimerTask.Schedule_Date) { task.runTime = parseTime(tmp[2]); } else if (type == BaseTimerTask.Schedule_Date_Period) { task.runTime = parseTime(tmp[2]); task.period = Long.parseLong(tmp[3]); } else if (type == BaseTimerTask.FixedRate_Date) { task.runTime = parseTime(tmp[2]); task.period = Long.parseLong(tmp[3]); } else if (type == BaseTimerTask.FixedRate_Period) { task.runTime = parseTime(tmp[2]); task.delay = Long.parseLong(tmp[4]); } else { System.exit(0);// 配置文件出错 } timer.addTimer(obj.toString(), task);// 添加Timer到队列中 } } } catch (Exception e) { e.printStackTrace(); } } //字符串转换Date public Date parseTime(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); return format.parse(s); } }
4、监听器:
package com.sxit.util; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import com.sxit.common.BaseTimer; import com.sxit.common.TimerConfig; public class ContextListener implements ServletContextListener { /** * Web容器初始化 */ public void contextInitialized(ServletContextEvent sce) { BaseTimer.getInstance().stopTask(); } /** * Web容器销毁 */ public void contextDestroyed(ServletContextEvent sce) { //读取Timer配置文件 并加入到队列中 TimerConfig.getInstance().setTask(); //启动Timer BaseTimer.getInstance().startTask(); } }
5、测试Timer:
package com.sxit.timertask; import com.sxit.common.BaseTimerTask; public class TestTask extends BaseTimerTask { public TestTask(int type) { super(type); } public void run() { System.out.println("这里执行任务"); } }
6、Timer配置文件:
#增值业务用户导入 #ImportCpUserTimerTask=ImportCpUserTimerTask,timertask.imports.ImportCpUserTimerTask,2,1000,5000 #未定制用户导入 #ImportUnOrderUserTimerTask=ImportUnOrderUserTimerTask,timertask.imports.ImportUnOrderUserTimerTask,2,1000,5000 #增值业务日报表 #CpUserDayTimerTask=CpUserDayTimerTask,timertask.businessRpt.CpUserDayTimerTask,5,05:33:00,86400000 #亲情号码按班级导入 #ImportFamilyMobileTaskNew=ImportManage,timertask.imports.familymobile.ImportFamilyMobileTaskNew,2,1000,50000 #定时解析10086的投诉文档 #UsercomplaintTimerTask=UsercomplaintTimerTask,timertask.businessRpt.UsercomplaintTimerTask,1,09:45:00,86400000 #定时解析10086的投诉文档(new) #UserComplaintTimerTaskNew=UserComplaintTimerTaskNew,timertask.businessRpt.UserComplaintTimerTaskNew,1,03:00:00,86400000 #异网用户导入 ImportYwUserTimerTask=ImportYwUserTimerTask,timertask.imports.ImportYwUserTimerTask,2,1000,5000
相关推荐
java定时任务调度Timer简单示例代码 ,
Spring使用timer定时器-能精确几点运行定时任务的小demo
主要为大家详细介绍了java定时任务Timer和TimerTask使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java里timer执行定时任务
java定时任务,每天定时执行任务,每天到这个时间点都会执行
C# 用Timer实现定时任务程序 初学者参考使用
带定时任务功能的,基于thikphp6详情自行查看,
用C# Timer 实现的简易定时任务windows服务,加入执行时间自行修补功能,可以基本实现每隔1分钟调取一次接口的场景,代码从项目中剥离,无法直接运行,可以下载后查阅源代码,核心代码在 PushService.cs 文件中
使用java.util.timer实现的简单定时任务,在实现简单一次性定时任务时,使用java.util.timer非常的简单易用,适合没有接触过quartz的新手急用。
.net core 定时执行某一时间的任务,可设置循环的小时,分钟,秒, 资源里只执行了一次定时任务,可循环多次,运行时自动进入方法
NULL 博文链接:https://inspire-xg.iteye.com/blog/1405342
我们通常写的timer都是用main方法写的定时器,但同样我们也需要根据服务器启动后定时器也启动的 定时执行任务。不过有个致命伤就是集群方面跟quartz不能比,此方法代码做参考不错,如果要用,那么这个方法在一台...
Timer exe是主程序 Updater exe是更新器 新版发布后用户可以通过更新器进行更新 建议用户下载后先更新在使用 定时睡眠不能在关闭睡眠模式下使用 程序可以被任务管理器结束掉 第一次运行请在第一个界面选择...
Timer schedule JAVA J2EE WEB 指定每一天某时某分执行某项任务的功能。 读取目录下配置文件的内容。 开发环境jdk1.5.0_22、MyEclipse6.5中文版。...定时任务管理器,定时执行,定时任务,run 绝对能用
源码 博文链接:https://zmx.iteye.com/blog/1479602
工作中常常会有定时任务的开发需求,特别是移动端。最近笔者正好有所涉及,鉴于此,结合开发中的案例说明一下几种定时任务的 退出 。 需求说明 :定时更新正在生成的文件大小和状态【进行中、失败、完成】,如果...
使用Python的timer来完成每天执行一次的定时任务,环境是windows,python3.4
在Android开发中,通过以下三种方法定时执行任务: 一、采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式) 二、采用Handler的postDelayed... 一、Timer 实现定时任务 Timer timer; void onCreate
利用jdk中的timer和timertask来实现,每天定时执行的任务。