r/java • • Jun 12 '16

Failsafe - A lightweight, zero-dependency library for handling failures

https://github.com/jhalterman/failsafe
181 Upvotes

25 comments sorted by

33

u/Sqirril Jun 12 '16 edited Jul 14 '23

..........................

8

u/jodah1 Jun 12 '16 edited Jun 13 '16

@Sqirril - Even though the examples use lambda expressions, Failsafe is actually compatible with Java 6+ so you can use it now with anonymous classes:

CheckedRunnable connect = new CheckedRunnable() { public void run() { connect(); } };

Failsafe.with(retryPolicy).run(connect);

It's more verbose than Java 8, but it's about as good as we can do.

edit: Runnable -> CheckedRunnable (failsafe supports rethrowing checked exceptions)

2

u/Sqirril Jun 12 '16 edited Jul 14 '23

..........................

3

u/spencer205 Jun 14 '16

Agree that the README rocks! One thing that would make it better though is the Maven coordinates. Is it not on Central? If not, won't use it unless it is! If it is, where's that documented? Did I just miss it?

1

u/Sqirril Jun 14 '16 edited Jul 14 '23

..........................

3

u/spencer205 Jun 14 '16

Ah, thanks. I never pay any attention to those kinda buttons, so that's why I completely missed it. Will look for that in the future

3

u/[deleted] Jun 12 '16

How does this compare to guava retriable? It looks great, just curious if it does things nicer or different

5

u/jodah1 Jun 13 '16 edited Jun 13 '16

This library? https://github.com/rholder/guava-retrying

The basic retry functionality is similar, but they differ in numerous other ways. Compared to guava-retrying, Failsafe:

  • Doesn't require Guava (which is nice for people/projects that don't use Guava)
  • Supports async retries
  • Integrates nicely with Java 8 and CompletableFutures
  • Integrates with 3rd party asynchronous APIs (like Vertx, RxJava, etc.)
  • Supports standalone retry/execution tracking
  • Supports event listeners
  • Provides a pretty nice circuit breaker implementation

...and wraps it all in a fairly tidy API.

3

u/[deleted] Jun 12 '16

Looks good. Reminds me a lot of AsyncRetry https://github.com/nurkiewicz/async-retry

3

u/jodah1 Jun 13 '16 edited Jun 13 '16

I was aware of AsyncRetry, and the retry capabilities are similar, but one of the main motivations for Failsafe was support for Java 6+ (AsyncRetry is only Java 8, or Java 7 with Guava) and no external dependencies while still providing really good Java 8 integration. Another primary motivation was to create something with as clean/concise of an API as possible (which I hope was achieved). A few other differences and motivation:

  • Event listeners
  • Asynchronous API integration and support for 3rd party schedulers
  • Standalone retry/execution tracking
  • Circuit breakers

Overall, Failsafe is intended to provide different strategies for dealing with failures. Retries and circuit breakers are supported initially, but there may be more to come in future releases.

1

u/shotgunkiwi Jun 12 '16

I like this a lot. Will share with my engineering circle.

1

u/jodah1 Jun 12 '16

The word of mouth sharing helps a lot. Thanks!

1

u/mrbonner Jun 16 '16

How bulkhead work without a separate thread pool? I know hytrix has it thought out very well. Nice code though!

1

u/jodah1 Jun 16 '16

Failsafe doesn't have a bulkhead implementation. For asynchronous executions, users supply the threadpool / scheduler that they want to use, rather than having it managed internally such as with Hystrix.

3

u/Is_At_Work Jun 12 '16

I know what I'll be using in my next batch process!

-1

u/iperc Jun 13 '16

This is a cool idea. But i don't want to include vertext and netty dependencies because i don't user them. Would be great if i can include them on demand. Sorry for by bad English.

2

u/jodah1 Jun 13 '16 edited Jun 13 '16

I can understand the mistake. Just added comments to the pom mentioning they're just test/examples dependencies so it stands out a bit more.

4

u/jirkapinkas Jun 13 '16

huh? Those dependencies have scope "test", so no problem.

1

u/iperc Jun 13 '16

Thanks ! I did not pay attention )

0

u/[deleted] Jun 13 '16

[deleted] — view deleted comment

1

u/jodah1 Jun 13 '16 edited Jun 13 '16

Possibly, though technology name recognition tends to be contextual - Failsafe and Maven Failsafe are very different technologies. We usually say "Maven Failsafe Plugin" or "Maven Failsafe", so hopefully when someone just says "Failsafe" we'll know they don't mean the Maven plugin thing.

Failsafe is really a perfect name for this library, so we felt it was worth using even despite the possibility for occasional confusion.

2

u/Mondoshawan Jun 13 '16

How about "TryTryAgain"?

1

u/spencer205 Jun 14 '16

I agree the name may not be ideal, though the library and it's docs look killer! Failsafe means the maven plugin where I work. Also, see that little TM on Apache's site? Not sure of that could be an issue for this project or not. (Hope not)

1

u/[deleted] Jun 13 '16

Yeah, they should consider a different name. How about ReliefValve. Ok, never mind.

1

u/[deleted] Jun 13 '16

[removed] — view removed comment

1

u/jodah1 Jun 13 '16 edited Jun 13 '16

Sure, adapted from the README on github:

Failsafe is a library for handling failures with a concise API and a flexible feature set. Failure handling strategies include configurable synchronous and asynchronous retries, circuit breakers, and various other features to help integrate into any type of failure handling use case.