当前位置:网站首页>[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 ;
边栏推荐
- Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
- Insmod prompt invalid module format
- NFT: how to make money with unique assets?
- 由扫地增而引起的小叙
- 开发者,云原生数据库是未来吗?
- Database connection pool & jdbctemplate
- CVPR 2022 | single step 3D target recognizer based on sparse transformer
- Making and using the cutting tool of TTF font library
- 初识Linkerd项目
- 10 minute fitness method reading notes (5/5)
猜你喜欢
Redis clean cache
HiEngine:可媲美本地的云原生内存数据库引擎
10 minute fitness method reading notes (3/5)
Simply take stock reading notes (4/8)
Preliminary exploration of basic knowledge of MySQL
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
NPM install reports an error
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Why is your next computer a computer? Explore different remote operations
随机推荐
Setting up sqli lab environment
CVPR 2022 | single step 3D target recognizer based on sparse transformer
Volatile instruction rearrangement and why instruction rearrangement is prohibited
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Preliminary exploration of basic knowledge of MySQL
HiEngine:可媲美本地的云原生内存数据库引擎
Install rhel8.2 virtual machine
Why is your next computer a computer? Explore different remote operations
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
Transactions from January 14 to 19, 2022
Redis master-slave configuration and sentinel mode
非技术部门,如何参与 DevOps?
Free testing of Taobao tmall API order and flag insertion remark interface
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
研究:数据安全工具在 60% 的情况下无法抵御勒索软件
Clear neo4j database data
Keras implements verification code identification
Pytoch through datasets Imagefolder loads datasets directly from files
Difference between JUnit theories and parameterized tests
Common commands and basic operations of Apache Phoenix