clean history

This commit is contained in:
Looly
2019-08-14 10:02:32 +08:00
commit 6b011af032
1215 changed files with 159913 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package cn.hutool.cron.demo;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.cron.CronUtil;
public class AddAndRemoveMainTest {
public static void main(String[] args) {
CronUtil.setMatchSecond(true);
CronUtil.start(false);
CronUtil.getScheduler().clear();
String id = CronUtil.schedule("*/2 * * * * *", new Runnable() {
@Override
public void run() {
Console.log("task running : 2s");
}
});
ThreadUtil.sleep(3000);
CronUtil.remove(id);
Console.log("Task Removed");
id = CronUtil.schedule("*/3 * * * * *", new Runnable() {
@Override
public void run() {
Console.log("New task add running : 3s");
}
});
Console.log("New Task added.");
}
}

View File

@@ -0,0 +1,72 @@
package cn.hutool.cron.demo;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
/**
* 定时任务样例
*/
public class CronTest {
@Test
@Ignore
public void customCronTest() {
CronUtil.schedule("*/2 * * * * *", new Task() {
@Override
public void execute() {
Console.log("Task excuted.");
}
});
// 支持秒级别定时任务
CronUtil.setMatchSecond(true);
CronUtil.start();
}
@Test
@Ignore
public void cronTest() {
// 支持秒级别定时任务
CronUtil.setMatchSecond(true);
CronUtil.getScheduler().setDaemon(false);
CronUtil.start();
ThreadUtil.sleep(3000);
CronUtil.stop();
}
@Test
@Ignore
public void cronTest2() {
// 支持秒级别定时任务
CronUtil.setMatchSecond(true);
CronUtil.start();
ThreadUtil.sleep(30000);
}
@Test
// @Ignore
public void addAndRemoveTest() {
String id = CronUtil.schedule("*/2 * * * * *", new Runnable() {
@Override
public void run() {
Console.log("task running : 2s");
}
});
Console.log(id);
CronUtil.remove(id);
// 支持秒级别定时任务
CronUtil.setMatchSecond(true);
CronUtil.start();
}
}

View File

@@ -0,0 +1,19 @@
package cn.hutool.cron.demo;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.InvokeTask;
public class DeamonMainTest {
public static void main(String[] args) {
// 测试守护线程是否对作业线程有效
CronUtil.schedule("*/2 * * * * *", new InvokeTask("cn.hutool.cron.demo.TestJob.doWhileTest"));
// 当为守护线程时stop方法调用后doWhileTest里的循环输出将终止表示作业线程正常结束
// 当非守护线程时stop方法调用后不再产生新的作业原作业正常执行。
CronUtil.setMatchSecond(true);
CronUtil.start(true);
ThreadUtil.sleep(3000);
CronUtil.stop();
}
}

View File

@@ -0,0 +1,14 @@
package cn.hutool.cron.demo;
import cn.hutool.cron.CronUtil;
/**
* 定时任务样例
*/
public class JobMainTest {
public static void main(String[] args) {
CronUtil.setMatchSecond(true);
CronUtil.start(false);
}
}

View File

@@ -0,0 +1,36 @@
package cn.hutool.cron.demo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
/**
* 测试定时任务当触发到定时的时间点时执行doTest方法
*
* @author looly
*
*/
public class TestJob {
private String jobId = IdUtil.simpleUUID();
/**
* 执行定时任务内容
*/
public void doTest() {
// String name = Thread.currentThread().getName();
Console.log("Test Job {} running... at {}", jobId, DateUtil.now());
}
/**
* 执行循环定时任务测试在定时任务结束时作为deamon线程是否能正常结束
*/
public void doWhileTest() {
String name = Thread.currentThread().getName();
while (true) {
Console.log("Job {} while running...", name);
ThreadUtil.sleep(2000);
}
}
}

View File

@@ -0,0 +1,24 @@
package cn.hutool.cron.demo;
import java.util.concurrent.TimeUnit;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
/**
* 测试定时任务当触发到定时的时间点时执行doTest方法
*
* @author looly
*
*/
public class TestJob2 {
/**
* 执行定时任务内容
*/
public void doTest() {
Console.log("TestJob2.doTest开始执行……");
ThreadUtil.sleep(20, TimeUnit.SECONDS);
Console.log("延迟20s打印testJob2");
}
}

