当前位置:网站首页>[cloud native] use of Nacos taskmanager task management
[cloud native] use of Nacos taskmanager task management
2022-06-29 21:42:00 【Shizhenzhen's grocery store】
Task management class
because Nacos This is used in many places in TaskManager, So we need to know what this class is for , It will not be difficult to read the source code later ;
Say first conclusion :
TaskManager It can be regarded as a set of tasks to be executed , Used to handle tasks that must be performed successfully Single threaded processing of tasks , Ensure that the task is successfully handled ; If the execution fails , The task will be put back into the collection for the next consumption ;
AbstractTask
AbstractTask It's an abstract class , All tasks that need to be executed continue with this class ; This class mainly provides the data and methods needed to perform tasks ; for example
/* The interval between two processes of a task , In milliseconds */
private long taskInterval;
/* The last time the task was processed , In milliseconds */
private long lastProcessTime;
/* TaskManager Determine whether you need to deal with this Task, Subclasses can Override This function implements its own logic */
public boolean shouldProcess() {
return (System.currentTimeMillis() - this.lastProcessTime >= this.taskInterval);
}
TaskProcessor Task processor
TaskProcessor Is the task processor interface , It has a way
boolean process(String taskType, AbstractTask task);
Used to execute the corresponding AbstractTask The task class ; Different types of tasks , You can implement your own execution task logic ;
TaskManager Task management class
TaskManager It is a task management class ;
There are two properties in it to save the tasks to be consumed AbstractTask, And task execution TaskProcessor;
/** Tasks to be consumed AbstractTask**/
private final ConcurrentHashMap<String, AbstractTask> tasks = new ConcurrentHashMap<String, AbstractTask>();
/** Mission AbstractTask Corresponding task executor TaskProcessor**/
private final ConcurrentHashMap<String, TaskProcessor> taskProcessors =new ConcurrentHashMap<String, TaskProcessor>();
If taskProcessors No corresponding task executor was found in , Then it has a default actuator that will execute
/** Default actuator **/
private TaskProcessor defaultTaskProcessor;
Use cases
Nacos An important function of the configuration center module is , During initialization and every other period of time, all data will be stored in the database Dump To disk ;Dump It is a task class AbstractTask; We said that above
AbstractTask It is an information bearing object , Mainly for TaskProcessor Provide the data required for execution ; Let's see. DumpTask;
DumpTask

DumpTask Defines some of its own properties ; Look at other examples DumpAllTask、DumpAllBetaTask

These two task classes only define TASK_ID
Since there are DumpTask The task class , There must be a corresponding task processor class DumpProcessor;
DumpProcessor
DumpProcessor yes DumpTask The executor of the task ; Methods in actuators
public boolean process(String taskType, AbstractTask task)
The code is too long to be analyzed here , Its main operation is Save the configuration file to the local disk , And cache md5
See the article for details 【Nacos Configuration management of source code Four 】DumpService How to complete the configuration file Dump To disk
Corresponding DumpAllTask、DumpAllBetaTask The task executors of the task are DumpAllProcessor、DumpAllBetaProcessor
DumpAllTask Where the task triggers execution
It's on it DumpAllTask The definition and DumpAllTaskProcessor Definition of actuator ; How is it triggered after it is defined ?
DumpService initialization Dump Configuration information
This class is specialized in Dump Service class of configuration information ; As mentioned above DumpAll It is here that is called ; Let's take a look at his main methods ;
@PostConstruct
public void init() {
DumpAllProcessor dumpAllProcessor = new DumpAllProcessor(this);
/** stay new This TaskManager Class time , A thread dedicated to the task has already started , But there is no task at this time Task Add in **/
dumpAllTaskMgr = new TaskManager( "com.alibaba.nacos.server.DumpAllTaskManager");
dumpAllTaskMgr.setDefaultTaskProcessor(dumpAllProcessor);
Runnable dumpAll = new Runnable() {
@Override
public void run() {
dumpAllTaskMgr.addTask(DumpAllTask.TASK_ID, new DumpAllTask());
}
};
/** Every time 10 Once per minute DumpAll operation **/
TimerTaskService.scheduleWithFixedDelay(dumpAll, initialDelay, DUMP_ALL_INTERVAL_IN_MINUTE,
TimeUnit.MINUTES);
}
DumpService This is called back during initialization init Method ;
1. First new One. DumpAllProcessor actuator ;
2. Again new One. TaskManager Task manager ; stay new This task manager , A thread will be started to execute all the tasks to be executed ; It's just that no tasks have been added at this time ;
3. Set the default executor of this task manager to DumpAllProcessor;
4. Every ten minutes TaskManager Add a DumpAllTask The task of ; Once added, it will be TaskManager Thread in processingThread perform process Method ;
边栏推荐
- leetcode:724. Find the central subscript of the array
- Threejs basic introduction
- 小型圖書館項目總結
- leetcode:724. 寻找数组的中心下标
- DB queries the database, merges two unrelated tables, adds non-existent fields, and assigns default values
- Is it safe to open a securities account in Caixue school?
- How to judge the quality of conductive slip ring from its appearance
- 美国隧道法ASTM E84 表面阻燃测试
- leetcode:238. 除自身以外数组的乘积
- Alibaba cloud released the atlas of China's robot industry (2022), 122 Pages pdf
猜你喜欢

How to evaluate iFLYTEK AI translation pen P20 series? Is it worth buying?

Sophon CE community edition goes online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool

Vipshop product details API interface (item_get- get vipshop product details interface), vipshop details API interface

Golang operation NSQ distributed message queue

leetcode:724. Find the central subscript of the array

Verilog implements DDS waveform generator module, which can realize adjustable frequency and phase, three waveforms

Matlab adds noise / disturbance to data

Implementation and Simulation of ads131a04 ADC Verilog

高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构

String字符串的存储原理
随机推荐
Bs-gx-017 online examination management system based on SSM
leetcode:307. 区域和检索 - 数组可修改
Which brokerage commission is the lowest and safest
Threejs basic introduction
Realize inotify and Rsync real-time backup
Golang operation etcd
ads131a04 ADC verilog实现及仿真
彩涂钢板密封板申请BS 476-3如何备样?
MES系统与ERP如何集成?本文告诉你答案
Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech
leetcode:724. 寻找数组的中心下标
[cloud native practice] kubesphere practice - Multi tenant system practice
STM32最小系统搭建(原理图)
直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述
leetcode:370. Interval addition
Navigation exercises [microcomputer principles] [exercises]
Gstreamer应用开发实战指南(五)
LSF bsub command
Sophon CE community edition goes online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
阿里巴巴店铺的所有商品API接口(item_search_shop-获得店铺的所有商品接口),阿里巴巴API接口