blob: a50375cf57f26b0c4bf193c255cad752b6b535f0 [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;
Madan Jampani03fb8b22016-08-22 09:08:42 -070019import java.util.concurrent.Executor;
Madan Jampani13f65152016-08-17 13:14:53 -070020import java.util.function.Consumer;
21
Madan Jampani03fb8b22016-08-22 09:08:42 -070022import com.google.common.util.concurrent.MoreExecutors;
23
Madan Jampani13f65152016-08-17 13:14:53 -070024/**
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 */
40public 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 Jampani03fb8b22016-08-22 09:08:42 -070055 * @param executor executor for running the callback
Madan Jampani13f65152016-08-17 13:14:53 -070056 * @return a future that is completed when subscription request is completed.
57 */
Madan Jampani03fb8b22016-08-22 09:08:42 -070058 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 Jampani13f65152016-08-17 13:14:53 -070068
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}