blob: ac3742e128a4de8d549feba0400092f2fd06e87a [file] [log] [blame]
Madan Jampani13f65152016-08-17 13:14:53 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.store.service;
17
18import java.util.concurrent.CompletableFuture;
19import java.util.function.Consumer;
20
21/**
22 * A distributed publish subscribe primitive.
23 * <p>
24 * This primitive provides ordered message delivery guarantee i.e. all messages will be delivered to
25 * all <i>active</i> subscribers and messages published from each publisher will be delivered
26 * to all active subscribers in the order in which they are published.
27 * <p>
28 * Transient disruptions in communication such as occasional message drops are automatically handled
29 * and recovered from without loss of delivery guarantees.
30 * <p>
31 * However, subscribers need to remain active or alive for these guarantees to apply. A subscriber that is
32 * partitioned away for an extended duration (typically 5 seconds or more) will be marked as inactive and
33 * during that period of inactivity will be removed from the list of current subscribers.
34 *
35 * @param <T> The type of message to be distributed to subscribers
36 */
37public interface Topic<T> extends DistributedPrimitive {
38
39 /**
40 * Publishes a message to all subscribers.
41 * <p>
42 * The message is delivered in a asynchronous fashion which means subscribers will receive the
43 * message eventually but not necessarily before the future returned by this method is completed.
44 * @param message The non-null message to send to all current subscribers
45 * @return a future that is completed when the message is logged (not necessarily delivered).
46 */
47 CompletableFuture<Void> publish(T message);
48
49 /**
50 * Subscribes to messages published to this topic.
51 * @param callback callback that will invoked when a message published to the topic is received.
52 * @return a future that is completed when subscription request is completed.
53 */
54 CompletableFuture<Void> subscribe(Consumer<T> callback);
55
56 /**
57 * Unsubscribes from this topic.
58 * @param callback previously subscribed callback
59 * @return a future that is completed when unsubscription request is completed.
60 */
61 CompletableFuture<Void> unsubscribe(Consumer<T> callback);
62}