From fbe96a81129a6111f95e68e1e6bd0e1c57a37f3c Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 12 Aug 2025 06:30:08 +0100 Subject: [PATCH] Polishing contribution Closes gh-35224 --- .../simp/stomp/DefaultStompSession.java | 1 - .../messaging/simp/stomp/StompSession.java | 6 ++- .../simp/stomp/DefaultStompSessionTests.java | 42 +++++-------------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java index 6f72bb0f953..4dd0418e6ab 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java @@ -351,7 +351,6 @@ public class DefaultStompSession implements ConnectionHandlingStompSession { if (headers == null){ headers = new StompHeaders(); } - String receiptId = checkOrAddReceipt(headers); Receiptable receiptable = new ReceiptHandler(receiptId); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompSession.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompSession.java index bcb816c0f6a..1bce9ff94d0 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompSession.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompSession.java @@ -182,13 +182,15 @@ public interface StompSession { /** * Remove the subscription by sending an UNSUBSCRIBE frame. + *

As of 7.0, this method returns {@link Receiptable}. */ Receiptable unsubscribe(); /** * Alternative to {@link #unsubscribe()} with additional custom headers - * to send to the server. - *

Note: There is no need to set the subscription id. + * to send to the server. Note, however, there is no need to set the + * subscription id. + *

As of 7.0, this method returns {@link Receiptable}. * @param headers the custom headers, if any * @since 5.0 */ diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java index c310afb870f..fea9dc4496e 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java @@ -664,7 +664,7 @@ public class DefaultStompSessionTests { } @Test - void unsubscribeWithReceipt() { + void unsubscribeWithoutReceipt() { this.session.afterConnected(this.connection); assertThat(this.session.isConnected()).isTrue(); Subscription subscription = this.session.subscribe("/topic/foo", mock()); @@ -672,62 +672,40 @@ public class DefaultStompSessionTests { Receiptable receipt = subscription.unsubscribe(); assertThat(receipt).isNotNull(); assertThat(receipt.getReceiptId()).isNull(); - - Message message = this.messageCaptor.getValue(); - StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); - assertThat(accessor.getCommand()).isEqualTo(StompCommand.UNSUBSCRIBE); - - StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); - assertThat(stompHeaders).hasSize(1); - assertThat(stompHeaders.getId()).isEqualTo(subscription.getSubscriptionId()); } @Test - void unsubscribeWithCustomHeaderAndReceipt() { + void unsubscribeWithReceipt() { this.session.afterConnected(this.connection); this.session.setTaskScheduler(mock()); this.session.setAutoReceipt(true); + Subscription subscription = this.session.subscribe("/topic/foo", mock()); - StompHeaders subHeaders = new StompHeaders(); - subHeaders.setDestination("/topic/foo"); - Subscription subscription = this.session.subscribe(subHeaders, mock()); - - StompHeaders custom = new StompHeaders(); - custom.set("x-cust", "value"); - - Receiptable receipt = subscription.unsubscribe(custom); + Receiptable receipt = subscription.unsubscribe(); assertThat(receipt).isNotNull(); assertThat(receipt.getReceiptId()).isNotNull(); Message message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); - assertThat(accessor.getCommand()).isEqualTo(StompCommand.UNSUBSCRIBE); - - StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); - assertThat(stompHeaders.getId()).isEqualTo(subscription.getSubscriptionId()); - assertThat(stompHeaders.get("x-cust")).containsExactly("value"); - assertThat(stompHeaders.getReceipt()).isEqualTo(receipt.getReceiptId()); + assertThat(accessor.getReceipt()).isEqualTo(receipt.getReceiptId()); } @Test void receiptReceivedOnUnsubscribe() { this.session.afterConnected(this.connection); - TaskScheduler scheduler = mock(); - this.session.setTaskScheduler(scheduler); + this.session.setTaskScheduler(mock()); this.session.setAutoReceipt(true); Subscription subscription = this.session.subscribe("/topic/foo", mock()); Receiptable receipt = subscription.unsubscribe(); - StompHeaderAccessor ack = StompHeaderAccessor.create(StompCommand.RECEIPT); - ack.setReceiptId(receipt.getReceiptId()); - ack.setLeaveMutable(true); - Message receiptMessage = MessageBuilder.createMessage(new byte[0], ack.getMessageHeaders()); - AtomicBoolean called = new AtomicBoolean(false); receipt.addReceiptTask(() -> called.set(true)); - this.session.handleMessage(receiptMessage); + StompHeaderAccessor ack = StompHeaderAccessor.create(StompCommand.RECEIPT); + ack.setReceiptId(receipt.getReceiptId()); + ack.setLeaveMutable(true); + this.session.handleMessage(MessageBuilder.createMessage(new byte[0], ack.getMessageHeaders())); assertThat(called.get()).isTrue(); }