Service:
package org.wso2.carbon.service;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceContext;
public class spsessionservctxservice {
public int multiply(int x, int y){
System.out.println("Setting context!!!!!");
System.out.println(x*y);
ServiceContext serviceContext =
MessageContext.getCurrentMessageContext().getServiceContext();
serviceContext.setProperty("VALUE", new Integer(x * y));
return 0;
}
public int getResult(){
System.out.println("Getting results!!!!!");
ServiceContext serviceContext =
MessageContext.getCurrentMessageContext().getServiceContext();
return ((Integer) serviceContext.getProperty("VALUE")).intValue();
}
}
You can download the service from:
https://svn.wso2.org/repos/wso2/trunk/commons/qa/qa-artifacts/app-server/session-management/spsessionservctxservice.aar
Deploy them in two instances in the same cluster.
Client:
package org.wso2.carbon.service;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import java.util.Random;
import java.rmi.RemoteException;
import org.wso2.carbon.service.SpsessionservctxserviceStub;
public class Client {
public static void main(String[] args) throws AxisFault {
Random random = new Random();
for(int x=0; x<10; x++){
int j = random.nextInt(1000);
int k = random.nextInt(2000);
ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem("/home/chamara/Carbon4/wso2as-5.0.0-SNAPSHOT_node1/repository", null);
SpsessionservctxserviceStub stb1 = new SpsessionservctxserviceStub(configContext, "http://10.200.2.75:9763/node1/services/spsessionservctxservice/");
//SpsessionservctxserviceStub.Multiply request = new SpsessionservctxserviceStub.Multiply();
ServiceClient sc = stb1._getServiceClient();
//sc.engageModule("addressing");
stb1._getServiceClient().engageModule("addressing");
Options opts = sc.getOptions();
opts.setManageSession(true);
sc.setOptions(opts);
//request.setX(j);
//request.setY(k);
try {
stb1.multiply(j,k);
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(1);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SpsessionservctxserviceStub stb2 = new SpsessionservctxserviceStub(configContext,"http://10.200.2.75:9764/node2/services/spsessionservctxservice/");
stb2._setServiceClient(sc);
stb2._getServiceClient().getOptions().setTo(new EndpointReference("http://10.200.2.75:9764/node2/services/spsessionservctxservice/"));
try {
/*
SpsessionservctxserviceStub.GetResultResponse res = stb2.getResult();
System.out.println(res.get_return());
*/
System.out.println("Result is: " + stb2.getResult());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Set the urls in multiply method to one server's service endpoint and getRequest method to the other server's endpoint.
The multiply method will put the state in the cluster and the cluster will keep it and replicate among nodes. The second method getRequest will get the kept state from any other node of the cluster.
No comments:
Post a Comment