开发者

Problem with Apache's Java XMLRPC library

开发者 https://www.devze.com 2022-12-10 03:50 出处:网络
So i\'m trying to get my Apache xmlrpc client/server implementation to play ball. Everything works fine except for one crucial issue:

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. 开发者_如何学GoIt 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?


So, for anyone else who still wants to use XMLRPC here's how I fixed this issue:

http://xmlrpc.sourceforge.net/

far superior to apache xmlrpc, in my opinion.


This is standard behaviour of Apache XMLRPC 3.x. http://ws.apache.org/xmlrpc/handlerCreation.html:

By default, Apache XML-RPC creates a new object for processing each request received at the server side.

However, you can emulate the behaviour of XMLRPC 2.x, where you registered handler objects instead of handler classes, using a RequestProcessorFactoryFactory. I have written a custom RequestProcessorFactoryFactory that you can use:

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;
      }
    });
  }

}

This can then be used with e.g. a XMLRPC WebServer like this

  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());


Maybe something to do with javax.xml.rpc.session.maintain set to true?


I know this is a really old post but I managed to solve the problem with Apache's Java XML-RPC.

First, I thought this could be solved with singleton class in Java but it doesn't work and throws "illegal access exception".

These are what I have done:

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
}

But metaspace increases gradually:

Problem with Apache's Java XMLRPC library

I worked too much on this metaspace issue when looping forever in Java but I couldn't figure out a solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号