大家好 我开发了一个Web服务。当两个不同的用户同时尝试访问Web服务时,我遇到了问题。 在Web服务中有两种方法
getUserService
假设
Time User Operation
10:10 am user1 setInputParameter
10:15 am user2 setInputParameter
10:20 am user1 getUserService
User1根据user2选择的输入参数获取结果,而不是(他自己)
我正在使用axis2 1.4,eclipse ant build, 我的服务就在这里
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);
}
}
}
}
答案 0 :(得分:3)
在我看来,您正试图在两次后续的Web服务调用之间保持状态。为此,您在Service类中维护一个私有字段。但是,由于服务类未在每个请求上实例化,因此您将在webservice的所有用户之间共享状态。
您需要做的是将状态存储在某种会话范围内。然后,Web服务客户端将需要为每个请求传回一些会话令牌。
使用Axis 2执行有状态Web服务的一种方法是详细here
答案 1 :(得分:0)
我不熟悉这些工具。
此链接有用吗? Tomcat 5.5 Axis2 application scope problem - Unable to create single instance
当客户端调用它时,应用程序服务器似乎不会创建(包含该类)Web方法的不同实例。