View File

@@ -0,0 +1,138 @@
package cn.hutool.cron.pattern;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.date.DateUtil;
import cn.hutool.cron.pattern.CronPattern;
/**
* 定时任务单元测试类
*
* @author Looly
*
*/
public class CronPatternTest {
@Test
public void matchAllTest() {
CronPattern pattern;
// 任何时间匹配
pattern = new CronPattern("* * * * * *");
Assert.assertTrue(pattern.match(DateUtil.current(false), true));
Assert.assertTrue(pattern.match(DateUtil.current(false), false));
}
@Test
public void matchAllTest2() {
// 在5位表达式中秒部分并不是任意匹配而是一个固定值
// 因此此处匹配就不能匹配秒
CronPattern pattern;
// 任何时间匹配
pattern = new CronPattern("* * * * *");
for(int i = 0; i < 1; i++) {
Assert.assertTrue(pattern.match(DateUtil.current(false), false));
}
}
@Test
public void cronPatternTest() {
CronPattern pattern;
// 12:11匹配
pattern = new CronPattern("39 11 12 * * *");
assertMatch(pattern, "12:11:39");
// 每5分钟匹配匹配分钟为[0,5,10,15,20,25,30,35,40,45,50,55]
pattern = new CronPattern("39 */5 * * * *");
assertMatch(pattern, "12:00:39");
assertMatch(pattern, "12:05:39");
assertMatch(pattern, "12:10:39");
assertMatch(pattern, "12:15:39");
assertMatch(pattern, "12:20:39");
assertMatch(pattern, "12:25:39");
assertMatch(pattern, "12:30:39");
assertMatch(pattern, "12:35:39");
assertMatch(pattern, "12:40:39");
assertMatch(pattern, "12:45:39");
assertMatch(pattern, "12:50:39");
assertMatch(pattern, "12:55:39");
// 2:01,3:01,4:01
pattern = new CronPattern("39 1 2-4 * * *");
assertMatch(pattern, "02:01:39");
assertMatch(pattern, "03:01:39");
assertMatch(pattern, "04:01:39");
// 2:01,3:01,4:01
pattern = new CronPattern("39 1 2,3,4 * * *");
assertMatch(pattern, "02:01:39");
assertMatch(pattern, "03:01:39");
assertMatch(pattern, "04:01:39");
// 08-07, 08-06
pattern = new CronPattern("39 0 0 6,7 8 *");
assertMatch(pattern, "2016-08-07 00:00:39");
assertMatch(pattern, "2016-08-06 00:00:39");
// 别名忽略大小写
pattern = new CronPattern("39 0 0 6,7 Aug *");
assertMatch(pattern, "2016-08-06 00:00:39");
assertMatch(pattern, "2016-08-07 00:00:39");
pattern = new CronPattern("39 0 0 7 aug *");
assertMatch(pattern, "2016-08-07 00:00:39");
// 星期四
pattern = new CronPattern("39 0 0 * * Thu");
assertMatch(pattern, "2017-02-09 00:00:39");
assertMatch(pattern, "2017-02-09 00:00:39");
}
@Test
public void CronPatternTest2() {
CronPattern pattern = new CronPattern("0/30 * * * *");
Assert.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:00:00").getTime(), false));
Assert.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:30:00").getTime(), false));
pattern = new CronPattern("32 * * * *");
Assert.assertTrue(pattern.match(DateUtil.parse("2018-10-09 12:32:00").getTime(), false));
}
@Test
public void patternTest() {
CronPattern pattern = new CronPattern("* 0 4 * * ?");
assertMatch(pattern, "2017-02-09 04:00:00");
assertMatch(pattern, "2017-02-19 04:00:33");
// 6位Quartz风格表达式
pattern = new CronPattern("* 0 4 * * ?");
assertMatch(pattern, "2017-02-09 04:00:00");
assertMatch(pattern, "2017-02-19 04:00:33");
}
@Test
public void rangePatternTest() {
CronPattern pattern = new CronPattern("* 20/2 * * * ?");
assertMatch(pattern, "2017-02-09 04:20:00");
assertMatch(pattern, "2017-02-09 05:20:00");
assertMatch(pattern, "2017-02-19 04:22:33");
pattern = new CronPattern("* 2-20/2 * * * ?");
assertMatch(pattern, "2017-02-09 04:02:00");
assertMatch(pattern, "2017-02-09 05:04:00");
assertMatch(pattern, "2017-02-19 04:20:33");
}
/**
* 表达式是否匹配日期
*
* @param pattern 表达式
* @param date 日期,标准日期时间字符串
*/
private void assertMatch(CronPattern pattern, String date) {
Assert.assertTrue(pattern.match(DateUtil.parse(date).getTime(), false));
Assert.assertTrue(pattern.match(DateUtil.parse(date).getTime(), true));
}
}

