tags: Problems encountered by daily development java
Spring frameworks come from 3.0, with task scheduling feature, it is a lightweight quartz, and it is convenient, simple, and does not need to rely on other JAR packets.
It is more convenient to use in SpringBoot, just need to add @Configuration and @Enables in the class head.
Then use @scheduled (cron = "cron =" 0 0/5 * * * *? ") To be implemented in a simple scheduling task.
@Component
@Configuration // 1. Mainly used to mark the configuration class, both the effect of Component.
@EnableScheduling // 2. Open timemian
public class XXXJob {
// log
private Logger logger = LoggerFactory.getLogger(XXXJob.class);
@Scheduled(cron = "0 0/5 * * * ?")
public void excuteTask() {
logger.info("XXX Task, start!");
// DO execution business logic
logger.info("XXX Task, end!");
}
Based on fixed CRON expressions based on fixed CRON expressions, is there a simple implementation of dynamic configuration?
We can achieve a simple dynamic configuration method based on NACOS
First of all our dispatch tasks to implement the SchedulingConfigura interface
@Component
@Configuration
@EnableScheduling
// Class To implement the SchedulingConfigura interface
public class XXXTask implements SchedulingConfigurer {
/ / Method for implementing an interface
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// Perform a task Using Addtriggertask
taskRegistrar.addTriggerTask(
// The first method implements its own scheduling task business logic
() -> executeYearBill(), //Business logic
/ / Implement the trigger logic
triggerContext -> {
/ / Take dynamic cron expression from NACOS
String cron = nacosUtil.getXXXCron();/ / Get the configuration yourself to implement the NACOS method
/ / Judge whether NACOS has a configured cron expression, if not given a default configuration
if(StringUtils.isEmpty(cron)){
cron = "0/2 * * * * ?"; / / The default configuration is executed every 2 seconds
}
logger.info("Current trigger ..."+cron);
// This is obtained after the dynamic configuration is executed, perform the calculation of the next promotion time.
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
});
}
}
This method can simply dynamically implement NACOS configuration modifications and real-time cron expressions perform scheduling tasks.
However, there is still a small pit, which is that once the scheduled task is stopped, even if the dynamic changes are not used, because the task is not executed. If you want to dynamically start, you need to achieve a distributed scheduling platform such as XXL-JOB.
Maven dependence adds to add this dependence to dynamically monitor NACOS configuration file modification Starting class plus injection solution @EnableDynamicConfigEvent nacos + Timing task time to r...
1. On the scheduled task class, use @EnableScheduling, @Component annotation to start the scheduled task Use @Scheduled annotation on the method, where fixedRate means to execute at a specified time i...
1. Add @EnableScheduling annotation to the startup class 2. Implementation code @Service @AllArgsConstructor public class JobService {private final SysOrderService sysOrderService; private final SysTe...
Timed task-based on springBoot annotation @Scheduled Based on the annotation @Scheduled is single-threaded by default. When multiple tasks are started, the execution time of the task is affected by th...
Create a timer Using SpringBoot Based on the annotation to create a timing task is very simple, you can complete only a few lines of code. code show as below: CRON Tips: Cron expression parameters are...
Springboot is based on @scheduled annotations to realize timing tasks 1: Import POM dependence Introducing the Springboot Starter package Second: Startup class Enable timing tasks: Adjust the decompos...
Title: Detailed task is explained when playing springboot author:mmzsblog Original address:https://www.mmzsblog.cn/articles/2019/08/08/1565247960802.html Timed task application scenario Time to push m...
Today, Demo is suitable for simple business scenarios, which is very simple. 1. First create a SpringBoot project 2, add CRON expression parameters in the configuration file 3, test Results of the:...
I wrote a timed task with springboot two days ago. I want the system to do certain things regularly, and I hope it will be parallel. Then the @Scheduled annotation is used The code is as follows opera...
Continued from above, quartz uses version 2.2.1, 11 database tables, 1. The quartz.properties configuration file remains unchanged (same as above): 2. The application-quartz.xml configuration is as fo...