java定时器,每天执行一次/每隔多久执行一次
每天执行一次:package com.guomo.app.utils;import cn.hutool.http.HttpRequest;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.guomo.app.e
一:
每天执行一次:
package com.guomo.app.utils;
import cn.hutool.http.HttpRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guomo.app.entity.vo.GreentimeVo;
import javax.servlet.ServletContextListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
//定时器
public class DayInterval implements ServletContextListener {
public static void showDayTime() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(year, month, day, 15, 40, 00);//设置要执行的日期时间
Date defaultdate = calendar.getTime();
Timer dTimer = new Timer();
dTimer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("每日任务已经执行");
}
}, defaultdate , 24* 60* 60 * 1000);//24* 60* 60 * 1000
}
public static void main(String[] args) {
showDayTime();
}
}
每隔多久执行一次:
package com.guomo.app.utils;
//调用的工具
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Task3 {
public static void main(String[] args) {
/**
* Runnable:实现了Runnable接口,jdk就知道这个类是一个线程
*/
Runnable runnable = new Runnable() {
//创建 run 方法
public void run() {
// task to run goes here
System.out.println("Hello, stranger");
}
};
// ScheduledExecutorService:是从Java SE5的java.util.concurrent里,
// 做为并发工具类被引进的,这是最理想的定时任务实现方式。
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
// 10:秒 5:秒
// 第一次执行的时间为10秒,然后每隔五秒执行一次
service.scheduleAtFixedRate(runnable, 10, 5, TimeUnit.SECONDS);
}
}
二:
Springboot中有**@EnableScheduling**注解可以做到开启服务器后通过Springboot整合Schedule来实现一个定时任务。
@SpringBootApplication
//添加下面这个注解
@EnableScheduling
public class AppApplication{
public static void main(String[] args) {
AppApplication.run(AppApplication.class, args);
}
}
启动的时候一旦扫描到就@Component标志有定时任务存在,就会执行定时任务。
@Component
public class MyScheduledTask {
@Scheduled(cron = "5 0 0 * * ?")
public void scheduledTask1(){
System.out.println("定时任务1");
}
@Scheduled(initialDelay = 1000 * 10,fixedDelay = 1000 * 5)
public void scheduledTask2(){
System.out.println("任务2执行");
}
@Scheduled(initialDelay = 1000 * 10,fixedRate = 1000 * 5)
public void scheduledTask3(){
System.out.println("任务3执行时间:"+System.currentTimeMillis()%10000);
try {
Thread.sleep(2*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("任务3结束时间:"+System.currentTimeMillis()%10000);
}
}
定时参数:
Cron:
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2)Seconds Minutes Hours DayofMonth Month DayofWeek
字段 | 允许值 | 允许的特殊字符 |
---|---|---|
秒(Seconds) | 0~59的整数 | , - * / 四个字符 |
分(Minutes) | 0~59的整数 | , - * / 四个字符 |
小时(Hours) | 0~23的整数 | , - * / 四个字符 |
日期(DayofMonth) | 1~31的整数 | ,- * ? / L W C 八个字符 |
月份(Month) | 1~12的整数或者 JAN-DEC | , - * / 四个字符 |
星期(DayofWeek) | 1~7的整数或者 SUN-SAT (1=SUN) | , - * ? / L C # 八个字符 |
年(可选,留空)(Year) | 1970~2099 | , - * / 四个字符 |
解释:
(1) * 表示匹配该域的任意值。假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但是有种情况例外。如写法:13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发。
(3)-:表示范围。例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次 。
(4)/:表示起始时间开始触发,然后每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5),:表示列出枚举值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6)L:表示最后,只能出现在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 。
(8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
2、其他参数
(1)initialDelay:启动后多久开始执行,单位时毫秒
(2)fixedRate:下次执行时间,任务开始运行的时候就计时。
(3)fixedDelay:下次执行时间,fixedDelay等任务进行完了才开始计时
以下是网上摘抄的部分例子说明:
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
corn表达式格式:
秒 分 时 日 月 周 年(可选)。
“?”字符:表示不确定的值
“,”字符:指定数个值
“-”字符:指定一个值的范围
“/”字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m
“L”字符:用在日表示一个月中的最后一天,用在周表示该月最后一个星期X
“W”字符:指定离给定日期最近的工作日(周一到周五)
“#”字符:表示该月第几个周X。6#3表示该月第3个周五
例子:
每隔5秒执行一次:/5 * * * ?
每隔1分钟执行一次:0 /1 * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
每天的8点开始,2小时执行一次:0 0 8/2 * * ?
更多推荐
所有评论(0)