我正在尝试编写一个多线程的Web服务,每个服务从一个线程调用4个webservices。当我尝试直接调用webservice时,它可以工作。但是当我尝试从一个线程调用它会抛出一个NullPointerException。有什么想法吗?
@WebService(serviceName = "Negotiator")
public class Negotiator implements Runnable{
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Negotiate4/Negotiate4.wsdl")
private Negotiate4_Service service_3;
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Negotiate3/Negotiate3.wsdl")
private Negotiate3_Service service_2;
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Negotiate2/Negotiate2.wsdl")
private Negotiate2_Service service_1;
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Negotiate1/Negotiate1.wsdl")
private Negotiate1_Service service;
private String ret[] = new String[10];
private Thread t[] = new Thread[10];
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
try {
Thread t1 = new Thread(new Negotiator(),"prov1");
Thread t2 = new Thread(new Negotiator(),"prov2");
Thread t3 = new Thread(new Negotiator(),"prov3");
Thread t4 = new Thread(new Negotiator(),"prov4");
t1.start();
t2.start();
t3.start();
t4.start();
t1.join();
t2.join();
t3.join();
t4.join();
}
catch(java.lang.InterruptedException e)
{
System.out.println("Exception: "+e);
}
String retString = "";
for(int i=0;i<4;i++) {
retString = retString + ret[i];
}
return "Hello " + retString + " !";
}
@Override
public void run()
{
Thread th = Thread.currentThread();
System.out.println("Inside run "+ th.getName());
switch (th.getName()) {
case "prov1":
ret[0] = negotiate("Hi1");
break;
case "prov2":
ret[1] = negotiate_1("Hi2");
break;
case "prov3":
ret[2] = negotiate_2("Hi3");
break;
default:
ret[3] = negotiate_3("Hi4");
break;
}
}
private String negotiate(java.lang.String name) {
org.prov1.Negotiate1 port1 = service.getNegotiate1Port();
return port1.negotiate(name);
}
private String negotiate_1(java.lang.String name) {
org.prov2.Negotiate2 port2 = service_1.getNegotiate2Port();
return port2.negotiate(name);
}
private String negotiate_2(java.lang.String name) {
org.prov3.Negotiate3 port3 = service_2.getNegotiate3Port();
return port3.negotiate(name);
}
private String negotiate_3(java.lang.String name) {
org.prov4.Negotiate4 port4 = service_3.getNegotiate4Port();
return port4.negotiate(name);
}
}
当我尝试从hello方法内部调用协商操作时,它可以工作。但是当从run()内部调用negotiate时,我得到一个NullPointerException。请帮忙。
答案 0 :(得分:0)
问题是在当前线程中处理@WebServiceRef
,并且很可能容器通过代理保持线程本地引用。如果您使用的是Java EE7容器,则可以尝试使用ManagedExecutorService
来调用方法,但不确定它是否有效。一切都取决于谁以及如何初始化服务类。提供有关设置的更多详细信息。