【问题标题】:XML-RPC exception switching from execute to executeAsyncXML-RPC 异常从 execute 切换到 executeAsync
【发布时间】:2014-05-05 04:09:51
【问题描述】:

我有以下 XML-RPC 实现工作,我从 apache website 复制并稍作修改。

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 中,不再适用。此外,我从类的开头省略了 import 语句(肯定没有语法错误)。任何帮助将不胜感激。

【问题讨论】:

    标签: java apache asynchronous xml-rpc


    【解决方案1】:

    您的主程序正在运行结束,因为 executeAsync 立即返回,而无需等待发送请求或返回响应。

    您想通过使用 executeAsync 完成什么?

    【讨论】:

    • 我将用一个需要更多时间的函数替换我拥有的 sum 函数。因此,我想调用异步执行。您的“跑完最后”评论也解决了这个问题。我放了一段时间(真);现在在我的代码末尾声明并且它有效!
    猜你喜欢
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2013-02-17
    • 2020-07-31
    • 2013-11-13
    • 2018-05-06
    相关资源
    最近更新 更多