增强 时间表达式配置
This commit is contained in:
parent
d22238ce9e
commit
cf6c05b089
@ -5,7 +5,8 @@ import com.lxyer.timer.task.Job;
|
|||||||
import com.lxyer.timer.task.Task;
|
import com.lxyer.timer.task.Task;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -39,13 +40,13 @@ public class TimerTask implements Task {
|
|||||||
@Override
|
@Override
|
||||||
public void setScheduled(Scheduled scheduled) {
|
public void setScheduled(Scheduled scheduled) {
|
||||||
this.scheduled = scheduled;
|
this.scheduled = scheduled;
|
||||||
this.theTime = scheduled.theTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
this.theTime = Date.from(scheduled.theTime().atZone(ZoneId.systemDefault()).toInstant()).getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long nextTime(){
|
public long nextTime(){
|
||||||
LocalDateTime next = scheduled.nextTime();
|
LocalDateTime next = scheduled.nextTime();
|
||||||
this.theTime = next.toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
|
||||||
|
|
||||||
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
System.out.println("下次执行:"+ sdf.format(next.toInstant(ZoneOffset.of("+8")).toEpochMilli()));*/
|
System.out.println("下次执行:"+ sdf.format(next.toInstant(ZoneOffset.of("+8")).toEpochMilli()));*/
|
||||||
@ -54,7 +55,7 @@ public class TimerTask implements Task {
|
|||||||
@Override
|
@Override
|
||||||
public long theTime(){
|
public long theTime(){
|
||||||
LocalDateTime next = scheduled.theTime();
|
LocalDateTime next = scheduled.theTime();
|
||||||
this.theTime = next.toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
|
||||||
return theTime;
|
return theTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,44 +13,55 @@ public class ScheduledCycle implements Scheduled {
|
|||||||
private long period;
|
private long period;
|
||||||
private TemporalUnit unit = ChronoUnit.MILLIS;
|
private TemporalUnit unit = ChronoUnit.MILLIS;
|
||||||
|
|
||||||
/**
|
private ScheduledCycle() {
|
||||||
* 构造方法仅限类内部使用,为降低使用成本统一使用 of 的静态方法构建对象
|
|
||||||
* @param period
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public ScheduledCycle(long period) {
|
|
||||||
this.theTime = LocalDateTime.now().plus(period, ChronoUnit.MILLIS);
|
|
||||||
this.period = period;
|
|
||||||
}
|
}
|
||||||
@Deprecated
|
|
||||||
public ScheduledCycle(long period,TemporalUnit unit) {
|
public static Scheduled of(String periodCfg) {
|
||||||
this.theTime = LocalDateTime.now().plus(period, unit);
|
TemporalUnit unit = ChronoUnit.MILLIS;
|
||||||
this.period = period;
|
String endchar = "";
|
||||||
this.unit = unit;
|
long period;
|
||||||
}
|
|
||||||
@Deprecated
|
if (periodCfg.matches("^\\d+[y,M,d,H,m,s]$")) {
|
||||||
public ScheduledCycle(LocalDateTime startTime, long period) {
|
endchar = periodCfg.substring(periodCfg.length() - 1);
|
||||||
this.theTime = startTime;
|
period = Long.parseLong(periodCfg.substring(0, periodCfg.length() - 1));
|
||||||
this.period = period;
|
} else if (periodCfg.matches("^\\d+$")) {
|
||||||
}
|
period = Long.parseLong(periodCfg);
|
||||||
@Deprecated
|
if (period <= 0) {
|
||||||
public ScheduledCycle(LocalDateTime startTime, long period, TemporalUnit unit) {
|
throw new IllegalArgumentException(String.format("ScheduledCycle period config error: [%s]", periodCfg));
|
||||||
this.theTime = startTime;
|
}
|
||||||
this.period = period;
|
} else {
|
||||||
this.unit = unit;
|
throw new IllegalArgumentException(String.format("ScheduledCycle period config error: [%s]", periodCfg));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("y".equals(endchar)) unit = ChronoUnit.YEARS;
|
||||||
|
else if ("M".equals(endchar)) unit = ChronoUnit.MONTHS;
|
||||||
|
else if ("d".equals(endchar)) unit = ChronoUnit.DAYS;
|
||||||
|
else if ("H".equals(endchar)) unit = ChronoUnit.HOURS;
|
||||||
|
else if ("m".equals(endchar)) unit = ChronoUnit.MINUTES;
|
||||||
|
else if ("s".equals(endchar)) unit = ChronoUnit.SECONDS;
|
||||||
|
|
||||||
|
return of(period, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Scheduled of(long period) {
|
public static Scheduled of(long period) {
|
||||||
return new ScheduledCycle(period);
|
return of(period, ChronoUnit.MILLIS);
|
||||||
}
|
}
|
||||||
public static Scheduled of(long period,TemporalUnit unit) {
|
|
||||||
return new ScheduledCycle(period, unit);
|
public static Scheduled of(long period, TemporalUnit unit) {
|
||||||
|
LocalDateTime theTime = LocalDateTime.now().plus(period, unit);
|
||||||
|
return of(theTime, period, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Scheduled of(LocalDateTime startTime, long period) {
|
public static Scheduled of(LocalDateTime startTime, long period) {
|
||||||
return new ScheduledCycle(startTime, period);
|
return of(startTime, period, ChronoUnit.MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Scheduled of(LocalDateTime startTime, long period, TemporalUnit unit) {
|
public static Scheduled of(LocalDateTime startTime, long period, TemporalUnit unit) {
|
||||||
return new ScheduledCycle(startTime, period, unit);
|
ScheduledCycle scheduled = new ScheduledCycle();
|
||||||
|
scheduled.theTime = startTime;
|
||||||
|
scheduled.period = period;
|
||||||
|
scheduled.unit = unit;
|
||||||
|
return scheduled;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -101,10 +101,6 @@ public class ScheduledExpres implements Scheduled{
|
|||||||
while (theTime.isBefore(now)){
|
while (theTime.isBefore(now)){
|
||||||
theTime = carry("m");
|
theTime = carry("m");
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(cfg);
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
||||||
System.out.println("init:"+ sdf.format(this.theTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user