定时器计划如何工作?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定时器计划如何工作?相关的知识,希望对你有一定的参考价值。
我使用计时器计划,如果我无法从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);
}
}
答案
我将timeOut用于服务,如下面的代码。
为什么我比定时器时间表之前得到“开始”和“结束”字样?
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
=============================");
}
}
}
}, 0, 1000);
}
System.out.println("END");
} catch (Exception e) {
System.out.println("EXCEPTIOn");
}
return "OK" + txt;
开始
结束
count => 1 Mehman
超时
=============================
count => 2 Mehman
超时
=============================
count => 3 Mehman
超时
=============================
count => 4 Mehman
超时
=============================
count => 5 Mehman
超时
=============================
count => 6 Mehman
超时
=============================
count => 7 Mehman
超时
=============================
以上是关于定时器计划如何工作?的主要内容,如果未能解决你的问题,请参考以下文章