当前位置:网站首页>[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 ;
边栏推荐
- 10 minute fitness method reading notes (2/5)
- Distributed solution - completely solve website cross domain requests
- Transactions from January 6 to October 2022
- GPON technical standard analysis I
- HiEngine:可媲美本地的云原生内存数据库引擎
- A possible investment strategy and a possible fuzzy fast stock valuation method
- 国内市场上的BI软件,到底有啥区别
- Using MySQL in docker
- Kotlin变量
- Docker configures redis and redis clusters
猜你喜欢
Transactions from December 29, 2021 to January 4, 2022
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
研究:数据安全工具在 60% 的情况下无法抵御勒索软件
JDBC -- use JDBC connection to operate MySQL database
深度长文探讨Join运算的简化和提速
Why is your next computer a computer? Explore different remote operations
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
Resnet18 actual battle Baoke dream spirit
Transactions from January 14 to 19, 2022
你的下一台电脑何必是电脑,探索不一样的远程操作
随机推荐
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Cypher syntax of neo4j graph database
Anaconda creates a virtual environment and installs pytorch
非技术部门,如何参与 DevOps?
单独编译内核模块
Distributed solution - distributed lock solution - redis based distributed lock implementation
End to end neural network
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Kotlin流程控制、循环
Pytoch implements tf Functions of the gather() function
Introduction to relational model theory
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
Pytorch two-layer loop to realize the segmentation of large pictures
C alarm design
Kotlin函数
Resnet+attention project complete code learning
[cloud native] event publishing and subscription in Nacos -- observer mode
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
2021-12-22 transaction record