parent
018e27b6e1
commit
8e57ac8679
@ -1,14 +1,18 @@
|
||||
package ${basePackage}.frame.schedule;
|
||||
package xyz.wbsite.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.config.CronTask;
|
||||
import org.springframework.scheduling.config.Task;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunCronTask extends RunTask {
|
||||
|
||||
abstract String cron();
|
||||
|
||||
@Override
|
||||
public Task build() {
|
||||
return new CronTask(this, cron());
|
||||
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||
return poolTaskScheduler.schedule(this, new CronTrigger(cron()));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package xyz.wbsite.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunDelayRepeatTask extends RunTask {
|
||||
|
||||
public abstract Duration interval();
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||
return poolTaskScheduler.scheduleWithFixedDelay(this, interval());
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package xyz.wbsite.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunFixRepeatTask extends RunTask {
|
||||
|
||||
public abstract Duration interval();
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||
return poolTaskScheduler.scheduleAtFixedRate(this, interval());
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package ${basePackage}.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.config.FixedDelayTask;
|
||||
import org.springframework.scheduling.config.FixedRateTask;
|
||||
import org.springframework.scheduling.config.Task;
|
||||
|
||||
public abstract class RunRepeatTask extends RunTask {
|
||||
|
||||
public abstract void config(Config config);
|
||||
|
||||
public class Config {
|
||||
int interval = 10 * 60 * 1000;
|
||||
int initialDelay = 0;
|
||||
boolean fix = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task build() {
|
||||
Config config = new Config();
|
||||
config(config);
|
||||
|
||||
if (config.fix) {
|
||||
return new FixedRateTask(this, config.interval, config.initialDelay);
|
||||
} else {
|
||||
return new FixedDelayTask(this, config.interval, config.initialDelay);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue