构建async httpbuilder类似于httpbuilder

时间:2013-12-24 12:52:10

标签: java asynchronous groovy neo4j httpbuilder

可能是错误的地方发布这个,但我一直在搞乱异步http建设者试图让基本的密码查询工作。它适用于Http Builders但无法使用异步版本。

   @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' )
    @Grab(group='net.sf.json-lib', module='json-lib', version='2.4', classifier='jdk15' )

    import groovyx.net.http.*
    import static groovyx.net.http.ContentType.*
    import static groovyx.net.http.Method.*

    def query(statement, params,success, error) {
    def http = new HTTPBuilder( 'http://localhost:7474' )
    http.request( POST, JSON ) {
    uri.path = '/db/data/cypher/'
    headers.'X-Stream' = 'true'
    requestContentType = JSON
    body =  [ query : statement , params : params ?: [:] ]

     // uri.query = [ param : 'value' ]

     response.success = { resp, json ->
       if (success) success(json)
       else {
        println "Status ${resp.statusLine} Columns ${json.columns}\nData: ${json.data}"
       }
     }

     response.failure = { resp, message ->
           def result=[status:resp.statusLine.statusCode,statusText:resp.statusLine.reasonPhrase]
           result.headers = resp.headers.collect { h -> [ (h.name) : h.value ] }
           result.message = message
       if (error) {
                 error(result)
       } else {
        println "Status: ${result.status} : ${result.statusText} "
        println 'Headers: ${result.headers}'
        println 'Message: ${result.message}'
       }
     }
    }
   }

query("MATCH n RETURN n;",[],{ println "Success: ${it}" },{ println "Error: ${it}" })

但是我已经尝试使用AsyncHttpBuilder。无法让它工作。现在我正在尝试一个简单的事情,并且无法让它给出任何有用的结果。

@Test
public void testQueue()
{
    def http = new AsyncHTTPBuilder( poolSize : 1 ,
            uri : 'http://localhost:7474/db/data/cypher' )
    def responses = []
    responses << http.post(query : [q:  "MATCH n RETURN n;"]) {return it}


    if (!responses.every{it.done})
    {
        println 'waiting...'
        Thread.sleep(2000)
    }
    responses.each {
        println(it)
    }
    http.shutdown()
} 

有什么想法?谢谢!

1 个答案:

答案 0 :(得分:1)

供参考:我已在https://groups.google.com/forum/?fromgroups#!topic/neo4j/5Cle5vBsMXQ

处回答了此问题
  

您需要在请求的正文中传递密码查询,而不是   查询参数。有关,请参阅https://gist.github.com/sarmbruster/8114445   工作实例