001/*
002 * Copyright 2002-2014 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.messaging.Message;
020import org.springframework.messaging.MessageChannel;
021
022/**
023 * Interface for interceptors that are able to view and/or modify the
024 * {@link Message Messages} being sent-to and/or received-from a
025 * {@link MessageChannel}.
026 *
027 * @author Mark Fisher
028 * @author Rossen Stoyanchev
029 * @since 4.0
030 */
031public interface ChannelInterceptor {
032
033        /**
034         * Invoked before the Message is actually sent to the channel.
035         * This allows for modification of the Message if necessary.
036         * If this method returns {@code null} then the actual
037         * send invocation will not occur.
038         */
039        Message<?> preSend(Message<?> message, MessageChannel channel);
040
041        /**
042         * Invoked immediately after the send invocation. The boolean
043         * value argument represents the return value of that invocation.
044         */
045        void postSend(Message<?> message, MessageChannel channel, boolean sent);
046
047        /**
048         * Invoked after the completion of a send regardless of any exception that
049         * have been raised thus allowing for proper resource cleanup.
050         * <p>Note that this will be invoked only if {@link #preSend} successfully
051         * completed and returned a Message, i.e. it did not return {@code null}.
052         * @since 4.1
053         */
054        void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex);
055
056        /**
057         * Invoked as soon as receive is called and before a Message is
058         * actually retrieved. If the return value is 'false', then no
059         * Message will be retrieved. This only applies to PollableChannels.
060         */
061        boolean preReceive(MessageChannel channel);
062
063        /**
064         * Invoked immediately after a Message has been retrieved but before
065         * it is returned to the caller. The Message may be modified if
066         * necessary. This only applies to PollableChannels.
067         */
068        Message<?> postReceive(Message<?> message, MessageChannel channel);
069
070        /**
071         * Invoked after the completion of a receive regardless of any exception that
072         * have been raised thus allowing for proper resource cleanup.
073         * <p>Note that this will be invoked only if {@link #preReceive} successfully
074         * completed and returned {@code true}.
075         * @since 4.1
076         */
077        void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex);
078
079}