• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.utils;
17 
18 import static software.amazon.awssdk.utils.Validate.paramNotNull;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import software.amazon.awssdk.annotations.SdkProtectedApi;
31 import software.amazon.awssdk.annotations.SdkTestInternalApi;
32 
33 /**
34  * Utilities that make it easier to create, use and destroy
35  * {@link ScheduledExecutor}s.
36  */
37 @SdkProtectedApi
38 public final class ScheduledExecutorUtils {
ScheduledExecutorUtils()39     private ScheduledExecutorUtils() {
40     }
41 
42     /**
43      * Wrap a scheduled executor in a type that cannot be closed, or shut down.
44      */
unmanagedScheduledExecutor(ScheduledExecutorService executor)45     public static ScheduledExecutorService unmanagedScheduledExecutor(ScheduledExecutorService executor) {
46         if (executor instanceof UnmanagedScheduledExecutorService) {
47             return executor;
48         }
49         if (executor == null) {
50             return null;
51         }
52         return new UnmanagedScheduledExecutorService(executor);
53     }
54 
55     /**
56      * Unwrap a scheduled executor. Requires the UnmanagedScheduledExecutorService to be the "outer" type.
57      */
unwrapUnmanagedScheduledExecutor(ScheduledExecutorService executor)58     public static ScheduledExecutorService unwrapUnmanagedScheduledExecutor(ScheduledExecutorService executor) {
59         if (executor instanceof UnmanagedScheduledExecutorService) {
60             return ((UnmanagedScheduledExecutorService) executor).delegate;
61         }
62         return executor;
63     }
64 
65     /**
66      * Wrapper around {@link ScheduledExecutorService} to prevent it from being
67      * closed. Used when the customer provides
68      * a custom scheduled executor service in which case they are responsible for
69      * the lifecycle of it.
70      */
71     @SdkTestInternalApi
72     public static final class UnmanagedScheduledExecutorService implements ScheduledExecutorService {
73 
74         private final ScheduledExecutorService delegate;
75 
UnmanagedScheduledExecutorService(ScheduledExecutorService delegate)76         UnmanagedScheduledExecutorService(ScheduledExecutorService delegate) {
77             this.delegate = paramNotNull(delegate, "ScheduledExecutorService");
78         }
79 
scheduledExecutorService()80         public ScheduledExecutorService scheduledExecutorService() {
81             return delegate;
82         }
83 
84         @Override
shutdown()85         public void shutdown() {
86             // Do nothing, this executor service is managed by the customer.
87         }
88 
89         @Override
shutdownNow()90         public List<Runnable> shutdownNow() {
91             return new ArrayList<>();
92         }
93 
94         @Override
isShutdown()95         public boolean isShutdown() {
96             return delegate.isShutdown();
97         }
98 
99         @Override
awaitTermination(long timeout, TimeUnit unit)100         public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
101             return delegate.awaitTermination(timeout, unit);
102         }
103 
104         @Override
schedule(Runnable command, long delay, TimeUnit unit)105         public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
106             return delegate.schedule(command, delay, unit);
107         }
108 
109         @Override
schedule(Callable<V> callable, long delay, TimeUnit unit)110         public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
111             return delegate.schedule(callable, delay, unit);
112         }
113 
114         @Override
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)115         public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
116             return delegate.scheduleAtFixedRate(command, initialDelay, period, unit);
117         }
118 
119         @Override
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)120         public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,
121                 TimeUnit unit) {
122             return delegate.scheduleWithFixedDelay(command, initialDelay, delay, unit);
123         }
124 
125         @Override
isTerminated()126         public boolean isTerminated() {
127             return delegate.isTerminated();
128         }
129 
130         @Override
submit(Callable<T> task)131         public <T> Future<T> submit(Callable<T> task) {
132             return delegate.submit(task);
133         }
134 
135         @Override
submit(Runnable task, T result)136         public <T> Future<T> submit(Runnable task, T result) {
137             return delegate.submit(task, result);
138         }
139 
140         @Override
submit(Runnable task)141         public Future<?> submit(Runnable task) {
142             return delegate.submit(task);
143         }
144 
145         @Override
invokeAll(Collection<? extends Callable<T>> tasks)146         public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
147             return delegate.invokeAll(tasks);
148         }
149 
150         @Override
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)151         public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
152                 throws InterruptedException {
153             return delegate.invokeAll(tasks, timeout, unit);
154         }
155 
156         @Override
invokeAny(Collection<? extends Callable<T>> tasks)157         public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
158                 throws InterruptedException, ExecutionException {
159             return delegate.invokeAny(tasks);
160         }
161 
162         @Override
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)163         public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
164                 throws InterruptedException, ExecutionException, TimeoutException {
165             return delegate.invokeAny(tasks, timeout, unit);
166         }
167 
168         @Override
execute(Runnable command)169         public void execute(Runnable command) {
170             delegate.execute(command);
171         }
172     }
173 }
174