Merge branch '5.3.x'

This commit is contained in:
rstoyanchev 2022-09-12 12:27:31 +01:00
commit c854e35c9d
9 changed files with 138 additions and 86 deletions

View File

@ -19,7 +19,6 @@ package org.springframework.messaging.simp.stomp;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -28,6 +27,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -439,7 +439,7 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
String receiptId = headers.getReceiptId(); String receiptId = headers.getReceiptId();
ReceiptHandler handler = this.receiptHandlers.get(receiptId); ReceiptHandler handler = this.receiptHandlers.get(receiptId);
if (handler != null) { if (handler != null) {
handler.handleReceiptReceived(); handler.handleReceiptReceived(headers);
} }
else if (logger.isDebugEnabled()) { else if (logger.isDebugEnabled()) {
logger.debug("No matching receipt: " + accessor.getDetailedLogMessage(message.getPayload())); logger.debug("No matching receipt: " + accessor.getDetailedLogMessage(message.getPayload()));
@ -544,7 +544,7 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
@Nullable @Nullable
private final String receiptId; 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<Runnable> receiptLostCallbacks = new ArrayList<>(2);
@ -554,6 +554,9 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
@Nullable @Nullable
private Boolean result; private Boolean result;
@Nullable
private StompHeaders receiptHeaders;
public ReceiptHandler(@Nullable String receiptId) { public ReceiptHandler(@Nullable String receiptId) {
this.receiptId = receiptId; this.receiptId = receiptId;
if (receiptId != null) { if (receiptId != null) {
@ -576,64 +579,80 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
@Override @Override
public void addReceiptTask(Runnable task) { public void addReceiptTask(Runnable task) {
addTask(task, true); addReceiptTask(headers -> task.run());
}
@Override
public void addReceiptTask(Consumer<StompHeaders> task) {
Assert.notNull(this.receiptId, "Set autoReceiptEnabled to track receipts or add a 'receiptId' header");
synchronized (this) {
if (this.result != null) {
if (this.result) {
task.accept(this.receiptHeaders);
}
}
else {
this.receiptCallbacks.add(task);
}
}
} }
@Override @Override
public void addReceiptLostTask(Runnable task) { public void addReceiptLostTask(Runnable task) {
addTask(task, false);
}
private void addTask(Runnable task, boolean successTask) {
Assert.notNull(this.receiptId,
"To track receipts, set autoReceiptEnabled=true or add 'receiptId' header");
synchronized (this) { synchronized (this) {
if (this.result != null && this.result == successTask) { if (this.result != null) {
invoke(Collections.singletonList(task)); if (!this.result) {
task.run();
}
} }
else { else {
if (successTask) { this.receiptLostCallbacks.add(task);
this.receiptCallbacks.add(task);
}
else {
this.receiptLostCallbacks.add(task);
}
} }
} }
} }
private void invoke(List<Runnable> callbacks) { public void handleReceiptReceived(StompHeaders receiptHeaders) {
for (Runnable runnable : callbacks) { handleInternal(true, receiptHeaders);
try {
runnable.run();
}
catch (Throwable ex) {
// ignore
}
}
}
public void handleReceiptReceived() {
handleInternal(true);
} }
public void handleReceiptNotReceived() { public void handleReceiptNotReceived() {
handleInternal(false); handleInternal(false, null);
} }
private void handleInternal(boolean result) { private void handleInternal(boolean result, @Nullable StompHeaders receiptHeaders) {
synchronized (this) { synchronized (this) {
if (this.result != null) { if (this.result != null) {
return; return;
} }
this.result = result; this.result = result;
invoke(result ? this.receiptCallbacks : this.receiptLostCallbacks); this.receiptHeaders = receiptHeaders;
if (result) {
this.receiptCallbacks.forEach(consumer -> {
try {
consumer.accept(this.receiptHeaders);
}
catch (Throwable ex) {
// ignore
}
});
}
else {
this.receiptLostCallbacks.forEach(task -> {
try {
task.run();
}
catch (Throwable ex) {
// ignore
}
});
}
DefaultStompSession.this.receiptHandlers.remove(this.receiptId); DefaultStompSession.this.receiptHandlers.remove(this.receiptId);
if (this.future != null) { if (this.future != null) {
this.future.cancel(true); this.future.cancel(true);
} }
} }
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.messaging.simp.stomp; package org.springframework.messaging.simp.stomp;
import java.util.function.Consumer;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
@ -139,16 +141,27 @@ public interface StompSession {
/** /**
* Task to invoke when a receipt is received. * Task to invoke when a receipt is received.
* @param task the task to invoke
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null} * @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
*/ */
void addReceiptTask(Runnable runnable); void addReceiptTask(Runnable task);
/**
* Variant of {@link #addReceiptTask(Runnable)} with a {@link Consumer}
* of the headers from the {@code RECEIPT} frame.
* @param task the consumer to invoke
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
* @since 5.3.23
*/
void addReceiptTask(Consumer<StompHeaders> task);
/** /**
* Task to invoke when a receipt is not received in the configured time. * Task to invoke when a receipt is not received in the configured time.
* @param task the task to invoke
* @throws java.lang.IllegalArgumentException if the receiptId is {@code null} * @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
* @see org.springframework.messaging.simp.stomp.StompClientSupport#setReceiptTimeLimit(long) * @see org.springframework.messaging.simp.stomp.StompClientSupport#setReceiptTimeLimit(long)
*/ */
void addReceiptLostTask(Runnable runnable); void addReceiptLostTask(Runnable task);
} }

View File

@ -575,22 +575,30 @@ public class DefaultStompSessionTests {
this.session.setTaskScheduler(mock(TaskScheduler.class)); this.session.setTaskScheduler(mock(TaskScheduler.class));
AtomicReference<Boolean> received = new AtomicReference<>(); AtomicReference<Boolean> received = new AtomicReference<>();
AtomicReference<StompHeaders> receivedHeaders = new AtomicReference<>();
StompHeaders headers = new StompHeaders(); StompHeaders headers = new StompHeaders();
headers.setDestination("/topic/foo"); headers.setDestination("/topic/foo");
headers.setReceipt("my-receipt"); headers.setReceipt("my-receipt");
Subscription subscription = this.session.subscribe(headers, mock(StompFrameHandler.class)); Subscription subscription = this.session.subscribe(headers, mock(StompFrameHandler.class));
subscription.addReceiptTask(() -> received.set(true)); subscription.addReceiptTask(receiptHeaders -> {
received.set(true);
receivedHeaders.set(receiptHeaders);
});
assertThat((Object) received.get()).isNull(); assertThat((Object) received.get()).isNull();
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
accessor.setReceiptId("my-receipt"); accessor.setReceiptId("my-receipt");
accessor.setNativeHeader("foo", "bar");
accessor.setLeaveMutable(true); accessor.setLeaveMutable(true);
this.session.handleMessage(MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders())); this.session.handleMessage(MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders()));
assertThat(received.get()).isNotNull(); assertThat(received.get()).isNotNull();
assertThat(received.get()).isTrue(); assertThat(received.get()).isTrue();
assertThat(receivedHeaders.get()).isNotNull();
assertThat(receivedHeaders.get().get("foo").size()).isEqualTo(1);
assertThat(receivedHeaders.get().get("foo").get(0)).isEqualTo("bar");
} }
@Test @Test
@ -599,6 +607,7 @@ public class DefaultStompSessionTests {
this.session.setTaskScheduler(mock(TaskScheduler.class)); this.session.setTaskScheduler(mock(TaskScheduler.class));
AtomicReference<Boolean> received = new AtomicReference<>(); AtomicReference<Boolean> received = new AtomicReference<>();
AtomicReference<StompHeaders> receivedHeaders = new AtomicReference<>();
StompHeaders headers = new StompHeaders(); StompHeaders headers = new StompHeaders();
headers.setDestination("/topic/foo"); headers.setDestination("/topic/foo");
@ -607,13 +616,20 @@ public class DefaultStompSessionTests {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
accessor.setReceiptId("my-receipt"); accessor.setReceiptId("my-receipt");
accessor.setNativeHeader("foo", "bar");
accessor.setLeaveMutable(true); accessor.setLeaveMutable(true);
this.session.handleMessage(MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders())); this.session.handleMessage(MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders()));
subscription.addReceiptTask(() -> received.set(true)); subscription.addReceiptTask(receiptHeaders -> {
received.set(true);
receivedHeaders.set(receiptHeaders);
});
assertThat(received.get()).isNotNull(); assertThat(received.get()).isNotNull();
assertThat(received.get()).isTrue(); assertThat(received.get()).isTrue();
assertThat(receivedHeaders.get()).isNotNull();
assertThat(receivedHeaders.get().get("foo").size()).isEqualTo(1);
assertThat(receivedHeaders.get().get("foo").get(0)).isEqualTo("bar");
} }
@Test @Test

View File

@ -25,11 +25,11 @@ import org.springframework.context.SmartLifecycle;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
/** /**
* A base class for WebSocket connection managers. Provides a declarative style of * Base class for a connection manager that automates the process of connecting
* connecting to a WebSocket server given a URI to connect to. The connection occurs when * to a WebSocket server with the Spring ApplicationContext lifecycle. Connects
* the Spring ApplicationContext is refreshed, if the {@link #autoStartup} property is set * to a WebSocket server on {@link #start()} and disconnects on {@link #stop()}.
* to {@code true}, or if set to {@code false}, the {@link #start()} and #stop methods can * If {@link #setAutoStartup(boolean)} is set to {@code true} this will be done
* be invoked manually. * automatically when the Spring {@code ApplicationContext} is refreshed.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
@ -163,11 +163,19 @@ public abstract class ConnectionManagerSupport implements SmartLifecycle {
return this.running; return this.running;
} }
/**
* Whether the connection is open/{@code true} or closed/{@code false}.
*/
public abstract boolean isConnected();
/**
* Subclasses implement this to actually establish the connection.
*/
protected abstract void openConnection(); protected abstract void openConnection();
/**
* Subclasses implement this to close the connection.
*/
protected abstract void closeConnection() throws Exception; protected abstract void closeConnection() throws Exception;
protected abstract boolean isConnected();
} }

