- 通过一个主crontab任务去维护别的任务
- 自定义的计划任务完全由PHP编写
- 任务的执行计划时间表设置与crontab的时间表设置语法一致
- 在指定的时间内只会运行一个任务
- 邮件告警异常退出任务
- 在root权限下可以控制任务的执行用户
- 指定主机名运行
-
win7里任务管理器能设置显示CPU时间吗
2013-12-20 12:06:13[img=https://img-bbs.csdn.net/upload/201312/20/1387512294_828148.jpg][/img] 上面这图是在windows server里的任务管理器 win7能设置显示这个CPU时间的列吗 -
C#通用类库--设置开机自运行禁用任务管理器注册表等操作
2017-03-02 11:46:58在做项目当中,有一项特殊的客户需求就是软件需要开机自运行,并且在运行期间不能进行关机等特定操作,需要屏蔽任务管理器和注册表等,于是自己把这些写成了通用的类,以后只要直接调用就行! //类名:...在做项目当中,有一项特殊的客户需求就是软件需要开机自运行,并且在运行期间不能进行关机等特定操作,需要屏蔽任务管理器和注册表等,于是自己把这些写成了通用的类,以后只要直接调用就行!
//类名:EcanSystem
//作用:系统设置及其他
//作者:刘典武
//时间:2010-12-05
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace Ecan
{
public class EcanSystem
{
/// <summary>
/// 设置程序开机运行
/// </summary>
/// <param name="started">是否开机运行</param>
/// <param name="exeName">要运行的EXE程序名称(不要拓展名)</param>
/// <param name="path">要运行的EXE程序路径</param>
/// <returns>成功返回真,否则返回假</returns>
public bool runWhenStart(bool started, string exeName, string path)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch
{
return false;
}
}
return true;
}
/// <summary>
/// 解禁任务管理器
/// </summary>
/// <returns>成功返回真,否则返回假</returns>
public bool enableTaskmgr()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
}
try
{
key.SetValue("disabletaskmgr", 0, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 禁用任务管理器
/// </summary>
/// <returns>成功返回真,否则返回假</returns>
public bool notEnableTaskmgr()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
}
try
{
key.SetValue("disabletaskmgr", 1, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 解禁注册表
/// </summary>
/// <returns>成功返回真,否则返回假</returns>
public bool enableRegedit()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
}
try
{
key.SetValue("disableregistrytools", 0, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 禁用注册表
/// </summary>
/// <returns>成功返回真,否则返回假</returns>
public bool notEnableRegedit()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
}
try
{
key.SetValue("disableregistrytools", 1, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 结束进程
/// </summary>
/// <param name="processName">进程名称</param>
/// <returns>成功返回真,否则返回假</returns>
public bool killProcess(string processName)
{
try
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == processName)
{
p.Kill();
}
}
return true;
}
catch { return false; }
}
/// <summary>
/// 注册控件
/// </summary>
/// <param name="dllIdValue">控件注册后对应的键值</param>
/// <returns>成功返回真,否则返回假</returns>
public bool regDll(string dllIdValue)
{
try
{
RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"CLSTD\" + dllIdValue, true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.ClassesRoot.CreateSubKey(@"CLSTD\" + dllIdValue);
}
return true;
}
catch { return false; }
}
/// <summary>
/// 压缩图片(指定压缩比例值)
/// </summary>
/// <param name="fromFile">源文件</param>
/// <param name="saveFile">保存文件</param>
/// <param name="bili">比例值(例如0.5)</param>
/// <returns>成功返回真,否则返回假</returns>
public bool pressImage(string fromFile, string saveFile,double bili)
{
Image img;
Bitmap bmp;
Graphics grap;
int width, height;
try
{
img = Image.FromFile(fromFile);
width = Convert.ToInt32(img.Width * bili);
height = Convert.ToInt32(img.Height * bili);
bmp = new Bitmap(width, height);
grap = Graphics.FromImage(bmp);
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grap.DrawImage(img, new Rectangle(0, 0, width, height));
bmp.Save(saveFile, System.Drawing.Imaging.ImageFormat.Jpeg);
grap.Dispose();
bmp.Dispose();
img.Dispose();
return true;
}
catch { return false; }
}
/// <summary>
/// 压缩图片(指定高度和宽度)
/// </summary>
/// <param name="fromFile">源文件</param>
/// <param name="saveFile">保存文件</param>
/// <param name="width">宽度值</param>
/// <param name="height">高度值</param>
/// <returns>成功返回真,否则返回假</returns>
public bool pressImage(string fromFile, string saveFile, int width,int height)
{
Image img;
Bitmap bmp;
Graphics grap;
try
{
img = Image.FromFile(fromFile);
bmp = new Bitmap(width, height);
grap = Graphics.FromImage(bmp);
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grap.DrawImage(img, new Rectangle(0, 0, width, height));
bmp.Save(saveFile, System.Drawing.Imaging.ImageFormat.Jpeg);
grap.Dispose();
bmp.Dispose();
img.Dispose();
return true;
}
catch { return false; }
}
}
}一个C#资源分享平台,专业分享学习高质量代码,每周期布置学习任务,激发学习C#兴趣!(QQ群:128874886) -
定时任务管理器工具
2019-04-17 14:58:59自己造轮子是件有趣的事情,自己手写了一个定时器管理器。使用的场景是有多个后台运行的定时任务的web项目,愿景是让定时器执行过程可视化,可以在界面控制每一个定时任务,进行开关,立刻执行任务等操作。 功能 这...前言
自己造轮子是件有趣的事情,自己手写了一个定时器管理器。使用的场景是有多个后台运行的定时任务的web项目,愿景是让定时器执行过程可视化,可以在界面控制每一个定时任务,进行开关,立刻执行任务等操作。
功能
- 这个容器可以非常方便的管理多个定时任务,可以动态的在内存修改配置,修改后立马生效。无需重启项目。对于某个定时任务都可以独立配置
- 可以动态的设置任务的开关
- 动态改变定时任务的时间间隔
- 控制定时任务周一到周五是否执行或者不执行 (比如对于短信通知任务可以设置双休时不发送)
- 指定未来的某天定时任务是否执行(比如可以设置节假日不执行定时任务)
- 检测定时任务当前的执行状态
- 如果定时任务正在执行,那么会跳过本次执行
- 检测定时任务下次执行的时间
- 可以手动立刻执行务 (比如需要立刻更新某些状态,可以直接手动执行)
- 提供了任务启动后,任务发生错误时,开启和关闭任务后的回调接口(比如定时任务发生异常时可以短信通知)
- 可以查看每个定时任务的日志记录,可以设置日志数量(队列实现,存储在内存中,设置数量不会导致内存爆炸)
- 可以设置任务执行的有效次数,比如我只想执行3次,执行完3次后,后面就不执行了
- 可以动态的添加定时任务,定时任务也可动态的配置
提供操作界面,需要控制层程序支持。容器提供的相关动态的修改内存的配置,即可做到上面的控制
容器可以监听器中初始化,需要调用register方法注册定时任务(继承 com.zjca.bss.timer.TimerTask 就是定时任务类,可以注册到该定时容器)代码
TaskConfig 配置类
package com.zjca.bss.timer; import java.util.Date; import java.util.LinkedList; /** * @Auther: ycl * @Date: 2019/4/15 11:31 * @Description:定时任务配置 */ public class TaskConfig { /** * 定时器的有效性,默认有效,当为false时,即使时间到了,也不会触发任务 * 这个属性优先级最高,如果为false,下面属性全部不生效 */ private boolean enable = true; /** * 是否在定时器管理容器初始化时就执行一次任务,默认在启动时不执行 */ private boolean executeOnContainerBootstrap = false; /** * 启动的小时数,24小时制。0 - 23 */ private Integer startHour; /** * 启动的分钟数 0 - 60 */ private Integer startMinute; /** * 启动的秒数 0 - 60 */ private Integer startSecond; /** * 间隔执行时间,单位是毫秒 */ private long interval; /** * 一个星期7天,对应着7个boolean值,默认每天都有效,比如就星期二到星期五执行,周一和周六周日不执行就设置成 *{false,true,true,true,true,false,false} */ private boolean[] dayEnable = new boolean[]{true,true,true,true,true,true,true}; /** * 最大有效次数 * null表示无限次数 * 比如我只想定时器任务只执行,三次,设置成3就可以了,执行完三次之后就将任务设置成不可用 */ private Integer effectiveNum = null; /** * 不执行的日期。这个优先级大于 dayEnable。 * 可以动态的添加数据,添加指定日期,到了那天,定时器都不会执行 * 过期日期自动删除 */ public LinkedList<Date> notExecuteDateList = new LinkedList<>(); /** * 最大日志数量,定时器可以存储执行过程的一些日志,当日志数量大于设置的数量时,会删掉以前的,以队列的形式存在 * 默认存储20条记录 */ private int maxLogNum = 20; public TaskConfig(int startHour, int startMinute, int startSecond, long interval) { this.startHour = startHour; this.startMinute = startMinute; this.startSecond = startSecond; this.interval = interval; } public TaskConfig(long interval) { this.interval = interval; } public boolean isEnable() { return enable; } public void setEnable(boolean enable) { this.enable = enable; } public boolean isExecuteOnContainerBootstrap() { return executeOnContainerBootstrap; } public void setExecuteOnContainerBootstrap(boolean executeOnContainerBootstrap) { this.executeOnContainerBootstrap = executeOnContainerBootstrap; } public long getInterval() { return interval; } public void setInterval(long interval) { this.interval = interval; } public boolean[] getDayEnable() { return dayEnable; } public void setDayEnable(boolean[] dayEnable) { this.dayEnable = dayEnable; } public Integer getEffectiveNum() { return effectiveNum; } public void setEffectiveNum(Integer effectiveNum) { this.effectiveNum = effectiveNum; } public LinkedList<Date> getNotExecuteDateList() { return notExecuteDateList; } public void setNotExecuteDateList(LinkedList<Date> notExecuteDateList) { this.notExecuteDateList = notExecuteDateList; } public int getMaxLogNum() { return maxLogNum; } public void setMaxLogNum(int maxLogNum) { this.maxLogNum = maxLogNum; } public Integer getStartHour() { return startHour; } public Integer getStartMinute() { return startMinute; } public Integer getStartSecond() { return startSecond; } }
TimerTask 定时任务类
需要继承该类,实现
public abstract TaskRunResultDTO task(TaskConfig config) 抽象方法
在task方法中编写具体的业务逻辑,可以覆盖onStart onClose onError等回调方法package com.zjca.bss.timer; import com.zjca.bss.utils.DateUtils; import java.util.*; /** * @Auther: ycl * @Date: 2019/4/15 11:30 * @Description:定时任务 */ public abstract class TimerTask implements Runnable { /** * 是否正在运行 */ private volatile boolean running = false; /** * 任务id */ private String id; /** * 任务名称 */ private String taskName; /** * 任务执行次数 */ private int taskExecuteNum; /** * 最后执行时间 */ private Date lastTime; /** * 下次执行时间 */ private Long nextExecuteTime; /** * 间隔数 */ private Long interval; /** * 任务配置 */ private TaskConfig config; /** * 执行日志 */ private Queue<String> logList = new LinkedList<String>(); /** * @param taskName 定时任务名称 * @param config 定时任务配置 * @throws TaskException */ public TimerTask(String taskName,TaskConfig config) throws TaskException { String check = checkConfig(config); if(check != null){ throw new TaskException(check); } this.taskName = taskName; this.config = config; this.id = UUID.randomUUID().toString().replaceAll("-", ""); //设置下一次启动时间 Date date = DateUtils.createTime(null,null,null,config.getStartHour(),config.getStartMinute(),config.getStartSecond()); interval =config.getInterval(); if(System.currentTimeMillis() < date.getTime()){ nextExecuteTime = date.getTime(); }else if(System.currentTimeMillis() > date.getTime() + interval){ //启动时间过时 nextExecuteTime = System.currentTimeMillis() + interval; }else { nextExecuteTime = date.getTime() + interval; } } /** * 校验配置是否正确,不正确返回具体的原因,如果正确,返回Null * @param config * @return */ private String checkConfig(TaskConfig config){ return null; } @Override public void run() { //更新执行的信息 taskExecuteNum ++; lastTime = new Date(); running = true; //计算下次执行时间间隔 nextExecuteTime = lastTime.getTime() + interval; try { addLog("定时任务开始执行"); TaskRunResultDTO result = task(config); addLog("定时任务开始完毕"); after(config, result); }catch (Throwable e){ addLog("定时任务执行出错"); try { onError(config, e); }catch (Exception e1){ addLog("onError回调出错:" + e1.toString()); } }finally { running = false; } } //--------------------------主要的定时任务------------------------- /** * 要执行的定时任务 * @param config * @return TaskRunResultDTO 定时任务执行返回的结果,可以是null。但是如果有回调 after 函数,需要具体的设置值运行。 * @throws Exception */ public abstract TaskRunResultDTO task(TaskConfig config); //--------------------------一些回调方法,需要回调就覆盖该方法------------------------- /** * 在定时任务完成之后回调。调用 task 且没有任何错误后就会调用after * 需要回调就覆盖该方法 * @param config */ public void after(TaskConfig config,TaskRunResultDTO dto){} /** * 执行任务时发生错误会执行onError方法。 * 需要回调就覆盖该方法 * @param config */ public void onError(TaskConfig config,Throwable error) {} /** * 关闭定时任务时回调 * 需要回调就覆盖该方法 * @param config */ public void onClose(TaskConfig config){} /** * 开启定时任务时回调 * 需要回调就覆盖该方法 * @param config */ public void onStart(TaskConfig config){} /** * 添加日志 */ public void addLog(String msg){ if(logList.size() >= config.getMaxLogNum()){ logList.poll(); } logList.offer(DateUtils.format(new Date()) + ": [" + this.taskName + "]" + msg); } /** * 开启定时器任务,并不是执行 */ public void start(){ config.setEnable(true); addLog("开启定时任务成功"); try { onStart(config); }catch (Exception e){ addLog("onStart回调出错:" + e.toString()); } } /** * 关闭定时器任务,可以开启 */ public void close(){ config.setEnable(false); addLog("关闭定时任务成功"); try { onClose(config); }catch (Exception e){ addLog("onClose回调出错:" + e.toString()); } } /** * 添加不执行定时器的日期,比如手动操作,添加节假日不用执行发送审核提醒的定时器 * @param date * @throws TaskException */ public void addNotExecuteDate(Date date) throws TaskException { if(date == null){ throw new TaskException("过时日期不能为空"); } /** * */ /*if(DateUtils.getDiffDay(date,null)<= 0){ throw new TaskException("过时日期不能添加,只能添加今天之后的日期"); }*/ config.notExecuteDateList.add(date); addLog("添加不执行的日期成功,日期为:" + DateUtils.format(date,DateUtils.FORMAT_DATE_ONLY)); } /** * 批量添加不执行定时器的日期 * @param dates * @throws TaskException */ public void addNotExecuteDate(Date[] dates) throws TaskException { if(dates == null || dates.length == 0){ throw new TaskException("过时日期不能为空"); } for(Date date:dates){ addNotExecuteDate(date); } } /** * 更新下次执行时间 * @param time 如果为null 默认+1天 */ public void updateNextExecuteTime(Long time){ if(time == null){ time = 86400000L;//24小时的毫秒时间 } nextExecuteTime += time; nextExecuteTimeStr = DateUtils.format(new Date(nextExecuteTime)); } /** * 立刻启动定时任务。 * 启动定时任务并不是多线程启动,而是调用这个方法的线程去执行这个定时任务 * 立刻执行定时任务不会受到配置的影响,也不会影响下一次定时任务的执行。 * 如果需要自定义,请重写该方法 */ public TaskRunResultDTO executeImmediately() { addLog("手动调用,立刻执行!"); TaskRunResultDTO resultDTO = null; try { running = true; resultDTO = task(config); } catch (Exception e) { addLog("手动调用执行失败!详情:" + e.toString()); }finally { running = false; } return resultDTO; } public boolean isRunning() { return running; } public String getId() { return id; } public String getTaskName() { return taskName; } public int getTaskExecuteNum() { return taskExecuteNum; } public Date getLastTime() { return lastTime; } public Long getNextExecuteTime() { return nextExecuteTime; } public Long getInterval() { return interval; } public TaskConfig getConfig() { return config; } public Queue<String> getLogList() { return logList; } }
TimerTaskContainer 定任务容器类
继承了TimerTask类的定时任务 注册到该容器即可管理容器内的任务
package com.zjca.bss.timer; import com.zjca.bss.utils.DateUtils; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @Auther: ycl * @Date: 2019/4/15 11:51 * @Description:定时器容器 * 这个容器可以非常方便的管理多个定时任务,可以动态的在内存修改配置,修改后立马生效。无需重启项目。对于某个定时任务可以管理以下配置 * 并且可以动态的设置任务的开关 * 动态改变定时任务的时间间隔 * 控制定时任务周一到周五是否执行或者不执行 (比如对于短信通知任务可以设置双休时不发送) * 指定未来的某天定时任务是否执行,(比如可以设置节假日不执行定时任务) * 检测定时任务当前的执行状态 * 检测定时任务下次执行的时间 * 可以手动立刻执行务 (比如需要立刻更新某些状态,可以直接手动执行) * 提供了任务启动后,任务发生错误时,开启和关闭任务后的回调接口(比如定时任务发生异常时可以短信通知) * 可以查看每个定时任务的日志记录,可以设置日志数量(队列实现,存储在内存中,设置数量不会导致内存爆炸) * 可以设置任务执行的有效次数,比如我只想执行3次,执行完3次后,后面就不执行了 * 可以动态的添加定时任务,定时任务也可动态的配置 * * * 提供操作界面,需要控制层程序支持。容器提供的相关动态的修改内存的配置,即可做到上面的控制 * 容器可以监听器中初始化,需要调用register方法注册定时任务(继承 com.zjca.bss.timer.TimerTask 就是定时任务类,可以注册到该定时容器) */ public class TimerTaskContainer{ private static TimerTaskContainer instance; private Timer timer; /** * 索引map通过任务id快速找到任务对象 */ Map<String,TimerTask> indexMap = new HashMap<String,TimerTask>(); /** * 任务列表 */ List<TimerTask> taskList = new ArrayList<TimerTask>(); /** * 执行定时任务的线程池 */ ExecutorService executorService; private TimerTaskContainer(){} public static TimerTaskContainer getInstance(){ if(instance == null){ synchronized (TimerTaskContainer.class){ if(instance == null){ instance = new TimerTaskContainer(); } } } return instance; } //注册定时任务 public void register(TimerTask task) { taskList.add(task); indexMap.put(task.getId(),task); if(task.getConfig().isExecuteOnContainerBootstrap()){ task.executeImmediately(); } } /** * 启动定时器容器 */ public void bootstrap(){ if(taskList == null || taskList.size() == 0){ return; } /** * 定时器管理器是一个定时任务线程 * 遍历每一个用户定义的任务类,读取配置信息,如果满足执行条件,就开启新线程,去执行这个任务 */ timer = new Timer(); executorService = Executors.newFixedThreadPool(5); timer.schedule(new java.util.TimerTask() { public void run() { taskListLoop: for(TimerTask task:taskList){ if(task == null){ continue ; } TaskConfig config = task.getConfig(); //判断是否到执行的时间点了。比如定时任务的下次执行时间是昨天早上8点,但是昨天设置了不执行,这个时间没有更新,今天早上8点要执行了,依然满足这个条件。今天会继续执行 if(task.getNextExecuteTime() > System.currentTimeMillis()){ continue ; } //定时器总的开关 if(!config.isEnable()){ continue; } //指定了星期不执行 if(!config.getDayEnable()[DateUtils.getDayOfWeek(null) - 1]){ //下次执行时间加1天 task.updateNextExecuteTime(null); continue; } //是否指定了今天不执行 List<Date> list = config.getNotExecuteDateList(); if(list != null || list.size() > 0){ Iterator<Date> iterator = list.listIterator(); while (iterator.hasNext()){ Date date = iterator.next(); int d = DateUtils.getDiffDay(date,null); if(d == 0){ //当天不执行,下次执行时间加到1天后 task.updateNextExecuteTime(null); continue taskListLoop; }else if(d < 0){ //过时,删掉 iterator.remove(); } } } //是否执行完了最大次数 if(config.getEffectiveNum() != null && task.getTaskExecuteNum() >= config.getEffectiveNum()){ //将定时器设置成不可用 config.setEnable(false); task.addLog("有效次数执行完毕,本次执行跳过"); continue ; } //如果正在运行,跳过本次执行 if(task.isRunning()){ task.addLog("检测到任务正在执行,本次跳过"); continue ; } //执行定时任务 executorService.execute(task); } } }, new Date(),1000); } /** * 关闭定时容器 * 释放定时线程和任务执行线程,正在执行的任务会尝试等它执行完毕之后再关掉该线程 * 关闭之后还能启动哦 */ public void close(){ if(timer != null){ timer.cancel(); } if(executorService != null){ executorService.shutdown(); } } public Map<String, TimerTask> getIndexMap() { return indexMap; } public List<TimerTask> getTaskList() { return taskList; } }
TaskException 定时器任务异常类
package com.zjca.bss.timer; /** * @Auther: ycl * @Date: 2019/4/15 11:32 * @Description: */ public class TaskException extends RuntimeException { /** * Constructs a new exception with the specified detail message. The * cause is not initialized, and may subsequently be initialized by * a call to {@link #initCause}. * * @param message the detail message. The detail message is saved for * later retrieval by the {@link #getMessage()} method. */ public TaskException(String message) { super(message); } /** * Constructs a new exception with the specified detail message and * cause. <p>Note that the detail message associated with * {@code cause} is <i>not</i> automatically incorporated in * this exception's detail message. * * @param message the detail message (which is saved for later retrieval * by the {@link #getMessage()} method). * @param cause the cause (which is saved for later retrieval by the * {@link #getCause()} method). (A <tt>null</tt> value is * permitted, and indicates that the cause is nonexistent or * unknown.) * @since 1.4 */ public TaskException(String message, Throwable cause) { super(message, cause); } }
TaskRunResultDTO 回调返回结果封装类
package com.zjca.bss.timer; /** * @Auther: ycl * @Date: 2019/4/15 15:44 * @Description: */ public class TaskRunResultDTO { private Integer code; private String msg; private Object value; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } }
初始化定时器容器
在监听器中配置并注册定时任务到定时管理容器中
package com.zjca.bss.listener; import com.zjca.bss.task.OverdueCertCheckTask; import com.zjca.bss.task.WaitAuditRegNotifyTask; import com.zjca.bss.task.AlipayDataSyncTask; import com.zjca.bss.timer.TaskConfig; import com.zjca.bss.timer.TimerTaskContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * @Auther: ycl * @Date: 2019/4/16 14:02 * @Description: 定时任务监听器 */ public class TimerTaskListener implements ServletContextListener { private final static Logger logger = LoggerFactory.getLogger(TimerTaskListener.class); @Override public void contextInitialized(ServletContextEvent servletContextEvent) { TimerTaskContainer timerTaskContainer = TimerTaskContainer.getInstance(); //----------------未审核申请表通知 定时器任务---------------- //每天早上8点,0分,0秒(24小时执行一次) TaskConfig waitAuditRegNotifyTaskConfig = new TaskConfig(8, 0, 0, 24 * 3600 * 1000); //设置周1到周5执行,周六,周日不执行 waitAuditRegNotifyTaskConfig.setDayEnable(new boolean[]{true, true, true, true, true, false, false}); //注册未审核申请表通知定时器 timerTaskContainer.register(new WaitAuditRegNotifyTask("未审核申请表通知", waitAuditRegNotifyTaskConfig)); //----------------过期证书检测定时器任务---------------- //每天凌晨2点对所有正常证书进行循环迭代。 TaskConfig overdueCertCheckTaskConfig = new TaskConfig(2, 0, 0, 24 * 3600 * 1000); //注册过期证书检测定时任务 timerTaskContainer.register(new OverdueCertCheckTask("过期证书检测", overdueCertCheckTaskConfig)); //----------------支付宝数据同步定时器任务---------------- //5分钟执行一次 TaskConfig alipayDataSyncTaskConfig = new TaskConfig(5 * 60 * 1000); timerTaskContainer.register(new AlipayDataSyncTask("支付宝数据检测", alipayDataSyncTaskConfig)); //启动定时器任务管理器 timerTaskContainer.bootstrap(); logger.info(">>>>>>>>>>>>>定时器管理器启动成功>>>>>>>>>>>>>>>>"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { TimerTaskContainer.getInstance().close(); } }
原理
只有一个定时线程每一秒遍历定时任务集合,判断是否达到执行条件,达到执行条件就会启用一个线程执行该定时任务
所以定时任务配置被改变后容器都能时时检测到,根据不同的条件去判断是否要执行这个定时任务。
定时任务类对象有一些方法可以操作定时器的开关、立即执行等操作,其他一些功能还需要完善,无非就是修改变量数据。定时任务注册时,会生成一个UUID作为该任务的ID,任务名称在创建任务对象时需要指定。任务存储在容器的这两个变量中
/** * 索引map通过任务id快速找到任务对象 */ Map<String,TimerTask> indexMap = new HashMap<String,TimerTask>(); /** * 任务列表 */ List<TimerTask> taskList = new ArrayList<TimerTask>();
后续步骤,就是自己写一个控制层方法,通过定期器容器拿数据,动态的设置配置就OK了,具体支持哪些配置看 TaskConfig 类
-
关于windows中的任务管理调度器
2017-11-14 11:51:00这个任务管理调度器是公司培训同事在讲studio中的job可以在studio中设置时间或者事件触发器来执行job也可以委托给第三方的软件来触发你的job. 以前真没有怎么听过windows的这个任务管理调度器现在总结一下怎...windows中的任务管理调度器
任务管理调度器大概就是给windows设置一个任务,同时还可以设置这个任务的执行时间,执行次数等.
这个任务管理调度器是公司培训同事在讲studio中的job可以在studio中设置时间或者事件触发器来执行job也可以委托给第三方的软件来触发你的job.
以前真没有怎么听过windows的这个任务管理调度器现在总结一下怎么找到.
(我的这个是英文操作系统)
在控制面板中输入schedule关键字,然后就就可以自动检索出来了.(我记得中文操作系统是输入"计划任务"或者"任务计划"也能找到)
点击之后:
创建一个任务点击右侧的"Create Task..."
通过这个面板创建一个任务.
本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/4221710.html,如需转载请自行联系原作者
-
php任务管理器 —— Jobby
2018-01-11 18:36:00任务的执行计划时间表设置与crontab的时间表设置语法一致 在指定的时间内只会运行一个任务 邮件告警异常退出任务 在root权限下可以控制任务的执行用户 指定主机名运行 转载于:...转载于:https://www.cnblogs.com/lglblogadd/p/8269978.html
-
excel任务日期管理器
2020-04-25 13:11:16如图,这个我在微软官方提供的模块做了一些小的修改,让其更符合项目的使用。 设计思路没有用到VBA,主要操作重点是名称和条件格式,配合公式就可以...至于日期的的格式只显示了日不显示月份,利用了设置单元格格... -
用任务管理器揪出暗藏的木马
2009-10-10 10:07:01用任务管理器揪出暗藏的木马 Windows任务管理器是大家对进程进行管理的主要工具,在它的“进程”选项卡中能查看当前系统进程信息。在默认设置下,一般只能看到映像名称、用户名、CPU占用、内存使用等几项,而更... -
如何应用任务管理器查杀木马病毒
2009-02-26 18:17:00Windows任务管理器是大家对进程进行管理的主要工具,在它的“进程”选项卡中能查看当前系统进程信息。在默认设置下,一般只能看到映像名称、用户名、CPU占用、内存使用等几项,而更多如I/O读写、虚拟内存大小等信息... -
PVFS2 源代码分析之输入输出src/io/job/job-time-mgr任务时间管理器
2010-07-27 10:32:00PVFS2发布的某些类型的任务(job)设置了超时,需要任务时间管理器(job time manager)统一记录和管理这些任务。 -
HorizonLite:Horizon Lite是一个基于条纹的简单任务管理器,适用于Android-源码
2021-02-18 11:05:37Horizon Lite是适用于Android的基于条纹的简单任务管理器 它通过使用户今天输入任务并完成任务以保持成绩,从而养成良好的习惯。 一旦添加了任务,便无法将其删除,因此必须完成任务,否则可能会失去连胜。 随着... -
【★用任务管理器揪出暗藏的木马★】
2013-07-16 14:30:32Windows任务管理器是大家对进程进行管理的主要工具,在它的“进程”选项卡中能查看当前win2003系统下载系统进程信息。在默认设置下,一般只能看到映像名称、用户名、CPU占用、内存使用等几项,而更多如I/O读写、虚拟... -
用任务管理器揪出暗藏的***
2009-09-07 21:16:26Windows任务管理器是大家对进程进行管理的主要工具,在它的“进程”选项卡中能查看当前系统进程信息。在默认设置下,一般只能看到映像名称、用户名、CPU占用、内存使用等几项,而更多如I/O读写、虚拟内存大小等信息... -
类似任务管理器的性能图表显示
2009-06-23 19:53:00int posline[26];BOOL OnInitDialog(){ CDialog::OnInitDialog(); posline[0]=16; for(int i=1;i posline[i]=posline[i-1]+25; SetTimer(1,1000,NULL);//设置绘图时间为1秒 return TRUE; } void CCtDlg::OnP -
解决Oracle10修改机器名后oracledbconsoleorcl服务无法启动的问题(即安装完oracle11g并设置任务管理器服务...
2019-10-31 09:16:50以前不经意修改了电脑的机器名,在安装了oracleh后第一次开机出现了oracle无法启动的问题,再次重启后设置为开机自启动的oracledbconsoleorcl服务又能自己启动了,觉得很纳闷。每次重新启动太麻烦,也不是解决问题的... -
quarz设置定时器任务的有效时间段_go-zero 如何应对海量定时/延迟任务?
2020-12-27 18:08:13一个系统中存在着大量的调度任务,同时调度任务存在时间的滞后性,而大量的调度任务如果每一个都使用自己的调度器来管理任务的生命周期的话,浪费cpu的资源而且很低效。 本文来介绍 go-zero 中 延迟操作,它可能让... -
查找任务管理器中w3wp.exe对应站点
2008-07-30 10:58:00有时发现有一个"w3wp.exe"的CPU占用率持续高达80%左右.....2、设置应用程序池的回收时间,默认为1720小时,可以根据情况修改。同时,设置同时运行的w3wp进程数目为1。再设置当内存或者cpu占用超过多少,就自动... -
android在手机桌面弹出一个提醒activity,finish掉后。从任务管理器进去还是这个提醒activity
2016-12-31 14:57:24最近做一个天气提醒的东西,设置时间后,在手机桌面弹出提醒的activity,finish掉它后打开任务管理。进入app。还是现实这个activity,如果finish掉后直接点击进入则正常。 -
定时任务的设置&使用
2019-04-28 15:56:49在哪个时间段启动定时任务,让它自动的去帮忙处理些事情,这些都可以靠自己去设置和定义,和JavaScript中的定时器类似但却有趣的多。那么,定时任务是怎么设置的呢? 首先我们需要用到一个插件:FluentScheduler,先... -
java定时任务管理_Quartz+Spring Boot实现动态管理定时任务
2021-02-12 23:42:12项目实践过程中碰到一个动态管理定时任务的需求:针对每个人员进行信息的定时更新,具体更新...设置一个统一的任务管理器,专门负责动态任务的增删改查。POM依赖xsi:schemaLocation="http://maven.apache.org/POM/4... -
Task Coach 个人任务管理 v1.3.22
2019-10-31 21:50:06使用 Task Coach 这个简单的 Todo 管理器,相信能让你的工作更加井井有条。目前该软件所具备的功能有:添加、删除、修改任务或子任务允许对任务的描述以及各种时间上的安排任务的分类保存过滤器自动保存等 -
2.7.9.54 按启动器设置后启动器响应慢
2021-01-06 18:32:52<div><p>按下启动器设置后,启动器没有任何反应,...只能任务管理器结束或者等待很长时间才有响应 希望能够优化相关代码!</p><p>该提问来源于开源项目:huanghongxun/HMCL</p></div>
-
Java杂项系列--函数式接口(java.util.function包)
-
标签组件-源码
-
AndroidTouch事件传递机制解析
-
MySQL NDB Cluster 负载均衡和高可用集群
-
spring-boot 2.4.2 bug resources目录文件夹新增 配置 不会编译到target目录中 升级2.4.3 解决
-
深究字符编码的奥秘,与乱码说再见
-
MaxScale 实现 MySQL 读写分离与负载均衡
-
HTML
-
个人学习记录-AD2021
-
2021 年该学的 CSS 框架 Tailwind CSS 实战视频
-
博客-spr21-源码
-
MySQL 性能优化(思路拓展及实操)
-
烧烤-源码
-
看板项目-源码
-
【Python-随到随学】FLask第二周
-
Android后台开发通
-
帮朋友分析并讲解的python虚拟环境-导入使用别人的代码.mp4
-
manuhg.github.io:一个非常简单的投资组合网站-源码
-
Feign 基本使用
-
travizzle.github.io:学习足够CSS和布局危险的示例网站-源码