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.tcp;
018
019import java.io.Closeable;
020
021import org.springframework.messaging.Message;
022import org.springframework.util.concurrent.ListenableFuture;
023
024/**
025 * A contract for sending messages and managing a TCP connection.
026 *
027 * @author Rossen Stoyanchev
028 * @since 4.0
029 * @param <P> the type of payload for outbound {@link Message Messages}
030 */
031public interface TcpConnection<P> extends Closeable {
032
033        /**
034         * Send the given message.
035         * @param message the message
036         * @return a ListenableFuture that can be used to determine when and if the
037         * message was successfully sent
038         */
039        ListenableFuture<Void> send(Message<P> message);
040
041        /**
042         * Register a task to invoke after a period of read inactivity.
043         * @param runnable the task to invoke
044         * @param duration the amount of inactive time in milliseconds
045         */
046        void onReadInactivity(Runnable runnable, long duration);
047
048        /**
049         * Register a task to invoke after a period of write inactivity.
050         * @param runnable the task to invoke
051         * @param duration the amount of inactive time in milliseconds
052         */
053        void onWriteInactivity(Runnable runnable, long duration);
054
055        /**
056         * Close the connection.
057         */
058        @Override
059        void close();
060
061}