起初我认为这是一个编程问题,因为我每10秒创建一次连接。哪些可能会创建连接并且没有正确关闭它。但是建议它不是编程而是实现/设计问题。
我需要每隔5或10秒检查一次可用的新资源,例如:testign.com/list_items。 webservices是使用cakephp开发的。
如果有任何可用资源,我将处理它。 任何人都可以帮助/指导我连接到url并每10秒轮询一次新资源。
执行以下代码的每10秒会在20次循环之后拒绝连接或连接超时异常,并且在异常开始命中之后,该网址无法加载到浏览器中。
oracleIncoming = new URL("http://calvins.restasy.com/bills/list_current/Kitchen");
inputStreamReaderList = new InputStreamReader(oracleIncoming.openStream());
//= new InputStreamReader(oracle.openStream());
for (int j = 0; j < 10;) {
URLReader.inputStreamReaderList.close();
inputStreamReaderList = new InputStreamReader(oracleIncoming.openStream());
BufferedReader in = new BufferedReader(URLReader.inputStreamReaderList);
String inputLine;
while ((inputLine = in.readLine()) != null) {
URLReader.storeInput = URLReader.storeInput + inputLine;
}
in.close();
URLReader.storeInput = "";
//send response with bill no
if (URLReader.sendBillNo == true) {
URLReader.sendBillNo = false;
oracle = new URL("http://calvins.restasy.com/bills/successfully_generated/" +billNo+"/"+"Kitchen");
inputStreamReader = new InputStreamReader(oracle.openStream());
try {
BufferedReader in2 = new BufferedReader(inputStreamReader);
String inputLine2;
while ((inputLine2 = in2.readLine()) != null) {
urlBillData2 = urlBillData2 + inputLine2;
}
in.close();
inputStreamReader.close();
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Successfully Printed Order " + billNo);
//System.out.println("http://calvins.restasy.com/bills/successfully_generated/" +billNo);
}
Thread.sleep(10000);
}
} catch (Exception e) {
System.out.println(e);
callWeb();
}
这是堆栈跟踪:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at java.net.URL.openStream(URL.java:1010)
at printerapp.URLReader.callWeb(URLReader.java:122)
at printerapp.URLReader.main(URLReader.java:71)
由于HotLicks建议仅在任何资源可用后才从Web服务响应,执行的php超时为30秒。 如何使请求保持活动超过这个时间。增加PHP执行时间限制将是一个安全问题。
提前致谢。
答案 0 :(得分:0)
while(true) {
dataIncoming = new URL("http://xyz.com/bills/list_current/Kitchen");
inputStreamReaderList = new InputStreamReader(dataIncoming.openStream());
//Your code
try {
Thread.sleep(5000);
} catch(Exception e) {
//Thread.sleep() can throw an exception if you
//notify the thread from another thread
}
}
答案 1 :(得分:0)
通常,您不应每5秒轮询一次网站。在需要接近即时响应的地方,网站应提供一种机制,您可以在其中发出获取,服务器将不会响应,直到有可用数据(尽管通常在几分钟后应该超时)。