我是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代码工作?
答案 0 :(得分:2)
您忘记在Java方法中使用括号()
调用。如果没有它们,你实际上试图调用的不是方法(getters),而是调用字段本身,并且它们具有私有访问权。
正确的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");