Springboot Scheduled Tasks

Source: Internet
Author: User

(1) Explanation of ideas;

(a) First of all, here we need to re-recognize a class Threadpooltaskscheduler: Thread pool Task scheduling class, capable of opening thread pools for task scheduling.

(b) The Threadpooltaskscheduler.schedule () method creates a timing plan scheduledfuture, which requires two parameters to be added in this method, Runnable (Thread interface Class) and Crontrigger (timed task trigger)

(c) There is a cancel in the Scheduledfuture to stop the scheduled task.

(2) Code parsing;

According to the above thinking analysis, we can easily know how to encode, first provide the following code:

Package com.kfit.task;

import java.util.Date;

import java.util.concurrent.ScheduledFuture;

import org.springframework.beans.factory.annotation.Autowired;

import Org.springframework.context.annotation.Bean;

import Org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import Org.springframework.scheduling.support.CronTrigger;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.RequestMapping;

import Org.springframework.web.bind.annotation.RestController;

/**

* @author Angel--Guardian Angel

* @version v.0.1

* @date April 6, 2017

*/

@RestController

@Component

Public class Dynamictask {

@Autowired

Private Threadpooltaskscheduler Threadpooltaskscheduler;

Private Scheduledfuture<?> future;

@Bean

Public Threadpooltaskscheduler Threadpooltaskscheduler () {

return New Threadpooltaskscheduler ();

}

@RequestMapping ("/startcron")

Public String Startcron () {

Future = Threadpooltaskscheduler.schedule (new myrunnable (), new Crontrigger ("0/5 * * * * *"));

System. out. println ("Dynamictask.startcron ()");

return "Startcron";

}

@RequestMapping ("/stopcron")

Public String Stopcron () {

if (Future! = null) {

Future.cancel (true);

}

System. out. println ("Dynamictask.stopcron ()");

return "Stopcron";

}

@RequestMapping ("/changecron10")

Public String StartCron10 () {

Stopcron ();//Stop First, turn on.

Future = Threadpooltaskscheduler.schedule (new myrunnable (), new Crontrigger ("*/10 * * * * *"));

System. out. println ("Dynamictask.startcron10 ()");

return "ChangeCron10";

}

Private class Myrunnable implements Runnable {

@Override

Public void Run () {

System. out. println ("DynamicTask.MyRunnable.run ()," + new Date ());

}

}

}

(a) We first have a class dynamictask;
(b) Defines two variables, Threadpooltaskscheduler and future where the future is the return value of the Treadpooltaskscheduler execution method schedule, mainly for the stop of the scheduled task.
(c) The method of writing the start Timer startcron ();
(d) Write the Stop Method Stopcron (), where the code, you need to be aware of the need to judge the future is null, otherwise it is easy to throw nullpointerexception;
(e) Write a method to modify the timing task execution Cycle ChangeCron10 (), the principle here is to close the timer before the innovation in creating a new timer.

(3) Modify the timing task execution cycle special description;

In the previous blog, we used a way to modify the value of the cron parameter by the way of global variables, then we can do so here, here simply provide the next idea, everyone to achieve.

Note the schedule () second parameter in Threadpooltaskscheduler supports trigger:

Threadpooltaskscheduler.schedule (Runnable arg0, Trigger arg1)

Then we can define a trigger, and then dynamically modify it, here is the core code as follows:

Private String cronstr = "*/5 * * * * *";

@RequestMapping ("/startcron1")

Public String StartCron1 () {

System. out. println ("StartCron1 >>>>");

Threadpooltaskscheduler.schedule (new myrunnable (), new Trigger () {

@Override

Public Date nextexecutiontime (Triggercontext triggercontext) {

return New Crontrigger (CRONSTR). Nextexecutiontime (Triggercontext);

}

});

System. out. println ("StartCron1 <<<<");

return "StartCron1";

}

Springboot Scheduled Tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.