![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
io.github.resilience4j:resilience4j-documentation
Advanced tools
Resilience4j is a lightweight, easy-to-use fault tolerance library designed for Java8 and functional programming
= Fault tolerance library designed for functional programming :author: Robert Winkler and Bohdan Storozhuk :hardbreaks: :icons:
image:https://travis-ci.org/resilience4j/resilience4j.svg?branch=master["Build Status", link="https://travis-ci.org/resilience4j/resilience4j"] image:https://coveralls.io/repos/github/resilience4j/resilience4j/badge.svg?branch=master&k=1["Coverage Status", link="https://coveralls.io/github/resilience4j/resilience4j?branch=master"] image:https://api.codacy.com/project/badge/Grade/f0295918d02b45d0928d5adc95f6eba1["Codacy code quality", link="https://www.codacy.com/app/robwin/resilience4j?utm_source=github.com&utm_medium=referral&utm_content=resilience4j/resilience4j&utm_campaign=Badge_Grade"] image:https://api.bintray.com/packages/resilience4j/Maven/resilience4j/images/download.svg[link="https://bintray.com/resilience4j/Maven/resilience4j/_latestVersion"] image:http://javadoc.io/badge/io.github.resilience4j/resilience4j.svg[JavaDoc, link="http://javadoc.io/doc/io.github.resilience4j/resilience4j"] image:http://img.shields.io/badge/license-ASF2-blue.svg["Apache License 2", link="http://www.apache.org/licenses/LICENSE-2.0.txt"]
== Introduction
Resilience4j is a lightweight, easy-to-use fault tolerance library inspired by https://github.com/Netflix/Hystrix[Netflix Hystrix], but designed for Java 8 and functional programming. Lightweight, because the library only uses http://www.vavr.io/[Vavr (formerly Javaslang)], which does not have any other external library dependencies. Netflix Hystrix, in contrast, has a compile dependency to https://github.com/Netflix/archaius[Archaius] which has many more external library dependencies such as Guava and Apache Commons Configuration. With Resilience4j you don't have to go all-in, you can pick what you need.
== Documentation
Setup and usage is described in our http://resilience4j.github.io/resilience4j/[User Guide].
== Overview
Resilience provides several core modules and add-on modules:
Core modules:
Add-on modules
To highlight a few differences to Netflix Hystrix:
Observable
or Flowable
with a Circuit Breaker, Bulkhead or Ratelimiter.== Spring Boot demo
Setup and usage in Spring Boot is demonstrated https://github.com/RobWin/resilience4j-spring-boot-demo[here].
== Usage examples
[[circuitbreaker]] === CircuitBreaker, Retry and Fallback
The following example shows how to decorate a lambda expression (Supplier) with a CircuitBreaker and how to retry the call at most 3 times when an exception occurs. You can configure the wait interval between retries and also configure a custom backoff algorithm. The example uses Vavr's Try Monad to recover from an exception and invoke another lambda expression as a fallback, when even all retries have failed.
// Simulates a Backend Service public interface BackendService { String doSomething(); }
// Create a CircuitBreaker (use default configuration) CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName"); // Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms Retry retry = Retry.ofDefaults("backendName");
// Decorate your call to BackendService.doSomething() with a CircuitBreaker Supplier decoratedSupplier = CircuitBreaker .decorateSupplier(circuitBreaker, backendService::doSomething);
// Decorate your call with automatic retry decoratedSupplier = Retry .decorateSupplier(retry, decoratedSupplier);
// Execute the decorated supplier and recover from any exception String result = Try.ofSupplier(decoratedSupplier) .recover(throwable -> "Hello from Recovery").get();
// When you don't want to decorate your lambda expression, // but just execute it and protect the call by a CircuitBreaker. String result = circuitBreaker.executeSupplier(backendService::doSomething);
The CircuitBreaker provides an interface to monitor metrics.
=== CircuitBreaker and RxJava
The following example shows how to decorate an Observable by using the custom RxJava operator.
NOTE: Resilience4j also provides RxJava operators for RateLimiter
, Bulkhead
and Retry
. Find out more in our http://resilience4j.github.io/resilience4j/[User Guide]
[[ratelimiter]] === RateLimiter
The following example shows how to restrict the calling rate of some method to be not higher than 1 req/sec.
// Create a custom RateLimiter configuration RateLimiterConfig config = RateLimiterConfig.custom() .timeoutDuration(Duration.ofMillis(100)) .limitRefreshPeriod(Duration.ofSeconds(1)) .limitForPeriod(1) .build(); // Create a RateLimiter RateLimiter rateLimiter = RateLimiter.of("backendName", config);
// Decorate your call to BackendService.doSomething() Supplier restrictedSupplier = RateLimiter .decorateSupplier(rateLimiter, backendService::doSomething);
// First call is successful Try firstTry = Try.ofSupplier(restrictedSupplier); assertThat(firstTry.isSuccess()).isTrue();
The RateLimiter provides an interface to monitor the number of available permissions. The AtomicRateLimiter has some enhanced Metrics with some implementation specific details.
RateLimiter.Metrics metrics = rateLimiter.getMetrics(); int numberOfThreadsWaitingForPermission = metrics.getNumberOfWaitingThreads(); // Estimates count of available permissions. Can be negative if some permissions where reserved. int availablePermissions = metrics.getAvailablePermissions();
[[bulkhead]] === Bulkhead The following example shows how to decorate a lambda expression with a Bulkhead. A Bulkhead can be used to limit the amount of parallel executions. This bulkhead abstraction should work well across a variety of threading and io models. It is based on a semaphore, and unlike Hystrix, does not provide "shadow" thread pool option.
Bulkhead bulkhead = Bulkhead.ofDefaults("backendName");
The Bulkhead provides an interface to monitor the current number of available concurrent calls.
[[cache]] === Cache
The following example shows how to decorate a lambda expression with a Cache abstraction. The cache abstraction puts the result of the lambda expression in a cache instance (JCache) and tries to retrieve a previous cached result from the cache before it invokes the lambda expression. If the cache retrieval from a distributed cache fails, the exception is taken care of and the lambda expression is called.
// Create a CacheContext by wrapping a JCache instance. javax.cache.Cache<String, String> cacheInstance = Caching.getCache("cacheName", String.class, String.class); Cache<String, String> cacheContext = Cache.of(cacheInstance);
The Cache provides an interface to monitor cache hits/misses.
[[metrics]] === Metrics
The following example shows how to decorate a lambda expression to measure metrics using Dropwizard Metrics. The Timer counts the number of total calls, successful calls, failed calls and measures the rate and response time of successful calls.
The Timer provides an interface to monitor metrics.
[[events]] == Consume emitted events
CircuitBreaker
, RateLimiter
, Cache
and Retry
components emit a stream of events which can be consumed.
CircuitBreaker
example below:
A CircuitBreakerEvent
can be a state transition, a successful call, a recorded error or an ignored error. All events contains additional information like event creation time and processing duration of the call. If you want to consume events, you have to register an event consumer.
You could use the CircularEventConsumer
to store events in a circular buffer with a fixed capacity.
You can use RxJava or Spring Reactor Adapters to convert the EventPublisher
into a Reactive Stream. The advantage of a Reactive Stream is that you can use RxJava's observeOn
operator to specify a different Scheduler that the CircuitBreaker will use to send notifications to its observers/consumers.
NOTE: You can also consume events from RateLimiter
, Bulkhead
, Cache
and Retry
. Find out more in our http://resilience4j.github.io/resilience4j/[User Guide]
== Companies who use Resilience4j
== License
Copyright 2017 Robert Winkler and Bohdan Storozhuk
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
FAQs
Resilience4j is a lightweight, easy-to-use fault tolerance library designed for Java8 and functional programming
We found that io.github.resilience4j:resilience4j-documentation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.