使用允许动态更新的 Cron 安排任务
Schedule a task with Cron which allows dynamic update
我用的是sprint boot 1.3,spring 4.2
在这个class
@Service
public class PaymentServiceImpl implements PaymentService {
....
@Transactional
@Override
public void processPayment() {
List<Payment> payments = paymentRepository.findDuePayment();
processCreditCardPayment(payments);
}
}
我想每隔 x 时刻调用 processPayment。
这个x时刻是在数据库中设置的。
用户可以修改。
所以我想我不能使用注释。
我开始这个
@EntityScan(basePackageClasses = {MyApp.class, Jsr310JpaConverters.class})
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class MyApp {
@Autowired
private DefaultConfigService defaultConfigService;
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Bean
public TaskScheduler poolScheduler() {
SimpleAsyncTaskExecutor taskScheduler = new SimpleAsyncTaskExecutor();
DefaultConfigDto defaultConfigDto = defaultConfigService.getByFieldName("payment-cron-task");
String cronTabExpression = "0 0 4 * * ?";
if (defaultConfigDto != null && !defaultConfigDto.getFieldValue().isEmpty()) {
cronTabExpression = "0 0 4 * * ?";
}
appContext.getBean("scheduler");
taskScheduler.schedule(task, new CronTrigger(cronTabExpression));
return scheduler;
}
也许这不是个好方法
有什么建议吗?
如果我需要创建一个 属性 like
,不知道是否要获取我的上下文
@Autowired
ConfigurableApplicationContext context;
主要
之后
public static void main(String[] args) {
context = SpringApplication.run(MyApp.class, args);
}
看问题好像是想更新scheduler,不重启
您共享的代码仅确保从数据库中选取配置,但如果不重启应用程序,它不会刷新。
以下代码将使用 spring 上下文中可用的默认调度程序,并根据数据库中可用的 cron 设置动态计算下一次执行时间:
示例代码如下:
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
@SpringBootApplication
@EnableScheduling
public class Perses implements SchedulingConfigurer {
private static final Logger log = LoggerFactory.getLogger(Perses.class);
@Autowired
private DefaultConfigService defaultConfigService;
@Autowired
private PaymentService paymentService;
public static void main(String[] args) {
SpringApplication.run(Perses.class, args);
}
private String cronConfig() {
String cronTabExpression = "*/5 * * * * *";
if (defaultConfigDto != null && !defaultConfigDto.getFieldValue().isEmpty()) {
cronTabExpression = "0 0 4 * * ?";
}
return cronTabExpression;
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
paymentService.processPayment();
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cron = cronConfig();
log.info(cron);
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}
如果有人仍然遇到这个问题,更好的解决方案是在不做太多更改的情况下随时从数据库中获取价值,这将是每分钟 运行 cron 并在当前分钟与配置值增量之间获取 mod从数据库中,如果这个 mod 等于 0 意味着它必须 运行 就像它是一个数学倍数一样,所以如果你希望它每 5 分钟 运行 例如 delta 应该是5.
样本:
@Scheduled(cron = "0 */1 * * * *") //fire every minute
public void perform() {
//running
Integer delta = 5;//get this value from databse
Integer minutes = getField(Calendar.MINUTE)//calendar for java 7;
Boolean toRun = true;//you can also get this one from database to make it active or disabled
toRun = toRun && (minutes % delta == 0);
if (toRun && (!isRunning)) {
isRunning = true;
try {
//do your logic here
} catch (Exception e) { }
isRunning = false;
}
}
public Integer getField(int field) {
Calendar now = Calendar.getInstance();
if(field == Calendar.MONTH) {
return now.get(field)+ 1; // Note: zero based!
}else {
return now.get(field);
}
}
希望对您有所帮助 :D
我用的是sprint boot 1.3,spring 4.2
在这个class
@Service
public class PaymentServiceImpl implements PaymentService {
....
@Transactional
@Override
public void processPayment() {
List<Payment> payments = paymentRepository.findDuePayment();
processCreditCardPayment(payments);
}
}
我想每隔 x 时刻调用 processPayment。
这个x时刻是在数据库中设置的。 用户可以修改。
所以我想我不能使用注释。
我开始这个
@EntityScan(basePackageClasses = {MyApp.class, Jsr310JpaConverters.class})
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class MyApp {
@Autowired
private DefaultConfigService defaultConfigService;
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Bean
public TaskScheduler poolScheduler() {
SimpleAsyncTaskExecutor taskScheduler = new SimpleAsyncTaskExecutor();
DefaultConfigDto defaultConfigDto = defaultConfigService.getByFieldName("payment-cron-task");
String cronTabExpression = "0 0 4 * * ?";
if (defaultConfigDto != null && !defaultConfigDto.getFieldValue().isEmpty()) {
cronTabExpression = "0 0 4 * * ?";
}
appContext.getBean("scheduler");
taskScheduler.schedule(task, new CronTrigger(cronTabExpression));
return scheduler;
}
也许这不是个好方法
有什么建议吗?
如果我需要创建一个 属性 like
,不知道是否要获取我的上下文@Autowired
ConfigurableApplicationContext context;
主要
之后public static void main(String[] args) {
context = SpringApplication.run(MyApp.class, args);
}
看问题好像是想更新scheduler,不重启
您共享的代码仅确保从数据库中选取配置,但如果不重启应用程序,它不会刷新。
以下代码将使用 spring 上下文中可用的默认调度程序,并根据数据库中可用的 cron 设置动态计算下一次执行时间:
示例代码如下:
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
@SpringBootApplication
@EnableScheduling
public class Perses implements SchedulingConfigurer {
private static final Logger log = LoggerFactory.getLogger(Perses.class);
@Autowired
private DefaultConfigService defaultConfigService;
@Autowired
private PaymentService paymentService;
public static void main(String[] args) {
SpringApplication.run(Perses.class, args);
}
private String cronConfig() {
String cronTabExpression = "*/5 * * * * *";
if (defaultConfigDto != null && !defaultConfigDto.getFieldValue().isEmpty()) {
cronTabExpression = "0 0 4 * * ?";
}
return cronTabExpression;
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
paymentService.processPayment();
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cron = cronConfig();
log.info(cron);
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}
如果有人仍然遇到这个问题,更好的解决方案是在不做太多更改的情况下随时从数据库中获取价值,这将是每分钟 运行 cron 并在当前分钟与配置值增量之间获取 mod从数据库中,如果这个 mod 等于 0 意味着它必须 运行 就像它是一个数学倍数一样,所以如果你希望它每 5 分钟 运行 例如 delta 应该是5.
样本:
@Scheduled(cron = "0 */1 * * * *") //fire every minute
public void perform() {
//running
Integer delta = 5;//get this value from databse
Integer minutes = getField(Calendar.MINUTE)//calendar for java 7;
Boolean toRun = true;//you can also get this one from database to make it active or disabled
toRun = toRun && (minutes % delta == 0);
if (toRun && (!isRunning)) {
isRunning = true;
try {
//do your logic here
} catch (Exception e) { }
isRunning = false;
}
}
public Integer getField(int field) {
Calendar now = Calendar.getInstance();
if(field == Calendar.MONTH) {
return now.get(field)+ 1; // Note: zero based!
}else {
return now.get(field);
}
}
希望对您有所帮助 :D