|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.nosql.async; |
| 20 | + |
| 21 | +import static java.util.concurrent.Executors.callable; |
| 22 | + |
| 23 | +import jakarta.enterprise.context.ApplicationScoped; |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.concurrent.Callable; |
| 26 | + |
| 27 | +/** |
| 28 | + * Abstraction for platform/environment-specific scheduler implementations for delayed and |
| 29 | + * optionally repeated executions. |
| 30 | + * |
| 31 | + * <p>Quarkus production systems use Vert.x, tests usually use Java executors. |
| 32 | + * |
| 33 | + * <p>Implementations, like Java executors or Vert.X, are usually {@link |
| 34 | + * ApplicationScoped @ApplicationScoped} in CDI environments. |
| 35 | + */ |
| 36 | +public interface AsyncExec { |
| 37 | + |
| 38 | + default <R> Cancelable<R> submit(Callable<R> callable) { |
| 39 | + return schedule(callable, Duration.ZERO); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Asynchronously run the given {@linkplain Callable callable} after the provided {@linkplain |
| 44 | + * Duration delay}. If the delay is not positive, the function is scheduled for immediate |
| 45 | + * execution. |
| 46 | + * |
| 47 | + * @param callable the callable to execute |
| 48 | + * @param delay the execution delay, zero and negative values mean immediate scheduling |
| 49 | + * @param <R> return type of the callable propagated through the returned cancelable |
| 50 | + * @return the cancelable for the scheduled task |
| 51 | + */ |
| 52 | + <R> Cancelable<R> schedule(Callable<R> callable, Duration delay); |
| 53 | + |
| 54 | + /** |
| 55 | + * This is a convenience function for {@link #schedule(Callable, Duration)} with a void result, |
| 56 | + * using a {@link Runnable}. |
| 57 | + */ |
| 58 | + default Cancelable<Void> schedule(Runnable runnable, Duration delay) { |
| 59 | + return schedule(callable(runnable, null), delay); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Schedules a runnable to be executed repeatedly using the given initial delay. This is |
| 64 | + * equivalent to calling {@link #schedulePeriodic(Runnable, Duration, Duration)} with the {@code |
| 65 | + * initialDelay} and {@code delay} having the same values. |
| 66 | + * |
| 67 | + * <p>There is intentionally no variant of {@code schedulePeriodic()} that takes a {@link Callable |
| 68 | + * Callable<R>} because there are multiple invocations of the runnable. |
| 69 | + */ |
| 70 | + default Cancelable<Void> schedulePeriodic(Runnable runnable, Duration delay) { |
| 71 | + return schedulePeriodic(runnable, delay, delay); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Schedules a runnable to be executed repeatedly, starting after the given initial delay. |
| 76 | + * |
| 77 | + * <p>There is intentionally no variant of {@code schedulePeriodic()} that takes a {@link Callable |
| 78 | + * Callable<R>} because there are multiple invocations of the runnable. |
| 79 | + * |
| 80 | + * @param runnable the runnable to execute |
| 81 | + * @param initialDelay initial delay, zero and negative values mean immediate scheduling |
| 82 | + * @param delay repetition delay, zero and negative cause an {@link IllegalArgumentException} |
| 83 | + * @return cancelable instance |
| 84 | + */ |
| 85 | + Cancelable<Void> schedulePeriodic(Runnable runnable, Duration initialDelay, Duration delay); |
| 86 | +} |
0 commit comments