Взаимодействие Scala 2.12 с Java 1.8 не компилируется для кода twitter finagle - PullRequest
0 голосов
/ 19 мая 2018

У меня есть приложение Java polyglot, которое в основном содержит код 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 здесь .

Ниже приведены подробности зависимости моего проекта -

Версия Scala - 2.12.6

Версия Java - 1.8.0_151

Версия finagle - 7.1.0

Есть идеи, как заставить работать Java-код?

1 Ответ

0 голосов
/ 19 мая 2018

Вы забыли, что в 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");
...