1

我有以下要转换为 groovy 的 Java 代码:

String containerId = "545cdc81a969";

ExecCreateCmdResponse execCreateCmdResponse = dockerClient
    .execCreateCmd(containerId)
    .withAttachStdout(true)
    .withCmd("sh", "-c", "sleep 5 && exit 5")
    .exec();

ExecStartResultCallback execStartCmd =
    dockerClient.execStartCmd(execCreateCmdResponse.getId())
         .exec(new ExecStartResultCallback(System.out, System.err))
         .awaitCompletion();

我当前的 groovy 版本是这样的:

 String id = "545cdc81a969";

    def execCreateCmdResponse = dockerClient
            .execCreateCmd(id)
            .withAttachStdout(true)
            .withCmd('sh','-c','sleep 5 && exit 5')
            .exec()


    dockerClient.execStartCmd(execCreateCmdResponse.getId())
            .withDetach(false)
            .exec(new ExecStartResultCallback(System.out, System.err))
            .awaitCompletion()

我的问题是,当我尝试运行 groovy 代码时出现以下错误:

* What went wrong:
Execution failed for task ':werner'.
> No signature of method: com.github.dockerjava.core.command.ExecStartCmdImpl.exec() is applicable for argument types: (com.github.dockerjava.core.command.ExecStartResultCallback) values: [com.github.dockerjava.core.command.ExecStartResultCallback@6ce82155]
  Possible solutions: exec(com.github.dockerjava.api.async.ResultCallback), exec(com.github.dockerjava.api.async.ResultCallback), every(), grep(), every(groovy.lang.Closure), grep(java.lang.Object)

Java-exec-Method 具有签名:

public <T extends ResultCallback<Frame>> T exec(T resultCallback);

我尝试将“new ExecStartResultCallback(System.out, System.err)”转换为“ResultCallback”,但没有成功。

有什么方法可以强制 Groovy 将实例作为 ResultCallback-Instance 处理,以便调用正确的方法?

问候,马本

4

1 回答 1

1

一位同事帮助解决了这个问题,我们发现实例 dockerClient 使用了自定义类加载器,我有一些问题。它可以通过使用来自 dockerInstance 的相同类加载器实例化新的 ExecStartResultCallback(System.out, System.err) 来解决:

    ClassLoader dockerClientClassLoader = dockerClient.getClass().getClassLoader()
    Class callbackClass = dockerClientClassLoader.loadClass("com.github.dockerjava.core.command.ExecStartResultCallback")
    def callback = callbackClass.getDeclaredConstructor(OutputStream.class, OutputStream.class).newInstance(System.out, System.err);

    dockerClient.execStartCmd(execCreateCmdResponse.getId())
            .withDetach(false)
            .exec(callback)
            .awaitCompletion()
于 2016-04-18T12:24:52.703 回答