Committed by
Gerrit Code Review
Topic: Support for passing a executor to subscribe method for invoking the callback
Change-Id: I9db485ee381c61fbfc38aba0c2bd90cb5af171e0
Showing
2 changed files
with
18 additions
and
3 deletions
... | @@ -16,8 +16,11 @@ | ... | @@ -16,8 +16,11 @@ |
16 | package org.onosproject.store.service; | 16 | package org.onosproject.store.service; |
17 | 17 | ||
18 | import java.util.concurrent.CompletableFuture; | 18 | import java.util.concurrent.CompletableFuture; |
19 | +import java.util.concurrent.Executor; | ||
19 | import java.util.function.Consumer; | 20 | import java.util.function.Consumer; |
20 | 21 | ||
22 | +import com.google.common.util.concurrent.MoreExecutors; | ||
23 | + | ||
21 | /** | 24 | /** |
22 | * A distributed publish subscribe primitive. | 25 | * A distributed publish subscribe primitive. |
23 | * <p> | 26 | * <p> |
... | @@ -49,9 +52,19 @@ public interface Topic<T> extends DistributedPrimitive { | ... | @@ -49,9 +52,19 @@ public interface Topic<T> extends DistributedPrimitive { |
49 | /** | 52 | /** |
50 | * Subscribes to messages published to this topic. | 53 | * Subscribes to messages published to this topic. |
51 | * @param callback callback that will invoked when a message published to the topic is received. | 54 | * @param callback callback that will invoked when a message published to the topic is received. |
55 | + * @param executor executor for running the callback | ||
56 | + * @return a future that is completed when subscription request is completed. | ||
57 | + */ | ||
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. | ||
52 | * @return a future that is completed when subscription request is completed. | 63 | * @return a future that is completed when subscription request is completed. |
53 | */ | 64 | */ |
54 | - CompletableFuture<Void> subscribe(Consumer<T> callback); | 65 | + default CompletableFuture<Void> subscribe(Consumer<T> callback) { |
66 | + return subscribe(callback, MoreExecutors.directExecutor()); | ||
67 | + } | ||
55 | 68 | ||
56 | /** | 69 | /** |
57 | * Unsubscribes from this topic. | 70 | * Unsubscribes from this topic. | ... | ... |
... | @@ -17,6 +17,7 @@ package org.onosproject.store.primitives.impl; | ... | @@ -17,6 +17,7 @@ package org.onosproject.store.primitives.impl; |
17 | 17 | ||
18 | import java.util.Map; | 18 | import java.util.Map; |
19 | import java.util.concurrent.CompletableFuture; | 19 | import java.util.concurrent.CompletableFuture; |
20 | +import java.util.concurrent.Executor; | ||
20 | import java.util.function.Consumer; | 21 | import java.util.function.Consumer; |
21 | 22 | ||
22 | import org.onosproject.store.service.AsyncAtomicValue; | 23 | import org.onosproject.store.service.AsyncAtomicValue; |
... | @@ -61,8 +62,9 @@ public class DefaultDistributedTopic<T> implements Topic<T> { | ... | @@ -61,8 +62,9 @@ public class DefaultDistributedTopic<T> implements Topic<T> { |
61 | } | 62 | } |
62 | 63 | ||
63 | @Override | 64 | @Override |
64 | - public CompletableFuture<Void> subscribe(Consumer<T> callback) { | 65 | + public CompletableFuture<Void> subscribe(Consumer<T> callback, Executor executor) { |
65 | - AtomicValueEventListener<T> valueListener = event -> callback.accept(event.newValue()); | 66 | + AtomicValueEventListener<T> valueListener = |
67 | + event -> executor.execute(() -> callback.accept(event.newValue())); | ||
66 | if (callbacks.putIfAbsent(callback, valueListener) == null) { | 68 | if (callbacks.putIfAbsent(callback, valueListener) == null) { |
67 | return atomicValue.addListener(valueListener); | 69 | return atomicValue.addListener(valueListener); |
68 | } | 70 | } | ... | ... |
-
Please register or login to post a comment