During the operation of kafkaproducer, the cluster meta information is needed, which is stored in cluster
Node, topicpartition and partitioninfo
Cluster meta information is composed of three types of objectsNode, TopicPartition, PartitionInfo
- Node source code node represents a node in the cluster
- Topicpartition source topicpartition represents a partition of topic
- Partitioninfo source code partitioninfo represents the details of a partition
Read the source code carefully. It can be seen that all three types of internal member variables are declared as final variables. Here, an immutability concurrent design pattern is used
Immutability’s concurrent design mode:Once the object is created, the state does not change, that is, member variables cannot be modified and immutable is used instead of locking (because immutable, no locking is needed), so as to solve the concurrency problem under multithreading.
Implementation of immutability mode
If all properties of a class are set to final and only read-only methods are allowed, then the class is basically immutable.More strictly, the class itself is final, that is, inheritance is not allowed. Because the subclass can override the method of the parent class, it is possible to change the immutability, so it is recommended that you use this stricter method in actual work.
Fast summary of immutable mode
- Class final
- Final attribute
- Method read-only
- PartitionInfoCan classes be immutable?
Immutability mode extension [article reference]
Immunity mode extension