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?

3
  • Do you really want/need to use XmlRpc? It's seriously old. Commented Oct 30, 2009 at 10:58
  • at this point i'm doing what they tell me in school ;) so you would suggest tossing XMLRPC and writing up the same thing in JAX-RPC? Commented Nov 3, 2009 at 18:31
  • JAX-RPC and XML-RPC are both obsolete. JAX-WS and other modern frameworks are where it's at. Commented Mar 23, 2010 at 10:34

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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:

enter image description here

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.