Madan Jampani | 13f6515 | 2016-08-17 13:14:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 13f6515 | 2016-08-17 13:14:53 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 03fb8b2 | 2016-08-22 09:08:42 -0700 | [diff] [blame] | 19 | import java.util.concurrent.Executor; |
Madan Jampani | 13f6515 | 2016-08-17 13:14:53 -0700 | [diff] [blame] | 20 | import java.util.function.Consumer; |
| 21 | |
Madan Jampani | 03fb8b2 | 2016-08-22 09:08:42 -0700 | [diff] [blame] | 22 | import com.google.common.util.concurrent.MoreExecutors; |
| 23 | |
Madan Jampani | 13f6515 | 2016-08-17 13:14:53 -0700 | [diff] [blame] | 24 | /** |
| 25 | * A distributed publish subscribe primitive. |
| 26 | * <p> |
| 27 | * This primitive provides ordered message delivery guarantee i.e. all messages will be delivered to |
| 28 | * all <i>active</i> subscribers and messages published from each publisher will be delivered |
| 29 | * to all active subscribers in the order in which they are published. |
| 30 | * <p> |
| 31 | * Transient disruptions in communication such as occasional message drops are automatically handled |
| 32 | * and recovered from without loss of delivery guarantees. |
| 33 | * <p> |
| 34 | * However, subscribers need to remain active or alive for these guarantees to apply. A subscriber that is |
| 35 | * partitioned away for an extended duration (typically 5 seconds or more) will be marked as inactive and |
| 36 | * during that period of inactivity will be removed from the list of current subscribers. |
| 37 | * |
| 38 | * @param <T> The type of message to be distributed to subscribers |
| 39 | */ |
| 40 | public interface Topic<T> extends DistributedPrimitive { |
| 41 | |
| 42 | /** |
| 43 | * Publishes a message to all subscribers. |
| 44 | * <p> |
| 45 | * The message is delivered in a asynchronous fashion which means subscribers will receive the |
| 46 | * message eventually but not necessarily before the future returned by this method is completed. |
| 47 | * @param message The non-null message to send to all current subscribers |
| 48 | * @return a future that is completed when the message is logged (not necessarily delivered). |
| 49 | */ |
| 50 | CompletableFuture<Void> publish(T message); |
| 51 | |
| 52 | /** |
| 53 | * Subscribes to messages published to this topic. |
| 54 | * @param callback callback that will invoked when a message published to the topic is received. |
Madan Jampani | 03fb8b2 | 2016-08-22 09:08:42 -0700 | [diff] [blame] | 55 | * @param executor executor for running the callback |
Madan Jampani | 13f6515 | 2016-08-17 13:14:53 -0700 | [diff] [blame] | 56 | * @return a future that is completed when subscription request is completed. |
| 57 | */ |
Madan Jampani | 03fb8b2 | 2016-08-22 09:08:42 -0700 | [diff] [blame] | 58 | CompletableFuture<Void> subscribe(Consumer<T> callback, Executor executor); |
| 59 | |
| 60 | /** |
| 61 | * Subscribes to messages published to this topic. |
| 62 | * @param callback callback that will invoked when a message published to the topic is received. |
| 63 | * @return a future that is completed when subscription request is completed. |
| 64 | */ |
| 65 | default CompletableFuture<Void> subscribe(Consumer<T> callback) { |
| 66 | return subscribe(callback, MoreExecutors.directExecutor()); |
| 67 | } |
Madan Jampani | 13f6515 | 2016-08-17 13:14:53 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Unsubscribes from this topic. |
| 71 | * @param callback previously subscribed callback |
| 72 | * @return a future that is completed when unsubscription request is completed. |
| 73 | */ |
| 74 | CompletableFuture<Void> unsubscribe(Consumer<T> callback); |
| 75 | } |