0

I am trying to create a service that will be able to execute some task (a method) and retry it that task fails.

For creating timers I am using a timer service with quartz (I am not able to change the timer service).

Timer service has method

this.timerService.createTimerRunRepeatedly(
            handledId,
            timerId,
            optionalDescription,
            totalFireCount,
            intervalMiliSeconds,
            offsetMiliseconds,
            optionalCallbackData);

It is also able to register a handler for created timer.

this.timerService.registerHandler(
                  handlerId,
                  firedCallback,
                  expiredCallback);

firedCallback -> (optional) callback that is called each time a timer with this handler fires (final Consumer<T> firedCallback)

expiredCallback -> (optional) callback that is called after a timer with this handler has fired the last time (final Consumer<T> expiredCallback)

I have created a new TaskRetryService but I have no idea how can I pass some method to this retryService for it to be executed.

Omore
  • 614
  • 6
  • 18
mirzak
  • 1,043
  • 4
  • 15
  • 30

2 Answers2

1

Since you are using Spring consider using Spring-Retry which has out-of-box feature(s) to handle failure and retry.

By annotating method with @Retryable(SomeException.class) the method will be retried when SomeException is raised. It can be further customized to specify the retry attempts, delays (random & exponential) between attempts as @Retryable(maxAttempts=12, backoff=@Backoff(delay=100, maxDelay=500))

For failure callback @Recover annotated method will be invoked.

For more implementation notes check the readme at github and this article.

Hope this helps!

Community
  • 1
  • 1
Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
0

Actually I managed to make it work since I had everything I needed. Just registered a handler

 @PostConstruct
 public void onPostConstruct() {
    handler = this.retryService.registerHandler("1", this::fireCallback, this::expiredCallback);
 }

Callbacks are

private <T> void fireCallback(final T t) {
    System.out.println("fireCallback");
}
private <T> void expiredCallback(final T t) {
    System.out.println("expiredCallback");
}

And created timer with

this.retryService.retryTask(handler, "12", 1, 5);

In retry service I have

public void retryTask(final HandlerId<String> handlerId,
                      final String timerId,
                      final Integer intervalTimeMinutes,
                      final Integer retryTimeMinutes) {
    this.timerService.createTimerRunRepeatedly(handlerId,
            timerId,
            "timer",
            getTotalFireCount(intervalTimeMinutes, retryTimeMinutes),
            (int) TimeUnit.MINUTES.toMillis(intervalTimeMinutes),
            0,
            null).thenAccept(timerWasCreated -> {
        if (timerWasCreated) {
            System.out.println("Timer was created");
        } else {
            System.out.println("Error while creating timer");
        }
    });
}

And

public <T> HandlerId<T> registerHandler(final String handlerId,
                                        final Consumer<T> firedCallback,
                                        final Consumer<T> expiredCallback) {
    return this.timerService.registerHandler(handlerId, firedCallback, expiredCallback);
}
mirzak
  • 1,043
  • 4
  • 15
  • 30