ChannelInterceptor default methods + deprecate adapter

This commit is contained in:
Rossen Stoyanchev 2018-05-23 21:48:19 -04:00
parent eed663ff7e
commit fbf25c536d
9 changed files with 42 additions and 41 deletions

View File

@ -34,7 +34,6 @@ import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.messaging.support.InterceptableChannel;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@ -274,7 +273,7 @@ public abstract class AbstractBrokerMessageHandler
/**
* Detect unsent DISCONNECT messages and process them anyway.
*/
private class UnsentDisconnectChannelInterceptor extends ChannelInterceptorAdapter {
private class UnsentDisconnectChannelInterceptor implements ChannelInterceptor {
@Override
public void afterSendCompletion(

View File

@ -38,13 +38,16 @@ public interface ChannelInterceptor {
* send invocation will not occur.
*/
@Nullable
Message<?> preSend(Message<?> message, MessageChannel channel);
default Message<?> preSend(Message<?> message, MessageChannel channel) {
return message;
}
/**
* Invoked immediately after the send invocation. The boolean
* value argument represents the return value of that invocation.
*/
void postSend(Message<?> message, MessageChannel channel, boolean sent);
default void postSend(Message<?> message, MessageChannel channel, boolean sent) {
}
/**
* Invoked after the completion of a send regardless of any exception that
@ -53,14 +56,18 @@ public interface ChannelInterceptor {
* completed and returned a Message, i.e. it did not return {@code null}.
* @since 4.1
*/
void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, @Nullable Exception ex);
default void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent,
@Nullable Exception ex) {
}
/**
* Invoked as soon as receive is called and before a Message is
* actually retrieved. If the return value is 'false', then no
* Message will be retrieved. This only applies to PollableChannels.
*/
boolean preReceive(MessageChannel channel);
default boolean preReceive(MessageChannel channel) {
return true;
}
/**
* Invoked immediately after a Message has been retrieved but before
@ -69,7 +76,9 @@ public interface ChannelInterceptor {
* This only applies to PollableChannels.
*/
@Nullable
Message<?> postReceive(Message<?> message, MessageChannel channel);
default Message<?> postReceive(Message<?> message, MessageChannel channel) {
return message;
}
/**
* Invoked after the completion of a receive regardless of any exception that
@ -78,6 +87,8 @@ public interface ChannelInterceptor {
* completed and returned {@code true}.
* @since 4.1
*/
void afterReceiveCompletion(@Nullable Message<?> message, MessageChannel channel, @Nullable Exception ex);
default void afterReceiveCompletion(@Nullable Message<?> message, MessageChannel channel,
@Nullable Exception ex) {
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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,7 +27,11 @@ import org.springframework.messaging.MessageChannel;
* @author Mark Fisher
* @author Rossen Stoyanchev
* @since 4.0
* @deprecated as of 5.0.7 {@link ChannelInterceptor} has default methods (made
* possible by a Java 8 baseline) and can be implemented directly without the
* need for this no-op adapter
*/
@Deprecated
public abstract class ChannelInterceptorAdapter implements ChannelInterceptor {
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 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.
@ -30,7 +30,7 @@ import org.springframework.messaging.MessageChannel;
* @author Rossen Stoyanchev
* @since 4.1.2
*/
public class ImmutableMessageChannelInterceptor extends ChannelInterceptorAdapter {
public class ImmutableMessageChannelInterceptor implements ChannelInterceptor {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {

View File

@ -17,10 +17,8 @@
package org.springframework.messaging.simp.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -64,7 +62,6 @@ import org.springframework.messaging.simp.user.UserDestinationMessageHandler;
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
import org.springframework.messaging.support.AbstractSubscribableChannel;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.messaging.support.ExecutorSubscribableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@ -607,7 +604,7 @@ public class MessageBrokerConfigurationTests {
@Configuration
static class CustomConfig extends BaseTestMessageBrokerConfig {
private ChannelInterceptor interceptor = new ChannelInterceptorAdapter() {};
private ChannelInterceptor interceptor = new ChannelInterceptor() {};
@Override
protected void configureClientInboundChannel(ChannelRegistration registration) {

View File

@ -88,7 +88,7 @@ public class ChannelInterceptorTests {
public void postSendInterceptorMessageWasSent() {
final AtomicBoolean preSendInvoked = new AtomicBoolean(false);
final AtomicBoolean completionInvoked = new AtomicBoolean(false);
this.channel.addInterceptor(new ChannelInterceptorAdapter() {
this.channel.addInterceptor(new ChannelInterceptor() {
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
assertInput(message, channel, sent);
@ -121,7 +121,7 @@ public class ChannelInterceptorTests {
};
final AtomicBoolean preSendInvoked = new AtomicBoolean(false);
final AtomicBoolean completionInvoked = new AtomicBoolean(false);
testChannel.addInterceptor(new ChannelInterceptorAdapter() {
testChannel.addInterceptor(new ChannelInterceptor() {
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
assertInput(message, channel, sent);
@ -199,7 +199,7 @@ public class ChannelInterceptorTests {
}
private abstract static class AbstractTestInterceptor extends ChannelInterceptorAdapter {
private abstract static class AbstractTestInterceptor implements ChannelInterceptor {
private AtomicInteger counter = new AtomicInteger();

View File

@ -186,8 +186,7 @@ public class ExecutorSubscribableChannelTests {
}
private abstract static class AbstractTestInterceptor extends ChannelInterceptorAdapter
implements ExecutorChannelInterceptor {
private abstract static class AbstractTestInterceptor implements ChannelInterceptor, ExecutorChannelInterceptor {
private AtomicInteger counter = new AtomicInteger();
@ -209,7 +208,9 @@ public class ExecutorSubscribableChannelTests {
}
@Override
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler, Exception ex) {
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler,
Exception ex) {
this.afterHandledInvoked = true;
}
}

View File

@ -29,7 +29,7 @@ import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@ -289,7 +289,7 @@ class TestHandshakeHandler implements HandshakeHandler {
}
class TestChannelInterceptor extends ChannelInterceptorAdapter {
class TestChannelInterceptor implements ChannelInterceptor {
}

View File

@ -46,7 +46,7 @@ import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompEncoder;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.ExecutorSubscribableChannel;
import org.springframework.messaging.support.ImmutableMessageChannelInterceptor;
import org.springframework.messaging.support.MessageBuilder;
@ -59,21 +59,10 @@ import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.handler.TestWebSocketSession;
import org.springframework.web.socket.sockjs.transport.SockJsSession;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
/**
* Test fixture for {@link StompSubProtocolHandler} tests.
@ -330,7 +319,7 @@ public class StompSubProtocolHandlerTests {
public void handleMessageFromClientWithImmutableMessageInterceptor() {
AtomicReference<Boolean> mutable = new AtomicReference<>();
ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
channel.addInterceptor(new ChannelInterceptorAdapter() {
channel.addInterceptor(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
@ -352,7 +341,7 @@ public class StompSubProtocolHandlerTests {
public void handleMessageFromClientWithoutImmutableMessageInterceptor() {
AtomicReference<Boolean> mutable = new AtomicReference<>();
ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
channel.addInterceptor(new ChannelInterceptorAdapter() {
channel.addInterceptor(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
@ -557,7 +546,7 @@ public class StompSubProtocolHandlerTests {
}
}
private static class AuthenticationInterceptor extends ChannelInterceptorAdapter {
private static class AuthenticationInterceptor implements ChannelInterceptor {
private final String name;