Assign Dynamic values in Annotation

35 Views Asked by At
@Component
@Slf4j
public class CronScheduler {

    @Autowired
    JobDetailsRepository jobDetailsRepository;

    @Autowired
    CostCodesController costCodesController;

    Map<String, String> jobSchedules = new HashMap<>();

    @Scheduled(cron = "${cron.expression}") // every 5 minutes will hit
    public void executeScheduledJobs() {
        log.info("inside executeScheduledJobs of CronScheduler.java");

        List<JobDetails> alljobDetails = jobDetailsRepository.findAll();

        for (JobDetails jobDetail : alljobDetails) {
            String cronExpression = "";
            cronExpression += "0 "; // Seconds
            cronExpression += jobDetail.getJobfrequency() + " "; // Minutes
            cronExpression += jobDetail.getJobTime() + " "; // Hours
            cronExpression += "* "; // Day of month
            cronExpression += "* "; // Month
            cronExpression += "? "; // Day of week

            jobSchedules.put(jobDetail.getJobName(), cronExpression);
        }
    }

    @Scheduled(cron = "#{jobSchedules.containsKey('importCostCodes') ? jobSchedules['importCostCodes'] : '0 0 0 ? * *'}")
    public void importCostCodes() {
        log.info("inside importCostCodes of CronScheduler.java");
        costCodesController.CostcenterExporting();
    }
}

is it possible to append the value to an annotation .

 @Scheduled(cron = "#{jobSchedules.containsKey('importCostCodes') ? jobSchedules['importCostCodes'] : '0 0 0 ? * *'}")

please help me.

0

There are 0 best solutions below