XMLRPC サーバへの通信方法
SOAP 通信に続いて、XMLRPC サーバに Java からアクセスしないといけないので、Java でアクセスする方法を調べるついでに Python, Ruby, Perl, PHP も調べた。
以下メモ書き。
XMLRPC サーバ
XMLRPC サーバは Python で作った(今回はクライアントの通信テスト用なのでエラーとかも適当)。
# 多分どの言語でも作れるけど、先日あった Python 自習室で XMLRPC の話題があったので Python で。
#!/usr/bin/env python # -*- coding: utf-8 -*- from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2',) class Operation(object): def addition(self, x, y): return x + y def add_list(self, l): ret = 0 for data in l: ret += data return ret host = 'localhost' port = 9000 server = SimpleXMLRPCServer((host, port), requestHandler=RequestHandler) server.register_introspection_functions() server.register_instance(Operation()) server.serve_forever()
引数の加算と配列を加算するもの。
Python
#!/usr/bin/env python # -*- coding: utf-8 -*- import xmlrpclib client = xmlrpclib.ServerProxy('http://localhost:9000') print client.addition(1, 2) print client.add_list([1, 2, 3, 4, 5])
3 15
簡単。
Ruby
#!/usr/bin/env ruby require 'xmlrpc/client' client = XMLRPC::Client.new2('http://localhost:9000/RPC2') p client.call('addition', 1, 2) p client.call('add_list', [1, 2, 3, 4, 5])
3 15
PHP
XMLRPC クライアントの拡張モジュールは見つからなかったので、Zend_XmlRpc_Client を使った。
<?php require_once 'Zend/XmlRpc/Client.php'; $client = new Zend_XmlRpc_Client('http://localhost:9000/RPC2'); var_dump($client->call('addition', array(1, 2))); var_dump($client->call('add_list', array(array(1, 2, 3, 4, 5))));
int(3) int(15)
ライブラリを使えば簡単に出来る。
Pear 版は試してないけど、多分同じように出来ると思う。
Perl
XMLRPC::Lite モジュールを使用する。
Plagger でも使われてるっぽいからこれで良いのかな。
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use XMLRPC::Lite; my $client = XMLRPC::Lite->proxy('http://localhost:9000/RPC2'); warn Dumper $client->call('addition', 1, 2)->result; warn Dumper $client->call('add_list', [1, 2, 3, 4, 5])->result;
$VAR1 = '3'; $VAR1 = '15';
CPAN モジュールは偉大。
Java
一番めんどくさかった。
XMLRPC クライアントに Apache の XML-RPC クライアントを使った。
自分は Maven2 を使用しているので、まずは依存モジュールを pom.xml に追加。
<dependency> <groupId>org.apache.xmlrpc</groupId> <artifactId>xmlrpc-client</artifactId> <version>3.1.3</version> </dependency>
dependencies に追加。
バージョン番号は Changes をみて最新を。
これでビルドすると自動的にダウンロードしてくれる。
package org.me.calculate; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; public class Main { public static void main(String[] args) { Logger logger = Logger.getLogger(XmlRpc.class.getName()); XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); try { config.setServerURL(new URL("http://localhost:9000/RPC2")); } catch (MalformedURLException ex) { logger.log(Level.SEVERE, null, ex); } XmlRpcClient client = new XmlRpcClient(); client.setConfig(config); try { Object[] params = new Object[] {1, 2}; Integer result = (Integer) client.execute("addition", params); logger.info(result.toString()); List<Integer> list = new ArrayList<Integer>(); int[] data = {1, 2, 3, 4, 5}; for (int i : data) { list.add(i); } params = new Object[] { list }; result = (Integer) client.execute("add_list", params); logger.info(result.toString()); } catch (XmlRpcException ex) { logger.log(Level.SEVERE, null, ex); } } }
2010/08/04 16:31:10 org.me.calculate.Main execute 情報: 3 2010/08/04 16:31:10 org.me.calculate.Main execute 情報: 15
引数の渡し方が Object なので色々めんどくさいがとりあえず取れた。
以下のサイトを参考にした。
ws-xmlrpc - The Apache XML-RPC Client
404 shin1のつぶやき ないわー Not Found: Trac XMLRPCをJavaから使用する