Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot : Getting @Scheduled cron value from database

I'm using Spring Boot and have issues scheduling a cron task using values existing in database.

For the time being, I'm reading values from properties file like below :

@Scheduled(cron= "${time.export.cron}")
public void performJob() throws Exception {
   // do something
}

This works nicely, but instead of getting values from properties file, I want to get them from database table. Is it possible and how ?

like image 788
Daniel Avatar asked May 09 '16 10:05

Daniel


People also ask

What is @scheduled in spring boot?

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. The @Scheduled annotation is used to trigger the scheduler for a specific time period.

What is @scheduled in spring?

Spring Core. Spring provides excellent support for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata.

Is @scheduled asynchronous?

There is no need to use @Async. Just use fixedRate attribute of @Scheduled instead of fixedDelay. Spring will make another invocation on the method after the given time regardless of any call is already being processed.


3 Answers

you can add a bean to get cron value from database in the SpringBootApplication main class or in any of the configuration class. Example code is below:

@Autowired
private CronRepository cronRepo;

@Bean
public int getCronValue()
{
    return cronRepo.findOne("cron").getCronValue();
}

you should create a table and provide suitable values in the database. After that you can provide the bean inside the @Scheduled. Example code is below:

@Scheduled(cron="#{@getCronValue}")

Hope it works for your issue.

like image 106
Vignesh Avatar answered Oct 06 '22 22:10

Vignesh


To achieve your goals you must configure your scheduler at runtime. It means you need to use more low-level scheduler API. Precisely when you have already prepared connect with your database you can configure your scheduler. I think you need to get rid of using @Scheduled annotation and manully manage your scheduler.

I think these topics can help to describe what I mean:

  1. How to change Spring's @Scheduled fixedDelay at runtime

  2. Scheduling a job with Spring programmatically (with fixedRate set dynamically)

However always you can use wild approaches where you would intercept the bean creation and replace original annotation on annotation with custom metadata but in order to implement it you must know many framework details and how @Scheduled annatation processor works.

like image 31
Alex Avatar answered Oct 06 '22 23:10

Alex


You need to load properties from the database table in which your value stored. and merge that db properties with application properties

    @Autowired
    private DataSource dataSource;

    @Autowired
    private DatabaseConfiguration configuration;

    @Bean(name = "propertyConfig")
    public DatabaseConfiguration getDatabaseConfiguration() {
        DatabaseConfiguration configuration = new DatabaseConfiguration(dataSource, "propertyTable", "key", "value");
        return configuration;
    }

    @Bean(name = "dbProperty")
    public Properties getDBProperties(){
        Properties properties = ConfigurationConverter.getProperties(configuration);
        return properties;
    }

For more help refer https://analyzejava.wordpress.com/2015/01/16/loading-configuration-properties-from-database-in-spring-based-application/

like image 1
NIrav Modi Avatar answered Oct 06 '22 23:10

NIrav Modi