0

I am using the FailSafe library to handle failures and retries.

The current implementation creates the generic FailsafeExecutor on each run -

var result = Failsafe.with(Collections.singletonList(retryPolicy))
  .with(scheduler)
  .<R>getAsyncExecution(execution ->
    doSomething(x, y));

FaileSafe.with definition -

public static <R, P extends Policy<R>> FailsafeExecutor<R> with(P outerPolicy, P... policies)

Since this is called on a critical path, I would really like to avoid this (the recreation of the object).

I tried initiating the FailsafeExecutor on the class level (the class is not generic) with a <?> -

private final FailsafeExecutor<?> executor;

but then when calling the executor I receive - Type parameter 'R' is not within its bound; should extend 'capture<?>'

Is there any way to get over this? Some kind of way I could make this object reusable? (same with the RetryPolicy, but I guess if there is a solution for the first it would apply for the policy as well)

Thanks.

RanAbitbul
  • 13
  • 3
  • "*Since this is called on a critical path, I would really like to avoid this.*" - Why exactly do you want to avoid this? --- "*... but since it is generic and it does not know it's type until runtime.*" - This is not how generics works. The types are defined at compile-time, and, due to [type erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html), the types are gone at runtime. – Turing85 Dec 04 '22 at 09:06
  • @Turing85 This function is a part of a library that will be used on a very high scale. I wouldn't want to create the object each time it reaches this code. Regarding the second part - I have edited the post. I hope it makes more sense – RanAbitbul Dec 04 '22 at 09:19
  • @Turing85 I guess the question is more about an alternative for a reusable object within the library itself. You might want to configure an object with non-generic policies and reuse that for different types. – Itzhak Eretz Kdosha Dec 05 '22 at 07:24

0 Answers0