There is a issue confused me so much when I using Apache XML RPC
Below is the code
public class AdderImpl implements Adder{
private Object obj=new String("Obj1");
public int add(int pNum1, int pNum2) {
obj="Changed";
return pNum1 + pNum2;
}
public Object get(){
return this.obj;
}
}
when I call the method from the client side the Object value is still Obj1, not the "Changed"
How can I get the changed the value of the obj
Client:
public class Client {
public static void main(String [] args) throws Exception
{
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();
client.setTransportFactory(
new XmlRpcCommonsTransportFactory(client));
client.setConfig(config);
// make a call using dynamic proxy
ClientFactory factory = new ClientFactory(client);
Adder adder = (Adder) factory.newInstance(Adder.class);
int sum = adder.add(2, 4);
System.out.println("2 + 4 = " + sum);
System.out.println(adder.get()==null?true:false);
System.out.println(adder.get().开发者_JAVA百科toString());
}
}
Thanks in advance
A new handler get's created each time. To obtain the behaviour you want you have the following options:
- Write the value to a database/file (i.e. persistence storage) and read/write it from there.
Make the field static, i.e.
private static Object obj=new String("Obj1");
Hope that helps.
精彩评论