Skip to content

Commit 4e97776

Browse files
committed
Polishing contribution
Closes gh-28715
1 parent d42f950 commit 4e97776

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.Type;
2020
import java.util.ArrayList;
21-
import java.util.Collections;
2221
import java.util.Date;
2322
import java.util.List;
2423
import java.util.Map;
@@ -549,7 +548,7 @@ private class ReceiptHandler implements Receiptable {
549548

550549
private final List<Consumer<StompHeaders>> receiptCallbacks = new ArrayList<>(2);
551550

552-
private final List<Consumer<StompHeaders>> receiptLostCallbacks = new ArrayList<>(2);
551+
private final List<Runnable> receiptLostCallbacks = new ArrayList<>(2);
553552

554553
@Nullable
555554
private ScheduledFuture<?> future;
@@ -582,44 +581,34 @@ public String getReceiptId() {
582581

583582
@Override
584583
public void addReceiptTask(Runnable task) {
585-
addTask(h -> task.run(), true);
584+
addReceiptTask(headers -> task.run());
586585
}
587586

588587
@Override
589588
public void addReceiptTask(Consumer<StompHeaders> task) {
590-
addTask(task, true);
591-
}
592-
593-
@Override
594-
public void addReceiptLostTask(Runnable task) {
595-
addTask(h -> task.run(), false);
596-
}
597-
598-
private void addTask(Consumer<StompHeaders> task, boolean successTask) {
599-
Assert.notNull(this.receiptId,
600-
"To track receipts, set autoReceiptEnabled=true or add 'receiptId' header");
589+
Assert.notNull(this.receiptId, "Set autoReceiptEnabled to track receipts or add a 'receiptId' header");
601590
synchronized (this) {
602-
if (this.result != null && this.result == successTask) {
603-
invoke(Collections.singletonList(task));
591+
if (this.result != null) {
592+
if (this.result) {
593+
task.accept(this.receiptHeaders);
594+
}
604595
}
605596
else {
606-
if (successTask) {
607-
this.receiptCallbacks.add(task);
608-
}
609-
else {
610-
this.receiptLostCallbacks.add(task);
611-
}
597+
this.receiptCallbacks.add(task);
612598
}
613599
}
614600
}
615601

616-
private void invoke(List<Consumer<StompHeaders>> callbacks) {
617-
for (Consumer<StompHeaders> consumer : callbacks) {
618-
try {
619-
consumer.accept(this.receiptHeaders);
602+
@Override
603+
public void addReceiptLostTask(Runnable task) {
604+
synchronized (this) {
605+
if (this.result != null) {
606+
if (!this.result) {
607+
task.run();
608+
}
620609
}
621-
catch (Throwable ex) {
622-
// ignore
610+
else {
611+
this.receiptLostCallbacks.add(task);
623612
}
624613
}
625614
}
@@ -639,13 +628,33 @@ private void handleInternal(boolean result, @Nullable StompHeaders receiptHeader
639628
}
640629
this.result = result;
641630
this.receiptHeaders = receiptHeaders;
642-
invoke(result ? this.receiptCallbacks : this.receiptLostCallbacks);
631+
if (result) {
632+
this.receiptCallbacks.forEach(consumer -> {
633+
try {
634+
consumer.accept(this.receiptHeaders);
635+
}
636+
catch (Throwable ex) {
637+
// ignore
638+
}
639+
});
640+
}
641+
else {
642+
this.receiptLostCallbacks.forEach(task -> {
643+
try {
644+
task.run();
645+
}
646+
catch (Throwable ex) {
647+
// ignore
648+
}
649+
});
650+
}
643651
DefaultStompSession.this.receiptHandlers.remove(this.receiptId);
644652
if (this.future != null) {
645653
this.future.cancel(true);
646654
}
647655
}
648656
}
657+
649658
}
650659

651660

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompSession.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,27 @@ interface Receiptable {
141141

142142
/**
143143
* Task to invoke when a receipt is received.
144+
* @param task the task to invoke
144145
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
145146
*/
146-
void addReceiptTask(Runnable runnable);
147+
void addReceiptTask(Runnable task);
147148

148149
/**
149-
* Consumer to invoke when a receipt is received. Accepts the headers of the received RECEIPT frame.
150+
* Variant of {@link #addReceiptTask(Runnable)} with a {@link Consumer}
151+
* of the headers from the {@code RECEIPT} frame.
152+
* @param task the consumer to invoke
150153
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
151-
* @since TBD
154+
* @since 5.3.23
152155
*/
153156
void addReceiptTask(Consumer<StompHeaders> task);
154157

155158
/**
156159
* Task to invoke when a receipt is not received in the configured time.
160+
* @param task the task to invoke
157161
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
158162
* @see org.springframework.messaging.simp.stomp.StompClientSupport#setReceiptTimeLimit(long)
159163
*/
160-
void addReceiptLostTask(Runnable runnable);
164+
void addReceiptLostTask(Runnable task);
161165
}
162166

163167

0 commit comments

Comments
 (0)