SpringBoot regular tasks, there is always a right for you

Foreword

  Timing task can help us deal with the task at a given time, such as your alarm clock will teach you to get up in the morning Yeah, like every morning to read his inaugural state of your table in the company staff, as if to leave, then the timing month after automatically deleted library ah (joke), and so on, following the introduction of three methods to create a cron job (in fact, are two, not the third).

Three ways

Use SpringBoot create a scheduled task is very simple, there are mainly three ways to create:

  • Annotation-based (@Scheduled)
  • Based Interface (SchedulingConfigurer) I believe we are very familiar with the former, but the actual use, we tend to want to read a specified time from the database to dynamically perform regular tasks, this time based on the timing task interface comes in handy.
  • Annotation-based multi-threaded regular tasks set

Based @Scheduled comment

The class is marked @Configuration configuration classes, automatically injected. @EnableScheduling open regular tasks.

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author: zp
 * @Date: 2019-09-28 17:08
 * @Description:
 */
@Configuration
@EnableScheduling
public class TaskBasedAnnotation {

    // 每两秒执行一次
    @Scheduled(cron = "*/2 * * * * ?")
    public void sayHello(){
        System.out.println("Hello, menmen!"+Thread.currentThread().getName());
    }

}
复制代码

Based SchedulingConfigurer Interface

import com.example.demojpa.dao.CronRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @author: zp
 * @Date: 2019-09-28 17:33
 * @Description:
 */
@Configuration
@EnableScheduling
public class TaskBasedInterface implements SchedulingConfigurer {

    /**
     * 这是JPA,Mapper可以注入Mapper文件
     * 都是为了从数据库读取配置 *3 * * * * ?
     */
    @Autowired
    CronRepository cronRepository;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addCronTask(()-> System.out.println("你好,门门!"+Thread.currentThread().getName()),cronRepository.getCron());
    }

}
复制代码

Asynchronous regular tasks

@EnableAsync open multiple threads. @Async mark it as an asynchronous task.

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author: zp
 * @Date: 2019-09-28 17:50
 * @Description:
 */
@Configuration
@EnableScheduling
@EnableAsync
public class AsyncTask {

    @Async
    @Scheduled(cron = "*/4 * * * * ?")
    public void message(){
        System.out.println("menmen ni hao !");
    }
}复制代码

file

You can see every thread is different. It can be used to perform the time-consuming task.

postscript

  The main purpose of this study is to exist in the database on thousands of smug words, and then combined with a messaging platform every morning I made a random, so sure are happy to die every day ~file

Guess you like

Origin juejin.im/post/5d9d7fc5e51d457805049722