2016-04-18 47 views
1

我有以下的Java代碼,我要轉換爲常規: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: [[email protected]155] 
    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-方法有簽名:

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

我試着投 「新ExecStartResultCallback(的System.out,System.err的)」 到 「ResultCallback」,但沒有奏效。

有沒有辦法強制Groovy將實例作爲ResultCallback-Instance處理,以便調用正確的方法?

問候, marbon

+0

請先添加'stacktrace'。那麼只有我們能夠幫助你解決@marbon。 'ExecStartResultCallback'類是'ResultCallbackTemplate'類,不是類型爲'ResultCallback'的類,如這裏所述>> https://github.com/docker-java/docker-java/blob/master/src/main/java/ com/github/dockerjava/core/command/ExecStartResultCallback.java –

+0

@VikrantKashyap但是'ResultCallbackTemplate'是一個'ResultCallback',因爲它在這裏說:https://github.com/docker-java/docker-java/blob/master/的src /主/ JAVA/COM/github上/ dockerjava /核心/異步/ ResultCallbackTemplate.java –

回答

1

一位同事幫助解決這個問題,我們發現,該實例dockerClient使用定製的ClassLoader,這我有一些問題。它可以通過與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()