我使用 sprint boot 1.3、spring 4.2
在这个班
@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);
}