View File

@ -28,10 +28,9 @@ import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator; import org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator;
/** /**
* A WebSocket connection manager that is given a URI, a {@link WebSocketClient}, and a * WebSocket {@link ConnectionManagerSupport connection manager} that connects
* {@link WebSocketHandler}, connects to a WebSocket server through {@link #start()} and * to the server via {@link WebSocketClient} and handles the session with a
* {@link #stop()} methods. If {@link #setAutoStartup(boolean)} is set to {@code true} * {@link WebSocketHandler}.
* this will be done automatically when the Spring ApplicationContext is refreshed.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen * @author Sam Brannen
@ -58,14 +57,6 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
} }
/**
* Decorate the WebSocketHandler provided to the class constructor.
* <p>By default {@link LoggingWebSocketHandlerDecorator} is added.
*/
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
return new LoggingWebSocketHandlerDecorator(handler);
}
/** /**
* Set the sub-protocols to use. If configured, specified sub-protocols will be * Set the sub-protocols to use. If configured, specified sub-protocols will be
* requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The
@ -130,6 +121,11 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
super.stopInternal(); super.stopInternal();
} }
@Override
public boolean isConnected() {
return (this.webSocketSession != null && this.webSocketSession.isOpen());
}
@Override @Override
protected void openConnection() { protected void openConnection() {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
@ -157,9 +153,12 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
} }
} }
@Override /**
protected boolean isConnected() { * Decorate the WebSocketHandler provided to the class constructor.
return (this.webSocketSession != null && this.webSocketSession.isOpen()); * <p>By default {@link LoggingWebSocketHandlerDecorator} is added.
*/
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
return new LoggingWebSocketHandlerDecorator(handler);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,11 +31,9 @@ import org.springframework.web.socket.client.ConnectionManagerSupport;
import org.springframework.web.socket.handler.BeanCreatingHandlerProvider; import org.springframework.web.socket.handler.BeanCreatingHandlerProvider;
/** /**
* A WebSocket connection manager that is given a URI, a * WebSocket {@link ConnectionManagerSupport connection manager} that connects
* {@link jakarta.websocket.ClientEndpoint}-annotated endpoint, connects to a * to the server via {@link WebSocketContainer} and handles the session with an
* WebSocket server through the {@link #start()} and {@link #stop()} methods. * {@link javax.websocket.ClientEndpoint @ClientEndpoint} endpoint.
* If {@link #setAutoStartup(boolean)} is set to {@code true} this will be
* done automatically when the Spring ApplicationContext is refreshed.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
@ -101,6 +99,12 @@ public class AnnotatedEndpointConnectionManager extends ConnectionManagerSupport
} }
@Override
public boolean isConnected() {
Session session = this.session;
return (session != null && session.isOpen());
}
@Override @Override
protected void openConnection() { protected void openConnection() {
this.taskExecutor.execute(() -> { this.taskExecutor.execute(() -> {
@ -135,10 +139,4 @@ public class AnnotatedEndpointConnectionManager extends ConnectionManagerSupport
} }
} }
@Override
protected boolean isConnected() {
Session session = this.session;
return (session != null && session.isOpen());
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -39,10 +39,9 @@ import org.springframework.web.socket.client.ConnectionManagerSupport;
import org.springframework.web.socket.handler.BeanCreatingHandlerProvider; import org.springframework.web.socket.handler.BeanCreatingHandlerProvider;
/** /**
* A WebSocket connection manager that is given a URI, an {@link Endpoint}, connects to a * WebSocket {@link ConnectionManagerSupport connection manager} that connects
* WebSocket server through the {@link #start()} and {@link #stop()} methods. If * to the server via {@link WebSocketContainer} and handles the session with an
* {@link #setAutoStartup(boolean)} is set to {@code true} this will be done automatically * {@link Endpoint}.
* when the Spring ApplicationContext is refreshed.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
@ -133,6 +132,12 @@ public class EndpointConnectionManager extends ConnectionManagerSupport implemen
} }
@Override
public boolean isConnected() {
Session session = this.session;
return (session != null && session.isOpen());
}
@Override @Override
protected void openConnection() { protected void openConnection() {
this.taskExecutor.execute(() -> { this.taskExecutor.execute(() -> {
@ -168,10 +173,4 @@ public class EndpointConnectionManager extends ConnectionManagerSupport implemen
} }
} }
@Override
protected boolean isConnected() {
Session session = this.session;
return (session != null && session.isOpen());
}
} }

View File

@ -325,7 +325,7 @@ to serialize only a subset of the object properties, as the following example sh
---- ----
[[rest-template-multipart]] [[rest-template-multipart]]
===== Multipart ==== Multipart
To send multipart data, you need to provide a `MultiValueMap<String, Object>` whose values To send multipart data, you need to provide a `MultiValueMap<String, Object>` whose values
may be an `Object` for part content, a `Resource` for a file part, or an `HttpEntity` for may be an `Object` for part content, a `Resource` for a file part, or an `HttpEntity` for

View File

@ -1347,7 +1347,7 @@ receipt if the server supports it (simple broker does not). For example, with th
headers.setDestination("/topic/..."); headers.setDestination("/topic/...");
headers.setReceipt("r1"); headers.setReceipt("r1");
FrameHandler handler = ...; FrameHandler handler = ...;
stompSession.subscribe(headers, handler).addReceiptTask(() -> { stompSession.subscribe(headers, handler).addReceiptTask(receiptHeaders -> {
// Subscription ready... // Subscription ready...
}); });
---- ----