根据要求,我需要从数据库获取cron表达式并将其设置为
安排了注释的参数,但收到无效的Cron表达式错误。下边是
我的代码,仅当我在@Scheduled
参数中直接添加cron表达式时,它才起作用
@Component
@EnableScheduling
@Configuration
public class TicketReportScheduler
{
private static Log _log =
LogFactoryUtil.getLog(TicketReportScheduler.class);
@Autowired
NotificationUtility notificationUtility;
@Bean
public String getConfigRefreshValue() {
return "0 */1 * * * ?";
}
//@Scheduled(cron = "0 */1 * * * ?") - works
@Scheduled(cron = "#{@getConfigRefreshValue}") // error
public void demoServiceMethod() throws Exception {
_log.info("Cron job started " + new Date());
}
}
运行时出错:
我用Google搜索了很多,但是无法解决我的问题。按照要求,我必须从数据库传递cron表达式。对于POC,我创建了一个简单的方法并返回cron表达式,但是它不起作用,并且出现错误提示:
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'demoServiceMethod': Cron expression must consist of 6 fields (found 1 in "#{@getConfigRefreshValue}")