Vavr

Lazy view of java.util.Collection in Vavr

 ̄綄美尐妖づ 提交于 2021-01-27 13:14:08
问题 I have an existing api which uses java.util.Collection when returning values. I would like to use those values in later parts of my program with Vavr but I don't want to use the eager methods like List.ofAll (because I do not want to traverse those Collection objects twice). My use case is something like this: List<Product> filter(java.util.Collection products) { return List.lazyOf(products).filter(pred1); } Is it possible? 回答1: Since the input collection to the method is a java Collection ,

图解resilience4j容错机制

早过忘川 提交于 2020-08-09 10:33:23
Resilience4j是一个轻量级、易于使用的容错库,其灵感来自Netflix Hystrix,但专为Java 8和函数式编程设计。轻量级,因为库只使用Vavr,它没有任何其他外部库依赖项。相比之下,Netflix Hystrix对Archaius有一个编译依赖关系,Archaius有更多的外部库依赖关系,如Guava和Apache Commons。 Resilience4j提供高阶函数(decorators)来增强任何功能接口、lambda表达式或方法引用,包括断路器、速率限制器、重试或舱壁。可以在任何函数接口、lambda表达式或方法引用上使用多个装饰器。优点是您可以选择所需的装饰器,而无需其他任何东西。 有了Resilience4j,你不必全力以赴,你可以选择你需要的。 https://resilience4j.readme.io/docs/getting-started 概览 本文将介绍resilience4j中的四种容错机制,不过鉴于容错机制原理的通用性,后文所介绍的这几种容错机制也可以脱离resilience4j而独立存在(你完全可以自己编码实现它们或者采用其他类似的第三方库,如 Netflix Hystrix )。下面将会用图例来解释舱壁( Bulkhead )、断路器( CircuitBreaker )、限速器( RateLimiter )、重试( Retry

Spring Boot2+Resilience4j实现容错之Bulkhead

自古美人都是妖i 提交于 2020-07-27 22:28:04
Resilience4j是一个轻量级、易于使用的容错库,其灵感来自Netflix Hystrix,但专为Java 8和函数式编程设计。轻量级,因为库只使用Vavr,它没有任何其他外部库依赖项。相比之下,Netflix Hystrix对Archaius有一个编译依赖关系,Archaius有更多的外部库依赖关系,如Guava和Apache Commons。 Resilience4j提供高阶函数(decorators)来增强任何功能接口、lambda表达式或方法引用,包括断路器、速率限制器、重试或舱壁。可以在任何函数接口、lambda表达式或方法引用上使用多个装饰器。优点是您可以选择所需的装饰器,而无需其他任何东西。 有了Resilience4j,你不必全力以赴,你可以选择你需要的。 https://resilience4j.readme.io/docs/getting-started 概览 Resilience4j提供了两种舱壁模式 (Bulkhead) ,可用于限制并发执行的次数: SemaphoreBulkhead(信号量舱壁,默认),基于Java并发库中的Semaphore实现。 FixedThreadPoolBulkhead(固定线程池舱壁),它使用一个有界队列和一个固定线程池。 本文将演示在Spring Boot2中集成Resilience4j库

Lock-free atomic update to immutable Map

巧了我就是萌 提交于 2020-06-25 05:18:26
问题 Given a Javaslang / Vavr immutable map, and a function that updates that map: private Map<Foo, Bar> myMap = HashMap.empty(); public void setBar(Foo foo, Bar bar) { myMap = myMap.put(foo, bar); } How can I ensure that two concurrent calls to setBar() for different Foo keys will both have their updates recorded? // thread A setBar(fooA, barA) // thread B setBar(fooB, barB) It seems like there's a risk that the calls will be interleaved such that: thread A gets {} thread B gets {} thread B

Vavr Option:Java Optional 的另一个选项

