Add ImmutableMessageChannelInterceptor
This change adds a ChannelInterceptor that flips the immutable flag on messages being sent. This allows components sending messages to leave the message mutable for interceptors to further apply modifications before the message is sent (and exposed to concurrency). The interceptor is automatically added with the STOMP/WebSocket Java and XML config and the StompSubProtocolHandler leaves parsed incoming messages mutable so they can be further modified before being sent. Issue: SPR-12321
This commit is contained in:
@@ -45,6 +45,7 @@ import org.springframework.messaging.simp.user.UserDestinationResolver;
|
||||
import org.springframework.messaging.simp.user.UserSessionRegistry;
|
||||
import org.springframework.messaging.support.AbstractSubscribableChannel;
|
||||
import org.springframework.messaging.support.ExecutorSubscribableChannel;
|
||||
import org.springframework.messaging.support.ImmutableMessageChannelInterceptor;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
@@ -118,6 +119,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
|
||||
if (this.clientInboundChannelRegistration == null) {
|
||||
ChannelRegistration registration = new ChannelRegistration();
|
||||
configureClientInboundChannel(registration);
|
||||
registration.setInterceptors(new ImmutableMessageChannelInterceptor());
|
||||
this.clientInboundChannelRegistration = registration;
|
||||
}
|
||||
return this.clientInboundChannelRegistration;
|
||||
@@ -152,6 +154,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
|
||||
if (this.clientOutboundChannelRegistration == null) {
|
||||
ChannelRegistration registration = new ChannelRegistration();
|
||||
configureClientOutboundChannel(registration);
|
||||
registration.setInterceptors(new ImmutableMessageChannelInterceptor());
|
||||
this.clientOutboundChannelRegistration = registration;
|
||||
}
|
||||
return this.clientOutboundChannelRegistration;
|
||||
@@ -169,6 +172,7 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
|
||||
ChannelRegistration reg = getBrokerRegistry().getBrokerChannelRegistration();
|
||||
ExecutorSubscribableChannel channel = reg.hasTaskExecutor() ?
|
||||
new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel();
|
||||
reg.setInterceptors(new ImmutableMessageChannelInterceptor());
|
||||
channel.setInterceptors(reg.getInterceptors());
|
||||
return channel;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.messaging.support;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
* A simpler interceptor that calls {@link MessageHeaderAccessor#setImmutable()}
|
||||
* on the headers of messages passed through the preSend method.
|
||||
*
|
||||
* <p>When configured as the last interceptor in a chain, it allows the component
|
||||
* sending the message to leave headers mutable for interceptors to modify prior
|
||||
* to the message actually being sent and exposed to concurrent access.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public class ImmutableMessageChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
|
||||
|
||||
@Override
|
||||
public Message<?> preSend(Message<?> message, MessageChannel channel) {
|
||||
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
|
||||
if (accessor != null && accessor.isMutable()) {
|
||||
accessor.setImmutable();
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user