这是我的预定任务类
@Component
public class ScheduledTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTask.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron = "${scheduling.job.cron}")
public void reportCurrentTime() {
LOGGER.info("The time is now {}", dateFormat.format(new Date()));
}
}
目前,我正在从application.properties文件加载cronjob语法。但我想在Spring启动应用程序启动时从SQL数据库加载cron语法。这是我的数据库脚本,它具有cronjob语法
CREATE TABLE tbl_configuration_details
(
config_id int NOT NULL,
schedule_time int NOT NULL,
schedule_time_format nvarchar(10) NOT NULL,
data_ttl int NOT NULL,
data_ttl_format nvarchar(10) NOT NULL,
cron_job_syntax nvarchar(255) NOT NULL,
created_timestamp datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_by int NOT NULL,
modified_timestamp datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
modified_by int NOT NULL,
PRIMARY KEY (config_id)
);
答案 0 :(得分:0)
步骤1:创建bean以从数据库
获取cron语法std::unexpected()
步骤2:使用#在Schedule annotation
中获取cron语法的bean值@Bean
public String getCronSyntax() {
AppConfiguration appConfiguration = configurationService.getConfiguration();
String cronSyntax = appConfiguration.getCronJobSyntax();
LOGGER.info("cronsyntax: " + cronSyntax);
return cronSyntax;
}