Search code examples
resilience4jopenfeign

Service is not being executed with CompletableFuture


I've been trying to use Resilience4j + OpenFeign to call services. When I get an error, the timeout is working properly, however, in case of success it always returning null and I couldn't find why. Can someone please help me on this?

Creating the builder:

    public AgentesAPI getAPI(String name, String url) {
        
        CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults(name);
        RateLimiter rateLimiter = RateLimiter.ofDefaults(name);

        FeignDecorators decorators = 
                FeignDecorators.builder()
                    .withCircuitBreaker(circuitBreaker)
                    .withRateLimiter(rateLimiter)
                    .withFallback(new AgentesAPIFallback())
                    .build();
        
        return Resilience4jFeign.builder(decorators)
                .logger(new Slf4jLogger())
                .logLevel(Level.BASIC)
                .target(AgentesAPI.class, url);
    }

Getting a CompletableFuture with RateLimiter:

public class AgentesAPITimeLimiterService {

    public CompletableFuture getCompletableFuture(String name, long miliseconds, Supplier supplier) {

        TimeLimiter timeLimiter = getTimeLimiter(name, miliseconds);
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();      

        return timeLimiter.executeCompletionStage(
                scheduler, () -> CompletableFuture.supplyAsync(supplier))
                .toCompletableFuture();
    }

    private TimeLimiter getTimeLimiter(String name, long miliseconds) {
        return TimeLimiter.of(name, 
                TimeLimiterConfig.custom().timeoutDuration(
                        Duration.ofMillis(miliseconds)).build());
    }
}

Calling the service and getting always null results:

    public Agencias getAgencias(Map<String, Integer> queryMap) {        
        try {
            CompletableFuture<Agencias> result = 
                    _agentesAPITimeLimiterService.getCompletableFuture(configuration.backendName(),
                            configuration.timeout(), (() -> agentesAPI.getAgencias(queryMap)));
            return result.get();
        } catch (Exception e) {
            throw new RuntimeException("Error getting Agencias!");
        }   
    }
public interface AgentesAPI {
    
    @RequestLine("POST /obtiene_agencias")
    @Headers("Content-Type: application/x-www-form-urlencoded")
    Agencias getAgencias(@QueryMap Map<String, Integer> queryMap);
}

The API is returning something, however, I couldn't make work in this way. Is there something that I'm missing here?

I'm using the versions below:

    compileInclude group: "io.github.openfeign", name: "feign-core", version: "11.0"
    compileInclude group: "io.github.openfeign", name: "feign-gson", version: "11.0"
    compileInclude group: "io.github.openfeign", name: "feign-slf4j", version: "11.0"
    compileInclude group: "io.github.resilience4j", name: "resilience4j-all", version: "1.5.0"
    compileInclude group: "io.github.resilience4j", name: "resilience4j-feign", version: "1.5.0"
    
    compileInclude group: "io.github.resilience4j", name: "resilience4j-timelimiter", version: "1.5.0"
    compileInclude group: "org.slf4j", name: "slf4j-api", version: "1.7.30"
    compileInclude group: "org.slf4j", name: "slf4j-simple", version: "1.7.30"

Thanks in advance.


Solution

  • It was resolved adding a decoder to the builder.