java开源定时任务管理_高精度、可用的定时任务管理工具 cknit 开源啦

cknit

cknit 是一款开源高可用定时多任务管理工具,定时精度为秒级别 ( 相比cron增加了秒的取值 ),能够确保高效、稳定的处理多任务。 定时精度随任务量的变化如下所示 ( 测试平台OSX ):

任务数量

定时精度偏差

1000

0.01s

100000

1s

支持平台

目前支持 Linux、mac 两大平台,mac 平台使用 select 系统调用,Linux平台使用 Posix (timer),因此 Linux 平台性能比 mac 平台稍高,任务调度精度更佳

时间间隔格式

标准格式:

* * * * * * *

含义

取值范围

特殊值含义

1

0-60

正常含义

2

0-59

正常含义

3

0-23

0:表示午夜12点

4

1-31

正常含义

5

0-11

0:表示一月

6

0-6

0:表示周日

可选模式:

/ : 斜线表示每隔多少区间,例如:

*/3 * * * * *

表示每隔三秒执行一次任务

, :逗号表示可选值,例如:

*/3 1,2,5,10 * * * *

表示在第1、2、5、10分钟每隔3秒执行一次任务

- :表示区间范围,区间取值是包含边界值,例如:

* 1-10,15,30-35 * * * *

表示在第1到10分钟、15分钟、30到35分钟的每一秒都执行一次任务

上面三种模式可以任意组合

设计架构

c8ab0754104dc272353d646d74462778.png

安装

cknit 采用 cmake 编译系统,因此需要目标机器安装 cmake 3.13 及以上版本

1、下载源码

git clone https://gitee.com/josinli/cknit.git

2、编译

mkdir build

cd build

cmake ..

make && make install

cknit

APIs管理

安装完成后,访问:

http://127.0.0.1:9898

响应如下:

{

"message": "Welcome use cknit",

"code": "ok",

"version": "1.0",

"port": 9898,

"APIs": [

{

"name": "Get all monitors tasks",

"method": "GET",

"protocol": "HTTP/1.1",

"url": "http://127.0.0.1:9898/monitors"

},

{

"name": "Add one monitors tasks",

"method": "POST",

"protocol": "HTTP/1.1",

"url": "http://localhost:9898/monitors"

},

{

"name": "Modify one monitors tasks",

"method": "PUT",

"protocol": "HTTP/1.1",

"url": "http://localhost:9898/monitors"

}

]

}

API: 获取当前所有的任务

GET http://127.0.0.1:9898/monitors

响应回答如下:

[

{

"command": "php ~/Desktop/index.php",

"period": "* 1,2,3,10-20 * * * *",

"id": 1,

"status": 0

},

{

"command": "php ~/Desktop/index.phpd",

"period": "* * * * * * */2"

}

]

API: 在线添加任务

POST http://127.0.0.1:9898/monitors

{

"command": "php ~/Desktop/index.php",

"period": "* * * * * * */2"

}

响应回答如下:

{

"message": "Success",

"code": "true",

"operation": "Add task"

}

API: 在线修改已存任务(id是系统自动分配的)

PUT http://127.0.0.1:9898/monitors

{

"id": 998,

"data": {

"status":0,

"period": "* * * 11 * */2",

}

}

响应回答如下:

{

"message": "Success",

"code": "true",

"operation": "Modify task"

}

JAVA精确定时器,利用系统时间,使长期工作的误差稳定。 功能: ·可定时启动任务或直接启动任务 ·重复启动任务(时间间隔可在任务线程中改变,范围大于100ms,否则精度降低) 引用列表: ·import psn.razerpen.thread.AccuracyTimer; ·import psn.razerpen.thread.AccuracyTimerMission; ·import psn.razerpen.time.TimeStruct; 使用方法: //1·继承AccuracyTimerMission接口,创建一个类。 class MyTimer implements AccuracyTimerMission { //2·指定一个周期 int nDelay=1000; //3·重写run方法(如不需要使用新线程执行任务,也可留空) /** * 任务线程,本函数继承自Runnable */ @Override public void run() { System.out.println(new TimeStruct()); } //4·重写RunInCurrentThread(long nCurrentMilliSecond)方法。该方法接收当前时间,并返回下一次执行的时间。如果返回值不大于nCurrentMilliSecond则中止计时器。该方法必须重写。 /** * 接收当前时间的毫秒值,并返回下一次执行的毫秒值。如果返回的下一个时间早于当前时间,则退出 */ @Override public long RunInCurrentThread(long nCurrentMilliSecond) { return nCurrentMilliSecond+=nDelay; } } //5·创建主线程代码 public class TestTimer { public static void main(String[] args) throws InterruptedException { //6·创建一个AccuracyTimer对象,并指定一个任务。 AccuracyTimer at=new AccuracyTimer(new MyTimer()); //7·(可选)如果不需要在新线程中启动任务,则写 // at.SetNewThreadEnabled(false); //否则不写或者写 // at.SetNewThreadEnabled(true); //8·(可选)设定第一次启动的时间点SetNextMissionTime/SetNextMissionMilliSecond或延迟时间SetNextMissionMilliSecondFromNow //设置为当前这一分钟的第59秒后启动(不写此行则表示直接启动) at.SetNextMissionTime(Integer.MIN_VALUE, -1, -1, -1, -1, 59, 0); //9·启动定时器 at.Start(); //10·主线程继续 for(int i=0;i<60;++i){ Thread.sleep(1000); } //11·结束定时器 at.End(); } } 详见sample.razerpen.thread包中TestTimer及各代码文件中注释
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值