I'm trying to create a Web Service with Axis2 and Tomcat 7. Everything is working great except I don't understand the following behavior:
I've created a Web Service with 2 operations, one sets an int local variable and the other one returns it, code looks like this:
package testServer;
public class service {
public int number;
public void setNumber(int i){ this.number = i; }
public int getNumber(){ return this.number; }
}
Client side looks like this:
package testserver;
import java.rmi.RemoteException;
import testserver.ServiceStub;
import testserver.ServiceStub.*;
public class CallService {
public CallService(){};
public void call() throws RemoteException{
ServiceStub s = new ServiceStub();
ServiceStub.SetNumber params = new ServiceStub.SetNumber();
params.setI(2);
s.setNumber(params);
ServiceStub.GetNumber n = new ServiceStub.GetNumber();
ServiceStub.GetNumberResponse r = s.getNumber(n);
System.out.println("number is: " + r.get_return());
}
}
Now, I'm expecting to get a "number is: 2" but instead I'm getting a "number is: 0". Can Anyone explain that to me please?
Because at each invocation, a different instance of the class is used.
In the client, you only have a ServiceStub
instance. But the server is creating a new instance of Service
(check CAPITALIZATION!!) for each request you make.
This is not as bad as you may think, think that the server does not really know where the requests are from.
To get the 2
, you could make the variable static
, just for testing. The "real" solution would be calling your server business logic methods (EJBs, POJOs) and have them store and retrieve the value.