当前位置:网站首页>[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 ;
边栏推荐
- API interfaces for all products in Alibaba stores (item_search_shop- obtain all product interfaces in the store), Alibaba API interfaces
- Shutter bottomnavigationbar with page switching example
- Water polo chart - using dynamic ripples to show percentages
- 直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述
- Goahead webserver migration
- 唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
- Threejs basic introduction
- yolov6训练自己的数据记录+yolov5对比测试
- verilog实现串口通信发送到数码管
- Reinforcement learning weekly (issue 51): integration of PAC, ilql, RRL & model free reinforcement learning into micro grid control: overview and Enlightenment
猜你喜欢

Calibration, correction and world coordinate calculation of binocular stereo vision camera (openCV)

Layer 3 loop brought by route Summary - solution experiment

直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述

一颗新的北极星已经升起!
![[advanced ROS chapter] lesson 2 custom header and source file encapsulation](/img/25/85e8c55605f5cc999a8e85f0a05f93.jpg)
[advanced ROS chapter] lesson 2 custom header and source file encapsulation

Storage principle of string
【云原生实战】KubeSphere实战——多租户系统实战

美国隧道法ASTM E84 表面阻燃测试

Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech

Viewing technological changes through Huawei Corps (V): smart Park
随机推荐
PostgreSQL每周新闻—6月22日
Selection of materials for space conductive disc slip ring
Navigation experiment [microcomputer principle] [experiment]
CORDIC based Signal Processor desgn
Shutter bottomnavigationbar toggle page hold
知识蒸馏(Knowledge Distilling)学习笔记
Implementation and Simulation of ads131a04 ADC Verilog
About Effect Size
A. Beat The Odds
唯品会商品详情API接口(item_get-获得唯品会商品详情接口),唯品会详情API接口
TMUX settings
【云原生实战】KubeSphere实战——多租户系统实战
Reinforcement learning weekly (issue 51): integration of PAC, ilql, RRL & model free reinforcement learning into micro grid control: overview and Enlightenment
Go standard library context package: data, cancellation signal, deadline and other related operations between a single request and multiple goroutines and the request domain
leetcode:370. 区间加法
LeetCode 1. 两数之和
leetcode:724. Find the central subscript of the array
Detailed explanation of key points in implementing MES system in Enterprises
PostgreSQL weekly news - June 22
亚马逊商品详情API接口-(item_get-获得AMAZON商品详情接口),亚马逊详情API接口