我使用Spring 4.2的sprint boot 1.3
在这个类
@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;
}
也许这不是好方法。
有什么建议吗?
如果我需要创建类似这样的属性,不知道是否能获得我的背景信息
@Autowired
ConfigurableApplicationContext context;
然后在主要
public static void main(String[] args) {
context = SpringApplication.run(MyApp.class, args);
}
最佳答案
看问题似乎要更新调度程序,而无需重新启动。
您共享的代码仅确保从数据库中选择配置,但是如果不重新启动应用程序,它将不会刷新。
以下代码将使用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;
}
});
}
}
关于spring - 使用Cron安排任务,该任务允许动态更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37335351/