Spring / Resilience4j Interview questions
How do you combine multiple Resilience4j decorators using the Decorators class?
The Decorators class chains .withX() calls onto a base Supplier, Function, or similar functional type, then materializes the whole composition with .decorate().
Supplier<String> decorated = Decorators.ofSupplier(() -> remoteCall()) .withBulkhead(bulkhead) .withCircuitBreaker(circuitBreaker) .withRetry(retry) .withFallback(Arrays.asList(CallNotPermittedException.class, TimeoutException.class), throwable -> "fallback-value") .decorate(); String result = decorated.get();
Each .with...() call wraps the previous result, so the order they're chained in is the order they're nested - the first one chained becomes the innermost wrapper, closest to the actual call, and the last one chained becomes outermost.
Because the whole thing resolves to a plain Supplier, the caller doesn't need to know or care how many patterns are layered underneath - it's called exactly like an undecorated one.
More Related questions...