Skip to content

Commit 46a53d6

Browse files
committed
breaking Java DSL api changes (Duration)
1 parent 340ea15 commit 46a53d6

File tree

7 files changed

+84
-17
lines changed

7 files changed

+84
-17
lines changed

http-caching/src/main/scala/org/apache/pekko/http/caching/javadsl/LfuCacheSettings.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,24 @@ abstract class LfuCacheSettings private[http] () { self: LfuCachingSettingsImpl
3131
/* JAVA APIs */
3232
def getMaxCapacity: Int
3333
def getInitialCapacity: Int
34-
def getTimeToLive: Duration
35-
def getTimeToIdle: Duration
34+
35+
/**
36+
* Java API
37+
* <p>
38+
* In 2.0.0, the return type of this method changed from `scala.concurrent.duration.Duration`
39+
* to `java.time.Duration`.
40+
* </p>
41+
*/
42+
def getTimeToLive: java.time.Duration
43+
44+
/**
45+
* Java API
46+
* <p>
47+
* In 2.0.0, the return type of this method changed from `scala.concurrent.duration.Duration`
48+
* to `java.time.Duration`.
49+
* </p>
50+
*/
51+
def getTimeToIdle: java.time.Duration
3652

3753
def withMaxCapacity(newMaxCapacity: Int): LfuCacheSettings = self.copy(maxCapacity = newMaxCapacity)
3854
def withInitialCapacity(newInitialCapacity: Int): LfuCacheSettings = self.copy(initialCapacity = newInitialCapacity)

http-caching/src/main/scala/org/apache/pekko/http/caching/scaladsl/LfuCacheSettings.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.apache.pekko
1717
import pekko.annotation.DoNotInherit
1818
import pekko.http.caching.impl.settings.LfuCachingSettingsImpl
1919
import pekko.http.caching.javadsl
20+
import pekko.http.impl.util.JavaDurationConverter
2021
import pekko.http.scaladsl.settings.SettingsCompanion
2122
import com.typesafe.config.Config
2223

@@ -34,8 +35,8 @@ abstract class LfuCacheSettings private[http] () extends javadsl.LfuCacheSetting
3435

3536
final def getMaxCapacity: Int = self.maxCapacity
3637
final def getInitialCapacity: Int = self.initialCapacity
37-
final def getTimeToLive: Duration = self.timeToLive
38-
final def getTimeToIdle: Duration = self.timeToIdle
38+
final def getTimeToLive: java.time.Duration = JavaDurationConverter.toJava(self.timeToLive)
39+
final def getTimeToIdle: java.time.Duration = JavaDurationConverter.toJava(self.timeToIdle)
3940

4041
override def withMaxCapacity(newMaxCapacity: Int): LfuCacheSettings = self.copy(maxCapacity = newMaxCapacity)
4142
override def withInitialCapacity(newInitialCapacity: Int): LfuCacheSettings =

http-core/src/main/java/org/apache/pekko/http/javadsl/TimeoutAccess.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ public interface TimeoutAccess {
3232
*
3333
* <p>Due to the inherent raciness it is not guaranteed that the returned timeout was applied
3434
* before the previously set timeout has expired!
35+
*
36+
* <p>In 2.0.0, the return type of this method changed from `scala.concurrent.duration.Duration`
37+
* to `java.time.Duration`.
3538
*/
36-
scala.concurrent.duration.Duration getTimeout();
39+
java.time.Duration getTimeout();
3740

3841
/**
3942
* Tries to set a new timeout. The timeout period is measured as of the point in time that the end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.pekko.http.impl.util
19+
20+
import java.time.temporal.ChronoUnit
21+
22+
import org.apache.pekko
23+
import pekko.annotation.InternalApi
24+
25+
import scala.concurrent.duration.FiniteDuration
26+
import scala.jdk.DurationConverters._
27+
28+
/**
29+
* Internal Pekko HTTP API
30+
*/
31+
@InternalApi
32+
private[http] object JavaDurationConverter {
33+
def toJava(d: scala.concurrent.duration.Duration): java.time.Duration = d match {
34+
case fd: scala.concurrent.duration.FiniteDuration => fd.toJava
35+
case scala.concurrent.duration.Duration.Inf => ChronoUnit.FOREVER.getDuration
36+
case scala.concurrent.duration.Duration.MinusInf => ChronoUnit.FOREVER.getDuration.negated()
37+
case scala.concurrent.duration.Duration.Undefined => ChronoUnit.FOREVER.getDuration
38+
}
39+
}

http-core/src/main/scala/org/apache/pekko/http/scaladsl/TimeoutAccess.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ package org.apache.pekko.http.scaladsl
1515

1616
import org.apache.pekko
1717
import pekko.annotation.DoNotInherit
18+
import pekko.http.impl.util.JavaDurationConverter
19+
import pekko.http.scaladsl.model.{ HttpRequest, HttpResponse }
1820

1921
import scala.concurrent.duration.Duration
20-
import pekko.http.scaladsl.model.{ HttpRequest, HttpResponse }
2122

2223
/**
2324
* Enables programmatic access to the server-side request timeout logic.
@@ -37,8 +38,7 @@ trait TimeoutAccess extends pekko.http.javadsl.TimeoutAccess {
3738
*/
3839
def timeout: Duration
3940

40-
/** Java API */
41-
def getTimeout: Duration = timeout
41+
override def getTimeout: java.time.Duration = JavaDurationConverter.toJava(timeout)
4242

4343
/**
4444
* Tries to set a new timeout.

http/src/main/scala/org/apache/pekko/http/javadsl/server/directives/TimeoutDirectives.scala

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ package org.apache.pekko.http.javadsl.server.directives
1616
import java.time.{ Duration => JDuration }
1717
import java.util.function.{ Function => JFunction, Supplier }
1818

19-
import scala.concurrent.duration.Duration
19+
import scala.jdk.DurationConverters._
2020

2121
import org.apache.pekko
22+
import pekko.http.impl.util.JavaDurationConverter
23+
import pekko.http.impl.util.JavaMapping.Implicits._
2224
import pekko.http.javadsl.model.{ HttpRequest, HttpResponse }
2325
import pekko.http.javadsl.server.Route
2426
import pekko.http.scaladsl.server.{ Directives => D }
25-
import pekko.util.JavaDurationConverters._
26-
27-
import pekko.http.impl.util.JavaMapping.Implicits._
2827

2928
abstract class TimeoutDirectives extends WebSocketDirectives {
3029

31-
def extractRequestTimeout(inner: JFunction[Duration, Route]): RouteAdapter = RouteAdapter {
32-
D.extractRequestTimeout { timeout => inner.apply(timeout).delegate }
30+
/**
31+
* Java API
32+
* <p>
33+
* In 2.0.0, the input function changed from expecting `scala.concurrent.duration.Duration`
34+
* as input to `java.time.Duration`.
35+
* </p>
36+
*/
37+
def extractRequestTimeout(inner: JFunction[java.time.Duration, Route]): RouteAdapter = RouteAdapter {
38+
D.extractRequestTimeout { timeout =>
39+
inner.apply(JavaDurationConverter.toJava(timeout)).delegate
40+
}
3341
}
3442

3543
/**
@@ -55,7 +63,7 @@ abstract class TimeoutDirectives extends WebSocketDirectives {
5563
*/
5664
def withRequestTimeout(timeout: JDuration, inner: Supplier[Route]): RouteAdapter =
5765
RouteAdapter {
58-
D.withRequestTimeout(timeout.asScala) { inner.get.delegate }
66+
D.withRequestTimeout(timeout.toScala) { inner.get.delegate }
5967
}
6068

6169
/**
@@ -83,7 +91,7 @@ abstract class TimeoutDirectives extends WebSocketDirectives {
8391
def withRequestTimeout(timeout: JDuration,
8492
timeoutHandler: JFunction[HttpRequest, HttpResponse],
8593
inner: Supplier[Route]): RouteAdapter = RouteAdapter {
86-
D.withRequestTimeout(timeout.asScala, in => timeoutHandler(in.asJava).asScala) { inner.get.delegate }
94+
D.withRequestTimeout(timeout.toScala, in => timeoutHandler(in.asJava).asScala) { inner.get.delegate }
8795
}
8896

8997
def withoutRequestTimeout(inner: Supplier[Route]): RouteAdapter = RouteAdapter {

http/src/main/scala/org/apache/pekko/http/scaladsl/server/directives/TimeoutDirectives.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ trait TimeoutDirectives {
3434
*/
3535
def extractRequestTimeout: Directive1[Duration] = Directive { inner => ctx =>
3636
val timeout = ctx.request.header[`Timeout-Access`] match {
37-
case Some(t) => t.timeoutAccess.getTimeout
37+
case Some(t) => t.timeoutAccess.timeout
3838
case _ =>
3939
ctx.log.warning("extractRequestTimeout was used in route however no request-timeout is set!")
4040
Duration.Inf

0 commit comments

Comments
 (0)