scalafinagletwitter-finagle

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


I've Java polyglot application which mainly has Java code and uses few Scala libraries as well.

The Scala below code compiles fine.

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")

I write the same code in java as below

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");
    }
}

When I compile the above java code, I get below errors -

[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

The project in github is here.

Below are dependency details of my project -

Scala Version - 2.12.6

Java Version - 1.8.0_151

finagle version - 7.1.0

Any idea how can I make the java code work?


Solution

  • You forgot that in Java methods are called with brackets (). Without them you actually tried to call not methods (getters) but fields themselves and they have private access.

    Correct Java translation is

    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");