房东的猫 提交于 2020-05-01 17:41:33
每当涉及Java,总会有很多选项。 这篇文章讨论了 Java 基础类 Optional 用法,与 Vavr 中的对应方法进行比较。Java 8最早引入了 Optional,把它定义为“一种容器对象,可以存储 null 或非 null 值”。 通常,在返回值可能为null的地方,会出现NullPointerException。开发人员可以使用 Optional 避免 null 值检查。在这种情况下,Optional 提供了一些方便的功能。但可惜的是,Java 8并没有包含所有功能。Optional中的某些功能需要使用 Java 11。要解决这类问题还可以使用 Vavr Option类。 本文将介绍如何使用 Java Optional类,并与 Vavr Option 进行比较。注意:示例代码要求使用Java 11及更高版本。所有代码在 Vavr0.10.2环境下完成测试。 让我们开始吧。 Java Optional 简介 Optional 并不是什么新概念,像 Haskell、Scala 这样的函数式编程语言已经提供了实现。调用方法后,返回值未知或者不存在(比如 null)的情况下,用 Optional 处理非常好用。下面通过实例进行介绍。 新建 Optional 实例 首先,需要获得 Optional 实例,有以下几种方法可以新建 Optional 实例。不仅如此

Vavr User Guide中英对照版

你说的曾经没有我的故事 提交于 2020-04-07 05:53:31
Vavr User Guide(Vavr用户指南) Daniel Dietrich, Robert Winkler - Version 0.9.2,2018-10-01 0. Vavr Vavr 是Java 8 的对象函数式扩展,目标是减少代码行数,提高代码质量,提供了持久化集合、错误处理函数式抽象、模式匹配等等。 Vavr 融合了面向对象编程的强大功能,具有功能编程的优雅性和坚固性。 最有趣的部分是拥有功能丰富且持久的集合库,可以与 Java 的标准集合顺利集成。 1. Introduction(简介) Vavr (formerly called Javaslang) is a functional library for Java 8+ that provides persistent data types and functional control structures. Vavr(以前称为Javaslang)是Java 8+的函数库,它提供持久数据类型和函数式控制结构。 1.1. Functional Data Structures in Java 8 with Vavr Java 8’s lambdas (λ) empower us to create wonderful API’s. They incredibly increase the

Vavr property testing

泪湿孤枕 提交于 2019-12-08 02:20:56
问题 Property checking feature is advertised in the latest Vavr documentation along with the following example of its usage: Arbitrary<Integer> ints = Arbitrary.integer(); // square(int) >= 0: OK, passed 1000 tests. Property.def("square(int) >= 0") .forAll(ints) .suchThat(i -> i * i >= 0) .check() .assertIsSatisfied(); However, as per the library's javadoc, neither Arbitrary generator nor Property type exist. What am I missing, if any? Is the documentation up to date? 回答1: Turns out, the following

How to correctly use VAVR collections to be thread safe?

大城市里の小女人 提交于 2019-12-07 12:24:24
问题 VAVR collections are "immutable". So, if I have static variable, for example, holding all the WebSocket sessions, how would I use VAVR so that the collection is thread safe? For example: @ServerEndpoint("/actions") public class DeviceWebSocketServer { private static Set<Session> sessions = //???; // how should I initialize this? @OnOpen public void open(Session session) { sessions = sessions.add(session); // is this OK??? } @OnClose public void close(Session session) { sessions = sessions

Vavr property testing

有些话、适合烂在心里 提交于 2019-12-06 13:37:15
Property checking feature is advertised in the latest Vavr documentation along with the following example of its usage: Arbitrary<Integer> ints = Arbitrary.integer(); // square(int) >= 0: OK, passed 1000 tests. Property.def("square(int) >= 0") .forAll(ints) .suchThat(i -> i * i >= 0) .check() .assertIsSatisfied(); However, as per the library's javadoc , neither Arbitrary generator nor Property type exist. What am I missing, if any? Is the documentation up to date? Turns out, the following vavr-test dependency was missing, which is not obvious from Vavr documentation: <dependency> <groupId>io

聊聊resilience4j的fallback

℡╲_俬逩灬. 提交于 2019-12-05 18:58:11
序 本文主要研究一下resilience4j的fallback 使用实例 @Test public void testFallback(){ // Execute the decorated supplier and recover from any exception String result = Try.ofSupplier(() -> backendService.doSomethingThrowException()) .recover(throwable -> "Hello from Recovery").get(); System.out.println(result); } Try vavr-0.9.2-sources.jar!/io/vavr/control/Try.java /** * The Try control gives us the ability write safe code without focusing on try-catch blocks in the presence of exceptions. * <p> * The following exceptions are considered to be fatal/non-recoverable: * <ul> * <li>{@linkplain