我使用计时器计划,如果我无法从SOAP服务获得响应,则每3秒调用一次方法。
我看到,如果我无法从服务中获得响应(我使用Thread.sleep(40000L)),我的方法一直在等待。
为什么呢?
我该如何解决这个问题?
例如。
@WebService(serviceName = "NewWebService")
public class NewWebService {
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
try {
Thread.sleep(8000L);
} catch (Exception e) {
}
return "Hello " + txt + " !";
}
}
boolean status = true;
public void myMethod() {
Timer timer = new Timer();
boolean status_in_progress = false;
while (status) {
timer.schedule(new TimerTask() {
int count = 1;
@Override
public void run() {
try {
System.out.println("a");
System.out.println("count=>" + count);
count++;
if (count > 5) {
System.out.println("timer.cancel > count =>" + count);
status = false;
timer.cancel();
}
az.NewWebService_Service service = new az.NewWebService_Service();
az.NewWebService port = service.getNewWebServicePort();
String a = port.hello("Mehman"); ;
if (!a.equals("")) {
status = false;
timer.cancel();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, 0, 3000);
}
}
答案 0 :(得分:0)
我将timeOut用于服务,如下面的代码。
为什么我在计时器时间表之前得到“START”和“END”字样?
try {
System.out.println("START");
Timer timer = new Timer();
if (true) {
timer.schedule(new TimerTask() {
int counter = 0;
@Override
public void run() {
try {
counter++;
System.out.println("count=>" + counter + " " + txt);
az.NewWebService_Service service = new az.NewWebService_Service();
az.NewWebService port = service.getNewWebServicePort();
int connectionTimeOutInMs = 10;
Map<String, Object> context = ((BindingProvider) port).getRequestContext();
context.put("com.sun.xml.internal.ws.connect.timeout", connectionTimeOutInMs);
context.put("com.sun.xml.internal.ws.request.timeout", connectionTimeOutInMs);
context.put("com.sun.xml.ws.request.timeout", connectionTimeOutInMs);
context.put("com.sun.xml.ws.connect.timeout", connectionTimeOutInMs);
String a = port.hello("Mehman");
if (counter > 6) {
timer.cancel();
}
} catch (Exception ex) {
if (counter > 6) {
timer.cancel();
}
if (ex.getCause().toString().contains("Read timed out")) {
System.out.println("TimeOUT \n =============================");
}
}
}
}, 0, 1000);
}
System.out.println("END");
} catch (Exception e) {
System.out.println("EXCEPTIOn");
}
return "OK" + txt;
开始强>
<强> END 强>
count =&gt; 1 Mehman
TimeOUT
=============================
count =&gt; 2 Mehman
TimeOUT
=============================
count =&gt; 3 Mehman
TimeOUT
=============================
count =&gt; 4 Mehman
TimeOUT
=============================
count =&gt; 5 Mehman
TimeOUT
=============================
count =&gt; 6 Mehman
TimeOUT
=============================
count =&gt; 7 Mehman
TimeOUT
=============================