0

我有 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中的项目在这里

以下是我的项目的依赖项详细信息 -

斯卡拉版本 - 2.12.6

Java 版本 - 1.8.0_151

最终版本 - 7.1.0

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

4

1 回答 1

2

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

正确的 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");
于 2018-05-19T13:40:54.773 回答