当前位置:网站首页>[cloud native] use of Nacos taskmanager task management
[cloud native] use of Nacos taskmanager task management
2022-07-05 12:51: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 ;
边栏推荐
- Super efficient! The secret of swagger Yapi
- mysql拆分字符串做条件查询
- Distributed cache architecture - cache avalanche & penetration & hit rate
- 单独编译内核模块
- Notes for preparation of information system project manager --- information knowledge
- Introduction to relational model theory
- 国内市场上的BI软件,到底有啥区别
- JDBC -- use JDBC connection to operate MySQL database
- Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
猜你喜欢

Simply take stock reading notes (2/8)

CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器

JDBC -- extract JDBC tool classes

Taobao order amount check error, avoid capital loss API

What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail

滴滴开源DELTA:AI开发者可轻松训练自然语言模型

【云原生】Nacos中的事件发布与订阅--观察者模式

开发者,云原生数据库是未来吗?

VoneDAO破解组织发展效能难题

Alipay transfer system background or API interface to avoid pitfalls
随机推荐
Kotlin流程控制、循环
Principle of universal gbase high availability synchronization tool in Nanjing University
How to connect the API interface of Taobao open platform (super detailed)
Time conversion error
NLP engineer learning summary and index
How can labels/legends be added for all chart types in chart. js (chartjs.org)?
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this
Keras implements verification code identification
RHCSA2
Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
Language model
A possible investment strategy and a possible fuzzy fast stock valuation method
[figure neural network] GNN from entry to mastery
insmod 提示 Invalid module format
About LDA model
stirring! 2022 open atom global open source summit registration is hot!