简体   繁体   中英

Spring boot scheduling of different tasks at different intervals

I am using Spring boot to build all the back end REST API's and reactJS for front end. My web application allows us to run different tasks present on the UI. Which basically means, REST API is triggered with the parameters for that particular task being executed. All is fine when we execute it through UI, but if i want to add a schedule feature for some of these tasks then i am little confused as to how to proceed with it.

I have seen few examples within spring boot, but they have the condition to have void return type of the object. Whereas my objects return String or long in some instances. what can be the best way to schedule jobs like these where the API needs to be run for different paramters at different times?

use case :

  • there are different tasks in my application
  • each task has different parameters that triggers an API
  • each task can have different schedule times.

how can the above use cases be used and built a scheduler using a spring boot app?

Examples I have seen are :

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

 @SpringBootApplication
 @EnableScheduling
 public class Application {
public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class);
  }
}
 /***************************************************************/
 package hello;

 import java.text.SimpleDateFormat;
 import java.util.Date;

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;

 @Component
 public class ScheduledTasks {

 private static final Logger log = 
  LoggerFactory.getLogger(ScheduledTasks.class);

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
    log.info("The time is now {}", dateFormat.format(new Date()));
}
  }

You described concerns that are good fit for batching framework. Spring Batch is top framework for this from Spring Platform.

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