Spring-boot multi-threaded concurrent timing task solution

I just read the article about Spring Boot implementing scheduled tasks, and it feels pretty good. Spring Boot uses Spring's own Schedule to implement timing tasks very simply and conveniently. Share it with everyone here.

Enable caching annotations

@SpringBootApplication
@EnableScheduling //开启定时任务
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

Write timed tasks

@Component
public class ScheduledTasks {
  private Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
  // cron接受cron表达式,根据cron表达式确定定时规则
  @Scheduled(cron="0/5 * * * * ? ")  //每5秒执行一次 
  public void testCron() {
  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
   logger.info(sdf.format(new Date())+"*********每5秒执行一次");
  }
}

mission completed

Start the project, check the console print information, and find that the scheduled task has taken effect. The integration of spring boot and Scheduled is complete.

There is a problem

But then I found a problem. By testing several tasks at the same time, I found that all tasks are completed by the same thread in the same thread pool. In the actual development process, we certainly don't want all tasks to run in one thread.

@Scheduled(cron="0/1 * * * * ? ")  //每1秒执行一次 
public void testCron1() {
  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  logger.info(sdf.format(new Date())+"*********每1秒执行一次");
}
@Scheduled(cron="0/2 * * * * ? ")  //每2秒执行一次 
public void testCron2() {
  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  logger.info(sdf.format(new Date())+"*********每2秒执行一次");
}
@Scheduled(cron="0/3 * * * * ? ")  //每3秒执行一次 
public void testCron3() {
  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  logger.info(sdf.format(new Date())+"*********每3秒执行一次");
}
@Scheduled(cron="0/4 * * * * ? ")  //每4秒执行一次 
public void testCron4() {
  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  logger.info(sdf.format(new Date())+"*********每4秒执行一次");
}

solution

So, how to design it as multi-threaded to achieve concurrency? I have seen such solutions online. Implement the SchedulingConfigurer interface through the ScheduleConfig configuration file and rewrite the setSchedulerfang method. We tried to configure it.

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
 @Override
 public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
   taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
 }
}

Parameters supported by @Scheduled:

1.cron: cron expression, specifying the task to be executed at a specific time; 2.fixedDelay: Indicates how long after the last task execution is completed, the parameter type is long, and the unit is ms; 3.fixedDelayString: The meaning is the same as fixedDelay, but the parameter type 4.fixedRate: Indicates that the task is executed at a certain frequency, the parameter type is long, and the unit is ms; 5.fixedRateString: Same as fixedRate, but the parameter type is changed to String; 6.initialDelay: Indicates how long to delay For the first execution of the task, the parameter type is long, and the unit is ms; 7.initialDelayString: The meaning is the same as initialDelay, but the parameter type is changed to String; 8.zone: Time zone, the default is the current time zone, which is generally not used.

Cron expression example:

Execute every 5 seconds: */5 * * * * ? Execute every 1 minute: 0 */1 * * * ? Execute once every day at 23 o'clock: 0 0 23 * * ? Execute once every day at 1 o'clock in the morning: 0 0 1 * * ? Execute once at 1 am on the 1st of each month: 0 0 1 1 * ? Execute once at 23:00 on the last day of each month: 0 0 23 L * ? Execute once every Sunday at 1 am: 0 0 1 ? * L Execute once at 26, 29, and 33: 0 26,29,33 * * * ? Execute once every day at 0:00, 13:00, 18:00, and 21:00: 0 0 0,13,18,21 * * ?

In fact, don't worry if you don't know Cron expressions. There are many online Cron generators on the Internet. We can generate cron that meets the requirements through online generators, which is also very convenient.

Guess you like

Origin blog.csdn.net/qq_36647209/article/details/130382758