3

So i'm trying to get my Apache xmlrpc client/server implementation to play ball. Everything works fine except for one crucial issue:

my handler class (mapped through the properties file org.apache.xmlrpc.webserver.XmlRpcServlet.properties) reacts as it should but it's constructor is called at every method invocation. It would seem that the handler class is instantiated at each call which is bad because I have data stored in instance variables that I need to save between calls.

How do I save a reference to the instantiated handler so that I can access it's instance variables?

4

4 回答 4

1

因此,对于仍想使用 XMLRPC 的其他人来说,这是我解决此问题的方法:

http://xmlrpc.sourceforge.net/

在我看来,远远优于 apache xmlrpc。

于 2010-03-23T10:32:14.787 回答
1

这是 Apache XMLRPC 3.x 的标准行为。http://ws.apache.org/xmlrpc/handlerCreation.html

默认情况下,Apache XML-RPC 创建一个新对象来处理服务器端收到的每个请求。

但是,您可以模拟 XMLRPC 2.x 的行为,您可以使用 RequestProcessorFactoryFactory 注册处理程序对象而不是处理程序类。我编写了一个可以使用的自定义 RequestProcessorFactoryFactory:

public class CustomHandler implements RequestProcessorFactoryFactory {

  Map<Class<?>, RequestProcessorFactory> handlers = 
    Collections.synchronizedMap(
      new HashMap<Class<?>, RequestProcessorFactory>());

  @Override
  public RequestProcessorFactory getRequestProcessorFactory(Class pClass) 
      throws XmlRpcException {
    return handlers.get(pClass);
  }

  public void addHandler(final Object handler) {
    handlers.put(handler.getClass(), new RequestProcessorFactory() {
      @Override
      public Object getRequestProcessor(XmlRpcRequest pRequest) 
          throws XmlRpcException {
        return handler;
      }
    });
  }

}

然后可以将其与例如这样的 XMLRPC WebServer 一起使用

  WebServer server = ...
  PropertyHandlerMapping phm = new PropertyHandlerMapping();
  server.getXmlRpcServer().setHandlerMapping(phm);
  Custom sh = new CustomHandler();
  phm.setRequestProcessorFactoryFactory(sh);
  Object handler = ... /** The object you want to expose via XMLRPC */
  sh.addHandler(handler);
  phm.addHandler(serverName, handler.getClass());
于 2013-02-06T13:58:04.003 回答
0

也许与javax.xml.rpc.session.maintain设置为真有关?

于 2009-10-28T12:33:46.790 回答
0

我知道这是一篇很老的帖子,但我设法用 Apache 的 Java XML-RPC 解决了这个问题。

首先,我认为这可以通过 Java 中的单例类解决,但它不起作用并引发“非法访问异常”。

这些是我所做的:

public class XmlRpcServer {

private static JFrame frame = new JFrame();
private static JPanel pane = new JPanel();

public static XmlRpcServer singleton_inst = new XmlRpcServer();

public XmlRpcServer() {
    
    // I kept the constructor empty.
}

public static void main(String[] args) throws XmlRpcException, IOException {

// In my case, I put the constructor code here.
// Then stuff for XML-RPC server
// Server Part
    WebServer ws = new WebServer(8741);
    
    PropertyHandlerMapping mapping = new PropertyHandlerMapping();
    
    mapping.addHandler("SERVER", singleton_inst.getClass());
    
    ws.getXmlRpcServer().setHandlerMapping(mapping);
    ws.start();
    ////
    
}
// I called doTheJob() from python via XML-RPC
public String doTheJob(String s) throws XmlRpcException {
    loop();
    return s;
}
// It executed loop() forever
private static void loop() throws XmlRpcException {
    // Actual work is here
}

但元空间逐渐增加:

在此处输入图像描述

在 Java 中永远循环时,我在这个元空间问题上工作了太多,但我找不到解决方案。

于 2021-11-02T13:18:29.977 回答