0
我有以下XML-RPC實現工作,我從apache website中複製並稍作修改。XML-RPC異常從執行到執行異步切換
public class DemoServer {
public static void main (String [] args) {
try {
WebServer webServer = new WebServer(8080);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("sample", RequestHandler.class);
xmlRpcServer.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig =
(XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
webServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
隨着客戶端:
public class DemoClient {
public static void main (String[] args) {
try {
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();
// set configuration
client.setConfig(config);
// make the a regular call
Object[] params = new Object[] { new Integer(2), new Integer(3) };
//!CRITICAL LINE!
Integer result = (Integer) client.execute("sample.sum", params);
System.out.println("2 + 3 = " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我首先運行demoServer的,然後我運行DemoClient,和它打印 「2 + 3 = 5」。 但是,如果我改變
Integer result = (Integer) client.execute("sample.sum", params);
到
client.executeAsync("sample.sum", params, new ClientCallback());
然後我得到以下幾點:
In error
java.lang.ExceptionInInitializerError
at java.lang.Runtime.addShutdownHook(Runtime.java:192)
at java.util.logging.LogManager.<init>(LogManager.java:237)
at java.util.logging.LogManager$1.run(LogManager.java:177)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.logging.LogManager.<clinit>(LogManager.java:158)
at java.util.logging.Logger.getLogger(Logger.java:273)
at sun.net.www.protocol.http.HttpURLConnection.<clinit>(HttpURLConnection.java:62)
at sun.net.www.protocol.http.Handler.openConnection(Handler.java:44)
at sun.net.www.protocol.http.Handler.openConnection(Handler.java:39)
at java.net.URL.openConnection(URL.java:945)
at org.apache.xmlrpc.client.XmlRpcSun15HttpTransport.newURLConnection(XmlRpcSun15HttpTransport.java:62)
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:62)
at org.apache.xmlrpc.client.XmlRpcClientWorker$1.run(XmlRpcClientWorker.java:80)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.IllegalStateException: Shutdown in progress
at java.lang.Shutdown.add(Shutdown.java:62)
at java.lang.ApplicationShutdownHooks.<clinit>(ApplicationShutdownHooks.java:21)
... 14 more
我ClientCallback類:
public class ClientCallback implements AsyncCallback {
@Override
public void handleError(XmlRpcRequest request, Throwable t) {
System.out.println("In error");
t.printStackTrace();
}
@Override
public void handleResult(XmlRpcRequest request, Object result) {
System.out.println("In result");
System.out.println(request.getMethodName() + ": " + result);
}
}
這是怎麼回事錯在這裏?我正在使用Apache XML-RPC版本3.1.2,不幸的是我發現的示例代碼在版本2.x中,並且不再適用。另外我從類的開始處省略了導入語句(確實沒有語法錯誤)。任何幫助將非常感激。
我將要取代總和功能,我將花費更多的時間。因此,我想讓這個調用異步執行。此外,您的「跑題」評論解決了這個問題。我放了一段時間(真);聲明在我的代碼結束現在和它的作品! – nmore