简体   繁体   中英

Spring - run @Scheduled dynamically n times

How to run @Scheduled n times, based on number of companies I have in application? For example method sendMail should run every day but in different time for every company I have in application.

  1. Run method every day at X(based on information from database) for company A (based on information from database)

     @Scheduled(cron="#{@getCronValue}") public void sendMail() {... send mail for company A }
  2. Run method every day at Y(based on information from database) for company B (based on information from database)

N. Run n times (number of companies)

I don't think that @Scheduled is a direct solution to your problem. I would recommend the following approach:

  1. create some sort of persistence mechanism (DB table, collection, ...)
  2. store a "job object" for each company (include the company identifier and the time the job should be executed)
  3. use @Scheduled to find jobs that should be executed
  4. call sendMail(company) for each job

You could use existing solutions like https://github.com/kagkarlsson/db-scheduler or Quartz Scheduler for this as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM