Tuesday, July 22, 2008

ปัญหา grails กับ xml-rpc

ใน open source project ที่กำลังทำอยู่ (ใช้ grails)
มันมี feature หนึ่งต้องสามารถรับ request เป็น xml-rpc ได้

จัดการเคาะ google หนึ่งที ก็ได้ความว่ามี plugin xmlrpc ให้ใช้
จัดการสั่ง grails install-plugin xmlrpc
implement service, controller อีก ~20 บรรทัด ก็ได้ xml-rpc ฝั่ง server มาเชยชม

จากนั้นก็ทดลองเขียน xml-rpc client ด้วย python
ทดลอง run ดูก็พบว่าโลกไม่สวยงามอย่างที่คิด
มี stack trace ยอดยืดเป็นหางว่าว
debug พักใหญ่ๆ ก็ได้ความว่า มันเป็น bug ตัวนี้

http://jira.codehaus.org/browse/GRAILSPLUGINS-201
http://jira.codehaus.org/browse/GRAILS-2017

อืมม์ fixed โดยการ disable feature หนึ่งทิ้งไป
แสดงว่าเราต้องเลือกว่า อยากได้ feature ไหนมากกว่ากัน
ในเบื้องต้นเนื่องจาก xmlrpc ผมยังเป็น rpc ตัวน้อยๆอยู่
ผมก็เลยเลือกว่าจะไม่ใช้ xmlrpc plugin (ซึ่งหมายความว่าต้อง implement xmlrpc แบบง่ายๆด้วยตัวเอง)

วิธีการง่ายๆแบบนี้เลย
class RpcController {

def pingService

def ping = {
// ตรงนี้ grails มันจัดการแปลง xml ที่ส่งมาใน request body
// ให้อยู่ใน params object
def method = params.methodCall
def methodName = method.methodName
def methodParams = method.params

def result
if (methodName == 'ping') {
result = pingService.ping(methodParams)
} else {
result = "error: mismatch methodName ${methodName}, expect 'ping'"
}

def writer = new StringWriter()
def mkb = new groovy.xml.MarkupBuilder(writer)

mkb.methodResponse {
params {
param {
value {
string(result)
}
}
}
}
render(writer.toString())
}
}


Update: พบปัญหาว่า กรณีมี params มากกว่า 1 ตัวแล้ว ดูเหมือนมันจะ parse ให้ผิด
ตอนนี้เลย switch ไปใช้ xml-rpc plugin แล้วทำการ disable auto xml parsing ไปก่อน

Related link from Roti

2 comments:

chanwit said...

พี่ไม่ลองใช้ xfire plugin ดูเหรอครับ :-) ผมลองเทียบ dispatching overhead แล้ว XML-RPC กับ SOAP ดูจะไม่ต่างกันมาก

ถ้าใช้ xfire มีปัญหาอะไรผมก็พอช่วย hack ได้บ้างครับ (ยกเว้น circular referencing ที่ตัว xfire มันไม่ support)

PPhetra said...

:) ถูกบังคับโดย client น่ะ