定时任务

Quartz 在某一天的某一秒执行一个任务

配置

1
2
3
4
5
6
7
<!-- pom.xml -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
<type>jar</type>
</dependency>
1
2
3
4
; job.properties
a.job=com.lyloou.FeeJob
a.cron=0 0/1 * * * ?
a.enable=true

更多 cron 配置,看 链接

Quartz 在某一天的某一秒执行一个任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// TimeUtils.java
public class TimeUtils {
// 1月中的15号,0点整
public static Timestamp getMonthDay15(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
}

// FeeJob.java
/**
* 入账任务
*
* @author lyloou
*/
public class FeeJob implements Job {

private final Log logger = Logger.get(FeeJob.class);

@Override
public void execute(JobExecutionContext jec) {
// 1分钟执行一次
Date date = new Date();
if (TimeUtils.getMonthDay15(date).getTime() != date.getTime()) {
return;
}

long t1 = System.currentTimeMillis();
// do your job here
long spend = System.currentTimeMillis() - t1;
logger.info(String.format("%s|执行xxx任务,耗时%s:", getClass().getName(), spend));
}
}