001/*
002 * Copyright 2002-2016 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;
018
019/**
020 * Defines methods for sending messages.
021 *
022 * @author Mark Fisher
023 * @since 4.0
024 */
025@FunctionalInterface
026public interface MessageChannel {
027
028        /**
029         * Constant for sending a message without a prescribed timeout.
030         */
031        long INDEFINITE_TIMEOUT = -1;
032
033
034        /**
035         * Send a {@link Message} to this channel. If the message is sent successfully,
036         * the method returns {@code true}. If the message cannot be sent due to a
037         * non-fatal reason, the method returns {@code false}. The method may also
038         * throw a RuntimeException in case of non-recoverable errors.
039         * <p>This method may block indefinitely, depending on the implementation.
040         * To provide a maximum wait time, use {@link #send(Message, long)}.
041         * @param message the message to send
042         * @return whether or not the message was sent
043         */
044        default boolean send(Message<?> message) {
045                return send(message, INDEFINITE_TIMEOUT);
046        }
047
048        /**
049         * Send a message, blocking until either the message is accepted or the
050         * specified timeout period elapses.
051         * @param message the message to send
052         * @param timeout the timeout in milliseconds or {@link #INDEFINITE_TIMEOUT}
053         * @return {@code true} if the message is sent, {@code false} if not
054         * including a timeout of an interrupt of the send
055         */
056        boolean send(Message<?> message, long timeout);
057
058}