ITeye博客

XML-RPC

frank127 · 2011-03-08 13:30 · 编程语言 · Apache · .net · XML · thread · Markdown

package org.apache.xmlrpc.demo.webserver;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

public class JavaServer {
	private static final int port = 8080;

	public static void main(String[] args) throws Exception {
		System.out.println("Attempting to start XML-RPC Server...");
		WebServer webServer = new WebServer(port);

		XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();

		PropertyHandlerMapping phm = new PropertyHandlerMapping();
		/*
		 * Load handler definitions from a property file. The property file
		 * might look like: Calculator=org.apache.xmlrpc.demo.Calculator
		 * org.apache
		 * .xmlrpc.demo.proxy.Adder=org.apache.xmlrpc.demo.proxy.AdderImpl
		 */
		phm.load(Thread.currentThread().getContextClassLoader(),
				"MyHandlers.properties");

		/*
		 * You may also provide the handler classes directly, like this:
		 * phm.addHandler("Calculator",
		 * org.apache.xmlrpc.demo.Calculator.class);
		 * phm.addHandler(org.apache.xmlrpc.demo.proxy.Adder.class.getName(),
		 * org.apache.xmlrpc.demo.proxy.AdderImpl.class);
		 */
		xmlRpcServer.setHandlerMapping(phm);

		XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer
				.getConfig();
		serverConfig.setEnabledForExtensions(true);
		serverConfig.setContentLengthOptional(false);

		webServer.start();
	}
}
package org.apache.xmlrpc.demo.client;

import java.net.URL;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.apache.xmlrpc.client.util.ClientFactory;

import com.foo.Adder;

public class JavaClient {
	public static void main(String[] args) throws Exception {
		// create configuration
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL("http://127.0.0.1:8080/xmlrpc"));
		config.setEnabledForExtensions(true);
		config.setConnectionTimeout(60 * 1000);
		config.setReplyTimeout(60 * 1000);

		XmlRpcClient client = new XmlRpcClient();

		// use Commons HttpClient as transport
		client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
		// set configuration
		client.setConfig(config);

		// make the a regular call
		Object[] params = new Object[] { new Integer(2), new Integer(3) };
		Integer result = (Integer) client.execute("Calculator.add", params);
		System.out.println("2 + 3 = " + result);

		// make a call using dynamic proxy
		ClientFactory factory = new ClientFactory(client);
		Adder adder = (Adder) factory.newInstance(Adder.class);
		int sum = adder.add(2, 4);
		System.out.println("2 + 4 = " + sum);
	}
}

MyHandlers.properties

Calculator=org.apache.xmlrpc.demo.Calculator com.foo.Adder=com.foo.AdderImpl

评论(2)

  1. #2 楼 · fxbird · 2011-04-21 13:46

    不要误导人啦,在web容器里不能用WebServer这个类。

  2. #1 楼 · ahomeeye · 2011-04-20 15:46

    大哥,下面的那个类没有提供呀。总得说明一下吧,初学者不懂的。 import com.foo.Adder;

评论功能维护中,当前仅展示历史评论。