001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.support;
018
019import org.springframework.lang.Nullable;
020import org.springframework.messaging.Message;
021import org.springframework.messaging.MessageChannel;
022import org.springframework.messaging.MessageHandler;
023
024/**
025 * An extension of {@link ChannelInterceptor} with callbacks to intercept the
026 * asynchronous sending of a {@link org.springframework.messaging.Message} to
027 * a specific subscriber through an {@link java.util.concurrent.Executor}.
028 * Supported on {@link org.springframework.messaging.MessageChannel}
029 * implementations that can be configured with an {@code Executor}.
030 *
031 * @author Rossen Stoyanchev
032 * @since 4.1
033 * @see Message
034 * @see MessageChannel
035 * @see MessageHandler
036 */
037public interface ExecutorChannelInterceptor extends ChannelInterceptor {
038
039        /**
040         * Invoked inside the {@link Runnable} submitted to the Executor just before
041         * calling the target MessageHandler to handle the message. Allows for
042         * modification of the Message if necessary or when {@code null} is returned
043         * the MessageHandler is not invoked.
044         * @param message the message to be handled
045         * @param channel the channel on which the message was sent to
046         * @param handler the target handler to handle the message
047         * @return the input message, or a new instance, or {@code null}
048         */
049        @Nullable
050        default Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
051                return message;
052        }
053
054        /**
055         * Invoked inside the {@link Runnable} submitted to the Executor after calling
056         * the target MessageHandler regardless of the outcome (i.e. Exception raised
057         * or not) thus allowing for proper resource cleanup.
058         * <p>Note that this will be invoked only if beforeHandle successfully completed
059         * and returned a Message, i.e. it did not return {@code null}.
060         * @param message the message handled
061         * @param channel the channel on which the message was sent to
062         * @param handler the target handler that handled the message
063         * @param ex any exception that may been raised by the handler
064         */
065        default void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler,
066                        @Nullable Exception ex) {
067        }
068
069}