Pass headers to STOMP receipt callbacks
See gh-28715
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
@@ -441,7 +442,7 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
|
||||
String receiptId = headers.getReceiptId();
|
||||
ReceiptHandler handler = this.receiptHandlers.get(receiptId);
|
||||
if (handler != null) {
|
||||
handler.handleReceiptReceived();
|
||||
handler.handleReceiptReceived(headers);
|
||||
}
|
||||
else if (logger.isDebugEnabled()) {
|
||||
logger.debug("No matching receipt: " + accessor.getDetailedLogMessage(message.getPayload()));
|
||||
@@ -546,9 +547,9 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
|
||||
@Nullable
|
||||
private final String receiptId;
|
||||
|
||||
private final List<Runnable> receiptCallbacks = new ArrayList<>(2);
|
||||
private final List<Consumer<StompHeaders>> receiptCallbacks = new ArrayList<>(2);
|
||||
|
||||
private final List<Runnable> receiptLostCallbacks = new ArrayList<>(2);
|
||||
private final List<Consumer<StompHeaders>> receiptLostCallbacks = new ArrayList<>(2);
|
||||
|
||||
@Nullable
|
||||
private ScheduledFuture<?> future;
|
||||
@@ -556,6 +557,9 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
|
||||
@Nullable
|
||||
private Boolean result;
|
||||
|
||||
@Nullable
|
||||
private StompHeaders receiptHeaders;
|
||||
|
||||
public ReceiptHandler(@Nullable String receiptId) {
|
||||
this.receiptId = receiptId;
|
||||
if (receiptId != null) {
|
||||
@@ -578,15 +582,20 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
|
||||
|
||||
@Override
|
||||
public void addReceiptTask(Runnable task) {
|
||||
addTask(h -> task.run(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addReceiptTask(Consumer<StompHeaders> task) {
|
||||
addTask(task, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addReceiptLostTask(Runnable task) {
|
||||
addTask(task, false);
|
||||
addTask(h -> task.run(), false);
|
||||
}
|
||||
|
||||
private void addTask(Runnable task, boolean successTask) {
|
||||
private void addTask(Consumer<StompHeaders> task, boolean successTask) {
|
||||
Assert.notNull(this.receiptId,
|
||||
"To track receipts, set autoReceiptEnabled=true or add 'receiptId' header");
|
||||
synchronized (this) {
|
||||
@@ -604,10 +613,10 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
|
||||
}
|
||||
}
|
||||
|
||||
private void invoke(List<Runnable> callbacks) {
|
||||
for (Runnable runnable : callbacks) {
|
||||
private void invoke(List<Consumer<StompHeaders>> callbacks) {
|
||||
for (Consumer<StompHeaders> consumer : callbacks) {
|
||||
try {
|
||||
runnable.run();
|
||||
consumer.accept(this.receiptHeaders);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
// ignore
|
||||
@@ -615,20 +624,21 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
|
||||
}
|
||||
}
|
||||
|
||||
public void handleReceiptReceived() {
|
||||
handleInternal(true);
|
||||
public void handleReceiptReceived(StompHeaders receiptHeaders) {
|
||||
handleInternal(true, receiptHeaders);
|
||||
}
|
||||
|
||||
public void handleReceiptNotReceived() {
|
||||
handleInternal(false);
|
||||
handleInternal(false, null);
|
||||
}
|
||||
|
||||
private void handleInternal(boolean result) {
|
||||
private void handleInternal(boolean result, @Nullable StompHeaders receiptHeaders) {
|
||||
synchronized (this) {
|
||||
if (this.result != null) {
|
||||
return;
|
||||
}
|
||||
this.result = result;
|
||||
this.receiptHeaders = receiptHeaders;
|
||||
invoke(result ? this.receiptCallbacks : this.receiptLostCallbacks);
|
||||
DefaultStompSession.this.receiptHandlers.remove(this.receiptId);
|
||||
if (this.future != null) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.messaging.simp.stomp;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@@ -143,6 +145,13 @@ public interface StompSession {
|
||||
*/
|
||||
void addReceiptTask(Runnable runnable);
|
||||
|
||||
/**
|
||||
* Consumer to invoke when a receipt is received. Accepts the headers of the received RECEIPT frame.
|
||||
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
|
||||
* @since TBD
|
||||
*/
|
||||
void addReceiptTask(Consumer<StompHeaders> task);
|
||||
|
||||
/**
|
||||
* Task to invoke when a receipt is not received in the configured time.
|
||||
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
|
||||
|
||||
Reference in New Issue
Block a user