错误:java.io.NotSerializableException:org.apache.commons.httpclient.HttpClient GROOVY

时间:2018-06-29 09:28:27

标签: jenkins groovy httpclient

我对Groovy脚本有疑问。

我正在对Jenkins进行POST调用,以访问执行Job的结果。

结果以JSON返回给我。因为我从非序列化对象返回错误,所以我使用了导入groovy.json.JsonSlurperClassic库来解析结果,该错误已得到解决。

但是现在它返回此错误:

an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@6db9f892
    in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@5338e01a
    in field com.cloudbees.groovy.cps.impl.CallEnv.caller
    in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@65caceba
    in field com.cloudbees.groovy.cps.Continuable.e
    in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@5fc119bf
    in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
    in object org.jenkinsci.plugins.workflow.cps.CpsThread@29f6e53a
    in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@18a9b692
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@18a9b692
**Caused: java.io.NotSerializableException: org.apache.commons.httpclient.HttpClient**
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.HashMap.internalWriteEntries(HashMap.java:1785)
    at java.util.HashMap.writeObject(HashMap.java:1362)
    at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:273)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.TreeMap.writeObject(TreeMap.java:2438)
    at sun.reflect.GeneratedMethodAccessor446.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:273)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.lambda$writeObject$0(RiverWriter.java:144)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:143)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:467)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:443)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:430)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:367)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:82)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Mi代码是:

def check_deployment(String proyecto, String country){

    proyecto = "#" + country + "-" + proyecto + "-" + env.BUILD_NUMBER

    // Credentials
    def username = commons_tasks.get_credentials("JENKINS_CD").user.toString()
    def password = commons_tasks.get_credentials("JENKINS_CD").password.toString()

    // Login
    def client = new HttpClient()
        client.state.setCredentials(
        new AuthScope( "tower.lares.dsd", 443, "realm"),
        new UsernamePasswordCredentials(username , password)
    )

    client.params.authenticationPreemptive = true

    def result_build = false
    def status_build = null

    while(!result_build){

        def post = new PostMethod("https://{URL_DOMAIN}/jenkins/job/{JOB_JENKINS}/api/json?depth=1")
        post.doAuthentication = true
        client.executeMethod(post)


        def response = post.getResponseBodyAsString()
        def jsonParse = new JsonSlurperClassic().parseText(response)


        def builds = jsonParse.builds;
        builds.find{
            if (proyecto == it.displayName){
                if (it.result != null){
                    result_build = true
                }
                status_build = it.result
                return true
            }
        }
    }


    if (status_build != "SUCCESS"){
        sh "exit 0"
    }else{
        sh "exit 1"
    }

}

我不知道问题是否可能是因为我还没有关闭客户端……还是我可以做些其他事情。

我试图放:  @ 非每次点击费用 在课程开始时,但不直接执行。

我还尝试将结果变量设置为 transient ,以表示它不是可序列化的...但是我仍然会收到错误消息。

有人知道这会是什么吗?

非常感谢您

1 个答案:

答案 0 :(得分:0)

如果我放置一个println()来返回我在JSON中寻找的某些字段的状态,则它不会将其返回给我。 我了解所有无法序列化的内容,不是吗?

我不知道在放置@ NonCPS 时的行为是什么,但是例如,如果在这些位置放置回显,则仅执行第一个:

@NonCPS
def check_deployment(String proyecto, String country){

    proyecto = "#es-integration-services-esb-dev-1.0.0-SNAPSHOT-"

    echo "Init call the Ansible's JOB"


    // Credentials
    def username = commons_tasks.get_credentials("JENKINS_CD").user.toString()
    def password = commons_tasks.get_credentials("JENKINS_CD").password.toString()


    // Login
    def client = new HttpClient()
        client.state.setCredentials(
        new AuthScope( "tower.lares.dsd", 443, "realm"),
        new UsernamePasswordCredentials(username , password)
    )

    echo "Client"
    client.params.authenticationPreemptive = true

    def result_build = false
    def status_build = null

    echo "While"
    while(!result_build){

        def post = new PostMethod("https://{URL_DOMAIN}/jenkins/job/{JOB_JENKINS}/api/json?depth=1")
        post.doAuthentication = true
        client.executeMethod(post)

        echo "Response"
        def response = post.getResponseBodyAsString()
        def jsonParse = new JsonSlurperClassic().parseText(response)

        echo "It JSON FOR"
        def builds = jsonParse.builds;
        builds.find{
            if (proyecto == it.displayName){
                println(" -- ID:  "+ it.id + " Nombre: " + it.displayName + " -> result is:  " + it.result + " ->  URL :  " + it.url + "Token: " + env.BUILD_NUMBER)
                if (it.result != null){
                    result_build = true
                }
                    status_build = it.result
                    return true
            }
        }
    }

    if (status_build != "SUCCESS"){
        sh "exit 0"
    }else{
        sh "exit 1"
    }

}

控制台日志为:

[Pipeline] echo
**Init call the Ansible's JOB**
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/...
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
[Pipeline] cleanWs
[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
[Pipeline] echo
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS

请仅向我展示第一个回声“初始化呼叫Ansible的工作”

谢谢您的回答!