嗨全部 我開發了一個web服務。當兩個不同的用戶試圖同時訪問Web服務時,我遇到了問題。 在Web服務的兩種方法都存在併發web服務的問題
- setInputParameter
getUserService
假設
Time User Operation 10:10 am user1 setInputParameter 10:15 am user2 setInputParameter 10:20 am user1 getUserService
用戶1根據由用戶2 seted不輸入參數得到的結果(他自己)
我使用Axis2 1.4,日食ant構建, 我的服務是放在這裏
- User類
- 服務類
- service.xml的
- 構建文件
- 的TestClass
package com.jimmy.pojo;
public class User {
private String firstName;
private String lastName;
private String[] addressCity;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String[] getAddressCity() {
return addressCity;
}
public void setAddressCity(String[] addressCity) {
this.addressCity = addressCity;
}
}
[/code]
[code=java]package com.jimmy.service;
import com.jimmy.pojo.User;
public class UserService {
private User user;
public void setInputParameter(User userInput) {
user = userInput;
}
public User getUserService() {
user.setFirstName(user.getFirstName() + " changed ");
if (user.getAddressCity() == null) {
user.setAddressCity(new String[] { "New City Added" });
} else {
user.getAddressCity()[0] = "===========";
}
return user;
}
}
[/code]
[code=java]<service name="MyWebServices" scope="application">
<description>
My Web Service
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<parameter name="ServiceClass">com.jimmy.service.UserService
</parameter>
</service>[/code]
[code=java]
<project name="MyWebServices" basedir="." default="generate.service">
<property name="service.name" value="UserService" />
<property name="dest.dir" value="build" />
<property name="dest.dir.classes" value="${dest.dir}/${service.name}" />
<property name="dest.dir.lib" value="${dest.dir}/lib" />
<property name="axis2.home" value="../../" />
<property name="repository.path" value="${axis2.home}/repository" />
<path id="build.class.path">
<fileset dir="${axis2.home}/lib">
<include name="*.jar" />
</fileset>
</path>
<path id="client.class.path">
<fileset dir="${axis2.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${dest.dir.lib}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${dest.dir}" />
<delete dir="src" includes="com/jimmy/pojo/stub/**"/>
</target>
<target name="prepare">
<mkdir dir="${dest.dir}" />
<mkdir dir="${dest.dir}/lib" />
<mkdir dir="${dest.dir.classes}" />
<mkdir dir="${dest.dir.classes}/META-INF" />
</target>
<target name="generate.service" depends="clean,prepare">
<copy file="src/META-INF/services.xml" tofile="${dest.dir.classes}/META-INF/services.xml" overwrite="true" />
<javac srcdir="src" destdir="${dest.dir.classes}" includes="com/jimmy/service/**,com/jimmy/pojo/**">
<classpath refid="build.class.path" />
</javac>
<jar basedir="${dest.dir.classes}" destfile="${dest.dir}/${service.name}.aar" />
<copy file="${dest.dir}/${service.name}.aar" tofile="${repository.path}/services/${service.name}.aar" overwrite="true" />
</target>
</project>
[/code]
[code=java]package com.jimmy.test;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.jimmy.pojo.User;
public class MyWebServices {
@SuppressWarnings("unchecked")
public static void main(String[] args1) throws AxisFault {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/MyWebServices");
options.setTo(targetEPR);
// Setting the Input Parameter
QName opSetQName = new QName("http://service.jimmy.com",
"setInputParameter");
User user = new User();
String[] cityList = new String[] { "Bangalore", "Mumbai" };
/* We need to set this for user 2 as user 2 */
user.setFirstName("User 1 first name");
user.setLastName("User 1 Last name");
user.setAddressCity(cityList);
Object[] opSetInptArgs = new Object[] { user };
serviceClient.invokeRobust(opSetQName, opSetInptArgs);
// Getting the weather
QName opGetWeather = new QName("http://service.jimmy.com",
"getUserService");
Object[] opGetWeatherArgs = new Object[] {};
Class[] returnTypes = new Class[] { User.class };
Object[] response = serviceClient.invokeBlocking(opGetWeather,
opGetWeatherArgs, returnTypes);
System.out.println("Context :"+serviceClient.getServiceContext());
User result = (User) response[0];
if (result == null) {
System.out.println("User is not initialized!");
return;
} else {
System.out.println("*********printing result********");
String[] list =result.getAddressCity();
System.out.println(result.getFirstName());
System.out.println(result.getLastName());
for (int indx = 0; indx < list.length ; indx++) {
String string = result.getAddressCity()[indx];
System.out.println(string);
}
}
}
}