Skip to content

Commit

Permalink
Merge pull request #6 from synadia-io/start-retrier-0-1-0
Browse files Browse the repository at this point in the history
Start Retrier 0.1.0
  • Loading branch information
scottf authored May 24, 2024
2 parents b5ec207 + 87e3873 commit 0eaf881
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 20 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/pubx-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
build:
runs-on: ubuntu-latest
env:
BUILD_EVENT: ${{ github.event_name }}
BUILD_EVENT: "release"
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
Expand All @@ -31,12 +31,13 @@ jobs:
nats-server -v
- name: Check out code
uses: actions/checkout@v3
- name: Prepare Gradle
run: |
rm README.md
cp -r js-publish-extensions/* ./
chmod +x gradlew
- name: Compile and Test
run: ./gradlew clean test
run: |
pushd js-publish-extensions
chmod +x gradlew && ./gradlew clean test
popd
- name: Verify, Sign and Publish Release
run: ./gradlew -i signArchives signMavenJavaPublication publishToSonatype closeAndReleaseSonatypeStagingRepository
run: |
pushd js-publish-extensions
./gradlew -i signArchives signMavenJavaPublication publishToSonatype closeAndReleaseSonatypeStagingRepository
popd
2 changes: 1 addition & 1 deletion js-publish-extensions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
id 'signing'
}

def jarVersion = "0.0.9"
def jarVersion = "0.0.11"
group = 'io.synadia'

def isMerge = System.getenv("BUILD_EVENT") == "push"
Expand Down
4 changes: 4 additions & 0 deletions retrier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The retrier is a general purpose java utility that can execute any action (funct

**Current Release**: 0.0.1   **Current Snapshot**: 0.0.2-SNAPSHOT

For how to use, please see [RetrierExample.java](src/examples/java/io/synadia/examples/RetrierExample.java)

The [Unit Tests](src/test/java/io/synadia/jnats/extension/RetrierTests.java) may also be of interest.

[![License Apache 2](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)

Copyright (c) 2024 Synadia Communications Inc. All Rights Reserved.
Expand Down
2 changes: 1 addition & 1 deletion retrier/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
id 'signing'
}

def jarVersion = "0.0.11"
def jarVersion = "0.1.0"
group = 'io.synadia'

def isMerge = System.getenv("BUILD_EVENT") == "push"
Expand Down
10 changes: 0 additions & 10 deletions retrier/src/examples/java/io/synadia/examples/Placeholder.java

This file was deleted.

113 changes: 113 additions & 0 deletions retrier/src/examples/java/io/synadia/examples/RetrierExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) 2024 Synadia Communications Inc. All Rights Reserved.
// See LICENSE and NOTICE file for details.

package io.synadia.examples;

import io.synadia.jnats.extension.Retrier;
import io.synadia.jnats.extension.RetryAction;
import io.synadia.jnats.extension.RetryConfig;
import io.synadia.jnats.extension.RetryObserver;

public class RetrierExample {

private static String RECOVERABLE = "recoverable";
private static String UNRECOVERABLE = "unrecoverable";

public static void main(String[] args) {
RetryConfig retryConfig = RetryConfig.builder()
.attempts(5)
.deadline(5000)
.backoffPolicy(new long[]{100, 200, 300, 400, 500})
.build();

System.out.println("In this example, the action fails a few times, but eventually succeeds");
ExampleActionProvider actionProvider = new ExampleActionProvider(3);
ExampleRetryObserver observer = new ExampleRetryObserver();
try {
Retrier.execute(retryConfig, actionProvider, observer);
}
catch (Exception e) {
System.out.println("This was setup to eventually complete, should not get here");
System.exit(-1);
}
System.out.println(" Expecting 4 action executions: " + actionProvider.executions);
System.out.println(" Expecting 0 failuresLeft: " + actionProvider.failuresLeft);
System.out.println(" Expecting to observe 3 exceptions: " + observer.exceptions);

// ==========================================================================================

System.out.println("\nIn this example, the action fails more times than attempts allowed");
actionProvider = new ExampleActionProvider(6);
observer = new ExampleRetryObserver();
try {
Retrier.execute(retryConfig, actionProvider, observer);
}
catch (Exception e) {
System.out.println("This was expected, even though it is \"recoverable\" -> " + e);
}
System.out.println(" Expecting 6 action executions: " + actionProvider.executions);
System.out.println(" Expecting 1 failuresLeft: " + actionProvider.failuresLeft);
System.out.println(" Expecting to observe 5 exceptions: " + observer.exceptions);

// ==========================================================================================

System.out.println("\nIn this example, there is an unrecoverable exception, so the observer cancelled the retry.");
ExampleUnrecoverableActionProvider unProvider = new ExampleUnrecoverableActionProvider(3);
observer = new ExampleRetryObserver();
try {
Retrier.execute(retryConfig, unProvider, observer);
}
catch (Exception e) {
System.out.println("This was expected, it is \"urecoverable\" -> " + e);
}
System.out.println(" Expecting 4 action executions: " + unProvider.executions);
System.out.println(" Expecting 0 failuresLeft: " + unProvider.failuresLeft);
System.out.println(" Expecting to observe 4 exceptions: " + observer.exceptions);
}

static class ExampleActionProvider implements RetryAction<Boolean> {
public int failuresLeft;
public int executions;

public ExampleActionProvider(int failuresBeforeWorking) {
this.failuresLeft = failuresBeforeWorking + 1;
}

@Override
public Boolean execute() throws Exception {
executions++;
if (--failuresLeft > 0) {
throw new Exception(RECOVERABLE);
}
return true;
}
}

static class ExampleUnrecoverableActionProvider implements RetryAction<Boolean> {
public int failuresLeft;
public int executions;

public ExampleUnrecoverableActionProvider(int failuresBeforeUnrecoverable) {
this.failuresLeft = failuresBeforeUnrecoverable + 1;
}

@Override
public Boolean execute() throws Exception {
executions++;
if (--failuresLeft > 0) {
throw new Exception(RECOVERABLE);
}
throw new Exception(UNRECOVERABLE);
}
}

static class ExampleRetryObserver implements RetryObserver {
public int exceptions = 0;

@Override
public boolean shouldRetry(Exception e) {
exceptions++;
return !e.getMessage().contains(UNRECOVERABLE);
}
}
}

0 comments on commit 0eaf881

Please sign in to comment.