STOMP client session supports sending ack/nack

Issue: SPR-14208
This commit is contained in:
Rossen Stoyanchev
2016-04-26 16:44:58 -04:00
parent 538c582342
commit 06b2d2b89e
3 changed files with 68 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -310,6 +310,23 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
return subscription;
}
@Override
public Receiptable acknowledge(String messageId, boolean consumed) {
StompHeaders stompHeaders = new StompHeaders();
stompHeaders.setId(messageId);
String receiptId = checkOrAddReceipt(stompHeaders);
Receiptable receiptable = new ReceiptHandler(receiptId);
StompCommand command = (consumed ? StompCommand.ACK : StompCommand.NACK);
StompHeaderAccessor accessor = createHeaderAccessor(command);
accessor.addNativeHeaders(stompHeaders);
Message<byte[]> message = createMessage(accessor, null);
execute(message);
return receiptable;
}
private void unsubscribe(String id) {
StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.UNSUBSCRIBE);
accessor.setSubscriptionId(id);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -86,6 +86,18 @@ public interface StompSession {
*/
Subscription subscribe(StompHeaders headers, StompFrameHandler handler);
/**
* Send an acknowledgement whether a message was consumed or not resulting
* in an ACK or NACK frame respectively.
* <p><strong>Note:</strong> to use this when subscribing you must set the
* {@link StompHeaders#setAck(String) ack} header to "client" or
* "client-individual" in order ot use this.
* @param messageId the id of the message
* @param consumed whether the message was consumed or not
* @return a Receiptable for tracking events
*/
Receiptable acknowledge(String messageId, boolean consumed);
/**
* Disconnect the session by sending a DISCONNECT frame.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -491,6 +491,42 @@ public class DefaultStompSessionTests {
assertEquals(subscription.getSubscriptionId(), stompHeaders.getId());
}
@Test
public void ack() throws Exception {
this.session.afterConnected(this.connection);
assertTrue(this.session.isConnected());
String messageId = "123";
this.session.acknowledge(messageId, true);
Message<byte[]> message = this.messageCaptor.getValue();
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
assertEquals(StompCommand.ACK, accessor.getCommand());
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
assertEquals(stompHeaders.toString(), 1, stompHeaders.size());
assertEquals(messageId, stompHeaders.getId());
}
@Test
public void nack() throws Exception {
this.session.afterConnected(this.connection);
assertTrue(this.session.isConnected());
String messageId = "123";
this.session.acknowledge(messageId, false);
Message<byte[]> message = this.messageCaptor.getValue();
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
assertEquals(StompCommand.NACK, accessor.getCommand());
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
assertEquals(stompHeaders.toString(), 1, stompHeaders.size());
assertEquals(messageId, stompHeaders.getId());
}
@Test
public void receiptReceived() throws Exception {