-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor retry code; configure keystore without relying on System pro…
…perties
- Loading branch information
Showing
5 changed files
with
174 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Logback GELF - zero dependencies Logback GELF appender library. | ||
* Copyright (C) 2024 Oliver Siegmar | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
package de.siegmar.logbackgelf; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.BooleanSupplier; | ||
|
||
final class RetryUtil { | ||
|
||
private RetryUtil() { | ||
// Utility class | ||
} | ||
|
||
@SuppressWarnings("checkstyle:IllegalCatch") | ||
public static <T> T retry(final Callable<T> action, final BooleanSupplier retryCondition, final int maxRetries, | ||
final long retryDelay) { | ||
int retryCount = 0; | ||
while (true) { | ||
try { | ||
return action.call(); | ||
} catch (final Exception e) { | ||
retryCount++; | ||
if (retryCount > maxRetries || !retryCondition.getAsBoolean()) { | ||
rethrow(e); | ||
} | ||
|
||
try { | ||
Thread.sleep(retryDelay); | ||
} catch (final InterruptedException ie) { | ||
Thread.currentThread().interrupt(); | ||
rethrow(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void rethrow(final Exception e) { | ||
if (e instanceof RuntimeException) { | ||
throw (RuntimeException) e; | ||
} | ||
|
||
throw new IllegalStateException(e); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Logback GELF - zero dependencies Logback GELF appender library. | ||
* Copyright (C) 2024 Oliver Siegmar | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
package de.siegmar.logbackgelf; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class RetryUtilTest { | ||
|
||
private final AtomicInteger counter = new AtomicInteger(); | ||
|
||
@Test | ||
void shouldRetryUntilMaxRetries() { | ||
assertThatThrownBy(() -> RetryUtil.retry(() -> incCounter(counter), () -> true, 5, 1)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Retry"); | ||
assertThat(counter).hasValue(6); | ||
} | ||
|
||
@Test | ||
void shouldRetryUntilMaxRetriesWhenConditionIsFalse() { | ||
assertThatThrownBy(() -> RetryUtil.retry(() -> incCounter(counter), () -> false, 5, 1)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Retry"); | ||
assertThat(counter).hasValue(1); | ||
} | ||
|
||
private static int incCounter(final AtomicInteger counter) { | ||
final int i = counter.incrementAndGet(); | ||
if (i > 0) { | ||
throw new IllegalStateException("Retry"); | ||
} | ||
return i; | ||
} | ||
|
||
} |
Binary file not shown.