【问题标题】:How scala implicit make abstract method valid parameter so I can use method in javascala如何隐式使抽象方法成为有效参数,以便我可以在java中使用方法
【发布时间】:2019-11-02 11:42:44
【问题描述】:

所以这是一个有点奇怪的问题,但我正在尝试使用 scala 中的一段代码并采用一组参数。在 scala 中,参数是隐含的,因此 scala 能够发挥它的魔力来填充这些对象需要的东西。但是由于我无法在 java 中使抽象对象成为“隐式”对象,因此我需要弄清楚如何创建对象,但我一辈子都做不到。

这是scala代码:

class AsyncSchemaRegistryClient (
  val baseUri: String
) (
  implicit as: ActorSystem,
  m: Materializer,
  ec: ExecutionContext
) extends SchemaRegistryClient[Future] with Json4sSupport {...

所以这是由原始代码中的另一种方法调用的(在 Guice Inject 和子模块的漫长曲折路径中,很难按原样遵循),我试图在 java 代码中调用它,如下所示:

private AsyncSchemaRegistryClient asyncSchemaRegistryClient = new AsyncSchemaRegistryClient("test", ActorSystem.create(), Materializer(), new ExecutionContext);

现在 ActorSystem.create() 似乎是有效的(至少编译器没有对此大喊大叫),但 Materializer 和 ExecutionContext 我无法初始化,因为它们是抽象的。另外值得一提的是,Materializer 是 akka.stream.Materializer 而 ExecutionContext 是 scala.concurrent.ExecutionContext。

我试图使用这个 AsyncSchemaRegistryClient 的原因是它已经设置了很多代码来正确调用模式注册表并处理它是否返回有效的模式数据,这似乎是最简单的方法在我的程序中对模式实施异步检查。

提前感谢您的任何建议!

【问题讨论】:

    标签: java scala asynchronous akka scala-java-interop


    【解决方案1】:

    试试

    ActorSystem system = ActorSystem.create();
    ExecutionContextExecutor ec = system.dispatcher();
    ActorMaterializer mat = ActorMaterializer.create(system);
    
    new AsyncSchemaRegistryClient("test", system, mat, ec);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多