This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

The scheduled task requirement is a common feature provided by SpringMVC, but it is particularly cumbersome to configure. This article shows you how to configure and use scheduled tasks.

1. Configure a scheduled task

The following configuration needs to be added to the spring-MVC configuration file.

    <task:executor id="executor" pool-size="10" queue-capacity="128" />
	<task:scheduler id="scheduler" pool-size="10" />
	<task:annotation-driven executor="executor"
		scheduler="scheduler" proxy-target-class="true" />
Copy the code

Note: The following header information needs to be added to the XML reference, otherwise an exception will be reported.

<! Task thread pool --> <! -- Number of task executor threads (number of threads a task needs to execute concurrently) --> XMLNS :task="http://www.springframework.org/schema/task <! - the task scheduler thread count (according to the number of timer Settings) -- - > http://www.springframework.org/schema/task <! - enable the annotation to identify @ Scheduled annotations - > http://www.springframework.org/schema/task/spring-task-3.0.xsdCopy the code

To use it, just use the @scheduled annotation, which can be written as follows.

@Component
public class SchedulingConfigN {
private static final Logger LOG = LoggerFactory.getLogger(SchedulingConfigN.class);
	@Scheduled(cron = "0/20 * * * *?")/from the first0Seconds start, every other20The second execution is performed once before and after the second minute hour day month year// If it is executed once an hour, it is 0 0 */1 * *?
                                                            	0 0 11 * *?
	public void scheduing() {
		System.out.println("Scheduled task start 1111"); }}Copy the code

2. Obtain the execution frequency of scheduled tasks from the database

One problem with the above configuration is that if you want to change the frequency of execution, you need to change the code and then package it to take effect. Is it possible to configure the execution frequency in the database and change it by modifying the database? The code is provided below for your reference.

@Configuration
@EnableScheduling
public class JobConfig implements SchedulingConfigurer {

    @Autowired
    @Qualifier("timingTasks")
    TimingTasks timingTasks;

    // Obtain the time service of the scheduled task
    @Bean(name={"timingTasks"})
    public TimingTasks timingTasks() {
        return new TimingTasks();
    }

    /** * Performs scheduled tasks. */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
            // 1. Add task content (Runnable)
            () -> System.out.println(Execute scheduled task 2: + LocalDateTime.now().toLocalTime()),
            // 2. Set the execution cycle (Trigger)
            triggerContext -> {
                // 2.1 Obtain the execution period from the database
                System.out.println("Commence execution");
                List<SaleProductFileBO> resultBO = timingTasks.selectTimingTasks(Long.valueOf(2));
                // Gets the time of the scheduled task
                String cron = resultBO.get(0).getFiles();
                // 2.3 Return to Execution Period (Date)
                return newCronTrigger(cron).nextExecutionTime(triggerContext); }); }}Copy the code

You are advised to learn about xxL-job if the production environment requires xxL-job. Open source and rave reviews.