Scala 2.12 与 java 1.8 的互操作不能为 twitter finagle 代码编译

Scala 2.12 interop with java 1.8 doesn't compile for twitter finagle code

我有 Java 多语言应用程序,主要有 Java 代码,也使用了很少的 Scala 库。

下面的 Scala 代码可以正常编译。

import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.finagle.tracing.Tracer
import com.twitter.util.Duration

private val loggerFinagle = java.util.logging.Logger.getLogger("FinagleLogger")

  val statsReceiver: StatsReceiver = ???
  val tracer: Tracer = ???
  val requestTimeout: Duration = ???
  val connectTimeout: Duration = ???

  val client: Service[Request, Response] = Http.client
    .withLabel("clientname")
    .withStatsReceiver(statsReceiver)
    .withTracer(tracer)
    .withRequestTimeout(requestTimeout)
    .withTransport.connectTimeout(???)
    .withSessionQualifier.noFailureAccrual
    .withSessionQualifier.noFailFast
    .withSession.acquisitionTimeout(connectTimeout)
    .withSessionPool.maxSize(1)
    .newService("localhost:10000,localhost:10001")

我在java中写了同样的代码如下

import com.twitter.finagle.Http;
import com.twitter.finagle.stats.StatsReceiver;
import com.twitter.finagle.tracing.Tracer;
import com.twitter.util.Duration;

public class JavaMain {
    public static void main(String[] args) {

        StatsReceiver statsReceiver  = null;
        Tracer tracer  = null;
        Duration requestTimeout  =null;
        Duration connectTimeout  = null;

        Http.client()
                .withLabel("clientname")
                .withStatsReceiver(statsReceiver)
                .withTracer(tracer)
                .withRequestTimeout(requestTimeout)
                .withTransport.connectTimeout(connectTimeout)
                .withSessionQualifier.noFailureAccrual()
                .withSessionQualifier.noFailFast()
                .withSession.acquisitionTimeout(connectTimeout)
                .withSessionPool.maxSize(1)
                .newService("localhost:10000,localhost:10001");
    }
}

当我编译上面的 java 代码时,出现以下错误 -

[info] Done updating.
[error] /Users/rajkumar.natarajan/eclipse-workspace/FinagleDemo/chapter1/src/main/java/JavaMain.java:19:1: withTransport has private access in com.twitter.finagle.Http.Client
[error]                 .withTransport.connectTimeout(connectTimeout)
[error] /Users/rajkumar.natarajan/eclipse-workspace/FinagleDemo/chapter1/src/main/java/JavaMain.java:20:1: withSessionQualifier has private access in com.twitter.finagle.Http.Client
[error]                 .withSessionQualifier.noFailureAccrual()
[error] /Users/rajkumar.natarajan/eclipse-workspace/FinagleDemo/chapter1/src/main/java/JavaMain.java:21:1: withSessionQualifier has private access in com.twitter.finagle.Http.Client
[error]                 .withSessionQualifier.noFailFast()
[error] /Users/rajkumar.natarajan/eclipse-workspace/FinagleDemo/chapter1/src/main/java/JavaMain.java:22:1: withSession has private access in com.twitter.finagle.Http.Client
[error]                 .withSession.acquisitionTimeout(connectTimeout)
[error] /Users/rajkumar.natarajan/eclipse-workspace/FinagleDemo/chapter1/src/main/java/JavaMain.java:23:1: withSessionPool has private access in com.twitter.finagle.Http.Client
[error]                 .withSessionPool.maxSize(1)
[error] (chapter1 / Compile / compileIncremental) javac returned non-zero exit code

github中的项目是here

下面是我的项目的依赖细节-

Scala 版本 - 2.12.6

Java 版本 - 1.8.0_151

finagle 版本 - 7.1.0

知道如何让 java 代码工作吗?

您忘记了在 Java 中方法是用方括号 () 调用的。没有它们,您实际上尝试调用的不是方法(吸气剂),而是字段本身,并且它们具有私有访问权限。

正确的Java翻译是

Http.client()
        .withLabel("clientname")
        .withStatsReceiver(statsReceiver)
        .withTracer(tracer)
        .withRequestTimeout(requestTimeout)
        .withTransport().connectTimeout(connectTimeout)
        .withSessionQualifier().noFailureAccrual()
        .withSessionQualifier().noFailFast()
        .withSession().acquisitionTimeout(connectTimeout)
        .withSessionPool().maxSize(1)
        .newService("localhost:10000,localhost:10001");