]> git.basschouten.com Git - openhab-addons.git/blob
81fab27081375729912ac14d47d7ce3f8ff1ea55
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.webservice.retry;
14
15 import java.util.function.Consumer;
16 import java.util.function.Supplier;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Interface for strategies implementing the retry behavior of requests against the Miele cloud.
23  *
24  * @author Björn Lange - Initial contribution
25  */
26 @NonNullByDefault
27 public interface RetryStrategy {
28     /**
29      * Performs an operation which may be retried several times.
30      *
31      * If retrying fails or a critical error occurred, this method may throw {@link Exception}s of any type.
32      *
33      * @param operation The operation to perform. To signal that an error can be resolved by retrying this operation it
34      *            should throw an {@link Exception}. Whether the operation is retried is up to the {@link RetryStrategy}
35      *            implementation.
36      * @param onException Handler to invoke when an {@link Exception} is handled by retrying the {@code operation}. This
37      *            handler should at least log a message. It must not throw any exception.
38      * @return The object returned by {@code operation} if it completed successfully.
39      */
40     <@Nullable T> T performRetryableOperation(Supplier<T> operation, Consumer<Exception> onException);
41
42     /**
43      * Performs an operation which may be retried several times.
44      *
45      * If retrying fails or a critical error occurred, this method may throw {@link Exception}s of any type.
46      *
47      * @param operation The operation to perform. To signal that an error can be resolved by retrying this operation it
48      *            should throw an {@link Exception}. Whether the operation is retried is up to the {@link RetryStrategy}
49      *            implementation
50      * @param onException Handler to invoke when an {@link Exception} is handled by retrying the {@code operation}. This
51      *            handler should at least log a message. It may not throw any exception.
52      */
53     default void performRetryableOperation(Runnable operation, Consumer<Exception> onException) {
54         performRetryableOperation(new Supplier<@Nullable Void>() {
55             @Override
56             public @Nullable Void get() {
57                 operation.run();
58                 return null;
59             }
60         }, onException);
61     }
62 }