当前位置:网站首页>Considerations for using redisson to operate distributed queues
Considerations for using redisson to operate distributed queues
2022-06-22 23:02:00 【M_ O_】
Say first conclusion :
Use Redisson Provided RedissonPriorityQueue when , Compare operations that do not use objects equals, But use compare Compare .
Recently, I did a service for scheduled tasks , Distributed priority queues are required , I chose Redisson library .
Redisson There are many queues , Also inherited java Bag Queue Interface , It looks very convenient .
After modifying the code , I found a very strange problem , After adding an element , It can't be removed , remove Back to false.
Element is a custom class , Rewrote equals and hashcode, So there should be no problem , Use java con The queue in the package is all right .
This is very strange , Redisson Although the interface is implemented , But the expected behavior is wrong , Don't pass equals Function to compare whether the elements are the same ,
Through code analysis RedissonPriorityQueue To find out why ( Show only key code ):
@Override
public boolean remove(Object value) {
BinarySearchResult<V> res = binarySearch((V) value, codec);
remove((int) res.getIndex());
}
above remove The function display uses binarySearch Element found , Then delete it
public BinarySearchResult<V> binarySearch(V value, Codec codec) {
// Call the comparator
int cmp = comparator.compare(value, res);
if (cmp == 0) {
//...
} else if (cmp < 0) {
// ...
} else {
// ...
}
}
}
The above code indicates that the comparator.compare Compare two elements , Returns the position of the element in the queue
// Comparator code
private Comparator<? super V> comparator = NaturalComparator.NATURAL_ORDER;
private static class NaturalComparator<V> implements Comparator<V>, Serializable {
private static final long serialVersionUID = 7207038068494060240L;
static final NaturalComparator NATURAL_ORDER = new NaturalComparator();
public int compare(V c1, V c2) {
Comparable<Object> c1co = (Comparable<Object>) c1;
Comparable<Object> c2co = (Comparable<Object>) c2;
return c1co.compareTo(c2co);
}
}
You can see that the comparator finally calls c1co.compareTo(c2co) To compare two elements .
Generally speaking , compare The function compares the priority of two elements , Instead of comparing whether the contents of two elements are the same , but RedissonPriorityQueue break with convention , Not only use compare To compare priorities , It is also used to compare whether the elements are the same .
So be very careful compare Implementation of function , For example, a task ( The task name uniquely identifies a task , Time as a priority ), It can be written like this :
@Override
public int compareTo(TaskNode o) {
if (getName().equals(o.getName())) {
// The same name , Description is the same task
return 0;
}
// Compare delay
long diffMs = getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS);
if (diffMs == 0) {
// Same delay , Comparison name
return getName().compareTo(o.getName());
}
return diffMs > 0 ? 1 : -1;
}
边栏推荐
- 2020-12-20
- How to manage tasks in note taking software such as flowus and notation?
- In a frame because it set 'X-FRAME-OPTIONS' to' deny '
- 【ROS】ROSmsg cakin_ Make compilation error
- 2021-04-05
- How to quickly build an enterprise knowledge base at low cost?
- MySQL functions
- Las point cloud data thinning in ArcGIS
- Grafana report display of sentinel based high availability current limiting system
- Is it bad for NFT that the market starts to cool down?
猜你喜欢

2021-03-06

Redis error reporting and common configurations
Learn redis with you (11) -- redis distributed lock

Is it bad for NFT that the market starts to cool down?

2021-04-16
Several ways of redis persistence -- deeply parsing RDB

2021-08-26

ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”

What are the methods of software stress testing and how to select a software stress testing organization?

《强化学习周刊》第50期:SafeRL-Kit、GMI-DRL、RP-SDRL & 离线元强化学习
随机推荐
Core and semiconductor "RF eda/ filter design platform" shines ims2022
2020-12-20
Spark RDD Programming Guide(2.4.3)
Spark SQL Start(2.4.3)
《强化学习周刊》第50期:SafeRL-Kit、GMI-DRL、RP-SDRL & 离线元强化学习
2021-08-21
组合总数[标准回溯 + 回溯技巧--降低栈深度]
Mysql database DQL query operation
Spark SQL Generic Load/Save Functions(2.4.3)
Redis big key problem
Case 2 of SQL performance degradation caused by modifying implicit parameters
Codeup longest palindrome substring
The link added in the bottom menu cannot jump to the secondary page
Grafana report display of sentinel based high availability current limiting system
Spark SQL 访问json和jdbc数据源
2021-04-14
Wechat applet batch submission for review
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
LinkedList 源码解析
Eureka service registration and discovery