blob: e6290b89b6bf4bc0b941716ebc27511bdeba55f3 [file] [log] [blame]
Jordan Halterman9bdc24f2017-04-19 23:45:12 -07001/*
2 * Copyright 2017-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.primitives.impl;
17
18import java.util.Collection;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.Executor;
21import java.util.function.Consumer;
22
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070023import org.onosproject.store.service.AsyncAtomicValue;
24import org.onosproject.store.service.Task;
25import org.onosproject.store.service.WorkQueue;
26import org.onosproject.store.service.WorkQueueStats;
27
28/**
29 * {@link AsyncAtomicValue} that executes asynchronous callbacks on a user provided
30 * {@link Executor}.
31 */
32public class ExecutingWorkQueue<E> extends ExecutingDistributedPrimitive implements WorkQueue<E> {
33 private final WorkQueue<E> delegateQueue;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070034
Jordan Halterman046faeb2017-05-01 15:10:13 -070035 public ExecutingWorkQueue(WorkQueue<E> delegateQueue, Executor orderedExecutor, Executor threadPoolExecutor) {
36 super(delegateQueue, orderedExecutor, threadPoolExecutor);
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070037 this.delegateQueue = delegateQueue;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070038 }
39
40 @Override
41 public CompletableFuture<Void> addMultiple(Collection<E> items) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070042 return asyncFuture(delegateQueue.addMultiple(items));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070043 }
44
45 @Override
46 public CompletableFuture<Collection<Task<E>>> take(int maxItems) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070047 return asyncFuture(delegateQueue.take(maxItems));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070048 }
49
50 @Override
51 public CompletableFuture<Void> complete(Collection<String> taskIds) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070052 return asyncFuture(delegateQueue.complete(taskIds));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070053 }
54
55 @Override
56 public CompletableFuture<Void> registerTaskProcessor(
57 Consumer<E> taskProcessor, int parallelism, Executor executor) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070058 return asyncFuture(delegateQueue.registerTaskProcessor(taskProcessor, parallelism, executor));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070059 }
60
61 @Override
62 public CompletableFuture<Void> stopProcessing() {
Jordan Halterman046faeb2017-05-01 15:10:13 -070063 return asyncFuture(delegateQueue.stopProcessing());
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070064 }
65
66 @Override
67 public CompletableFuture<WorkQueueStats> stats() {
Jordan Halterman046faeb2017-05-01 15:10:13 -070068 return asyncFuture(delegateQueue.stats());
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070069 }
70}