How to schedule several tasks in spring boot dynamically?

Asked

Viewed 316 times

3

I have a spring boot application and the same has several Jobs that will run from time to time and this time interval has to be recovered in the database.

In the Scheduledconfig class we have data recovery:

@Bean(name = "jobConfirmaPagamento")
@Primary
public String jobConfirmaPagamento(){
    logger.info("Consultando o tempo de execução para o rotina CONFIRMA-PAGAMENTO.");
    return configuracaoServico.consultarTempoExecucaoConfirmaPagamento();
}

@Bean(name = "jobLiberacaoPagamento")
@Lazy
public String jobLiberacaoPagamento() {
    logger.info("Consultando o tempo de execução para a rotina LIBERACAO-PAGAMENTO");
    return configuracaoServico.consultarTempoExecucaoLiberacaoPagamento();
}

@Bean(name = "jobSaldoProvisionado")
@Lazy
public String jobSaldoProvisionado() {
    logger.info("Consultando o tempo de execução para a rotina SALDO-PROVISIONADO");
    return configuracaoServico.consultarTempoExecucaoSaldoProvisionado();
}

@Bean(name = "jobSaldoDepositoJudicial")
@Lazy
public String jobSaldoDepositoJudicial() {
    logger.info("Consultando o tempo de execução para a rotina SALDO-DEPOSITO-JUDICIAL");
    return configuracaoServico.consultarTempoExecucaoSaldoDepositoJudicial();
}

But only jobConfirmaPackage is being fired.

1 answer

2

For this you can use Spel

I put a model down where I’m wearing @Scheduled for scheduling the job, in it I am injecting a bean containing the fetched property of the database containing the cron expression:

@Bean
public String getSchedulerConfigJobConfirmaPagamento() {
   return configRepository.findOne(Constants.SCHEDULER_JOB_CONFIRMA_PAGAMENTO).getConfigValue(); // Aqui será retornado sua String contendo o cron (0 12 * * MON por exemplo)
}

@Scheduled(fixedRateString = "#{@getSchedulerConfigJobConfirmaPagamento}")
public void runJobConfirmaPagamento() {
    ... // Aqui você possui a chamada para a lógica de seu job de fato
}

The idea is for you to have one bean for each cron setting, and use them accordingly for each activity

Browser other questions tagged

You are not signed in. Login or sign up in order to post.