Skip to content

Commit 0350a64

Browse files
artembilangaryrussell
authored andcommitted
Optimize synchronized in PartitionedDispatcher (#8640)
Even if the `PartitionedDispatcher.populatedPartitions()` is fast, in-memory, non-blocking operation, its active call from the `dispatch()` on every message sent to the channel may pin the virtual thread. * Optimize the `populatedPartitions()` for double `if` where we will step into a `synchronized` block only for first several concurrent messages **Cherry-pick to `6.1.x`**
1 parent 1a8450b commit 0350a64

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dispatcher/PartitionedDispatcher.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,16 @@ public boolean dispatch(Message<?> message) {
151151
return partitionDispatcher.dispatch(message);
152152
}
153153

154-
private synchronized void populatedPartitions() {
154+
private void populatedPartitions() {
155155
if (this.partitions.isEmpty()) {
156-
for (int i = 0; i < this.partitionCount; i++) {
157-
this.partitions.put(i, newPartition());
156+
synchronized (this.partitions) {
157+
if (this.partitions.isEmpty()) {
158+
Map<Integer, UnicastingDispatcher> partitionsToUse = new HashMap<>();
159+
for (int i = 0; i < this.partitionCount; i++) {
160+
partitionsToUse.put(i, newPartition());
161+
}
162+
this.partitions.putAll(partitionsToUse);
163+
}
158164
}
159165
}
160166
}

0 commit comments

Comments
 (0)