View File

@@ -0,0 +1,48 @@
package cn.hutool.cron.pattern;
import java.util.Date;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.date.DateUtil;
public class CronPatternUtilTest {
@Test
public void matchedDatesTest() {
//测试每30秒执行
List<Date> matchedDates = CronPatternUtil.matchedDates("0/30 * 8-18 * * ?", DateUtil.parse("2018-10-15 14:33:22"), 5, true);
Assert.assertEquals(5, matchedDates.size());
Assert.assertEquals("2018-10-15 14:33:30", matchedDates.get(0).toString());
Assert.assertEquals("2018-10-15 14:34:00", matchedDates.get(1).toString());
Assert.assertEquals("2018-10-15 14:34:30", matchedDates.get(2).toString());
Assert.assertEquals("2018-10-15 14:35:00", matchedDates.get(3).toString());
Assert.assertEquals("2018-10-15 14:35:30", matchedDates.get(4).toString());
}
@Test
public void matchedDatesTest2() {
//测试每小时执行
List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 * * *", DateUtil.parse("2018-10-15 14:33:22"), 5, true);
Assert.assertEquals(5, matchedDates.size());
Assert.assertEquals("2018-10-15 15:00:00", matchedDates.get(0).toString());
Assert.assertEquals("2018-10-15 16:00:00", matchedDates.get(1).toString());
Assert.assertEquals("2018-10-15 17:00:00", matchedDates.get(2).toString());
Assert.assertEquals("2018-10-15 18:00:00", matchedDates.get(3).toString());
Assert.assertEquals("2018-10-15 19:00:00", matchedDates.get(4).toString());
}
@Test
public void matchedDatesTest3() {
//测试最后一天
List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 L * *", DateUtil.parse("2018-10-30 23:33:22"), 5, true);
Assert.assertEquals(5, matchedDates.size());
Assert.assertEquals("2018-10-31 00:00:00", matchedDates.get(0).toString());
Assert.assertEquals("2018-10-31 01:00:00", matchedDates.get(1).toString());
Assert.assertEquals("2018-10-31 02:00:00", matchedDates.get(2).toString());
Assert.assertEquals("2018-10-31 03:00:00", matchedDates.get(3).toString());
Assert.assertEquals("2018-10-31 04:00:00", matchedDates.get(4).toString());
}
}

View File

@@ -0,0 +1,17 @@
#------------------------------------------------------------------
# 定时任务配置文件
# 定时任务表达分为以下几种情况:
# 1. 表达式为5位此时兼容Linux的Crontab模式第一位匹配分此时如果为秒匹配模式则秒部分为固定值取决于加入表达式时当前时间秒数
# 2. 表达式为6位此时兼容Quartz模式第一位匹配秒但是只有秒匹配模式时秒部分定义才有效
# 3. 表达式为7位此时兼容Quartz模式第一位匹配秒最后一位匹配年
#------------------------------------------------------------------
# cn.hutool.cron.demo.TestJob.doTest = */1 * * * * *
[cn.hutool.cron.demo]
# 6位表达式在秒匹配模式下可用此处表示每秒执行一次
# TestJob.doTest = */1 * * * * *
# 5位表达式在分匹配模式下可用此处表示每分钟执行一次
# 如果此时为秒匹配模式,则秒部分为固定数字(此秒取决于加入表达式当前时间的秒数)
TestJob.doTest = 0/30 * 8-18 * * ?
TestJob2.doTest = */3 * * * * *