简体   繁体   中英

What is the maximum Duration in Java Akka API?

I'm working with the Akka Java library (unfortunately, the Classic version). When implementing the fault tolerance Strategy, the documentation provide a snippet like this:

private static SupervisorStrategy strategy =
    new OneForOneStrategy(
        10,
        Duration.ofMinutes(1),        // <----- Note here!
        DeciderBuilder.match(...)
                      .build();

Moreover, it says: "Also, there are special values for these parameters. If you specify: -1 to maxNrOfRetries , and Duration.Inf() to withinTimeRange , then the child is always restarted without any limit". The problem is that, in my Java (openjdk-11), Duration.Inf() doesn't actually exist! .

I have read some SO answers that specify what is the effective maximum Duration, but Akka seem to not recognize them. The best I could do is to specify Duration.ofNanos(Long.MAX_VALUE) .

Am I missing something? Is there actually a value to input or is the Akka documentation just wrong?

The Duration.Inf() there isn't referring to java.time.Duration (even though the snippet above that in the docs does use the Java Duration API). Under the hood, Akka converts the Java Duration to a scala.concurrent.duration.Duration , which does have an Inf (the naming pattern indicates that it's a constant in Scala) which is defined to be greater than any FiniteDuration ). The Java API just has a constructor which takes java.time.Duration .

This should work:

new OneForOneStrategy(
    10,
    scala.concurrent.duration.Duration.Inf(),
    true,  // have to explicitly pass the default loggingEnabled
    DeciderBuilder.match(...).build()
);

The maximum value of a Java Duration is Duration.ofSeconds(Long.MAX_VALUE).plusNanos(999999999) . Note that inside Akka, when converted to Scala Duration this will be considered less than an infinite duration, but that probably doesn't matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM